Question

In: Computer Science

modify the code below to create a GUI program that accepts a String from the user...

modify the code below to create a GUI program that accepts a String from the user in a TextField and reports whether or not there are repeated characters in it. Thus your program is a client of the class which you created in question 1 above.


N.B. most of the modification needs to occur in the constructor and the actionPerformed() methods. So you should spend time working out exactly what these two methods are doing, so that you can make the necessary modifications.

Q1 CODE: (do not modify this one)

public class QuestionOne {
// main funtion
public static void main(String []args) {
Scanner sc = new Scanner(System.in);
// get user input string
System.out.println("Enter a String: ");
String s = sc.nextLine();
// calling the static function and store it to a variable
boolean repeatChar = repeatCharacter(s);
// if the result of the static function is true
if(repeatChar){
// print has repeated characters
System.out.println("String has repeated characters");
}
else {
// if not display no repeated characters
System.out.println("String does not have repeated characters");
}
}
// static function that accepts string as parameter
public static boolean repeatCharacter(String s){
char ch;
//check if repeated character available or not
for(int i=0; i < s.length(); ++i) {
ch = s.charAt(i);
//check if the index and the last index of the character is same or not
if(s.indexOf(ch) != s.lastIndexOf(ch)) {   
return true;
}
}
// no character repetition return false
return false;
}

CODE TO MODIFY THE GUI: (Please modify this one)

//SimplApp.java - a simple example of a GUI program

//You should be able to give a brief description of what

//such a program will do and the steps involved

import javax.swing.*; //for JFrame, JButton, JLabel

import java.awt.*; //for Container, BorderLayout

import java.awt.event.*; //for WindowAdapter,

ActionListner, ActionEvent

public class SimplApp extends JFrame {

// define window's width and height in pixels

private static final int WIDTH = 400;

private static final int HEIGHT = 200;

// used for displaying text in the window

private JLabel infoLabel;

private class ButtonAction implements

ActionListener {

public void actionPerformed(ActionEvent e){

infoLabel.setText("You fool !!");

} //end of actionPerformed

} //end of class ButtonAction

// used to destroy/close the window

private class WindowDestroyer extends

WindowAdapter {

public void windowClosing(WindowEvent e){

dispose();

System.exit(0);

} //end of windowClosing()

} //end of class WindowDestroyer

// Below is the constructor for the class SimplApp

public SimplApp(String windowTitle) {

super(windowTitle);

setSize(WIDTH, HEIGHT);

// create content pane to add components to

window

Container c1 = getContentPane();

c1.setLayout( new BorderLayout());

// create a label component with the String

centred

infoLabel = new JLabel( "Initial",

JLabel.CENTER);

c1.add( infoLabel, BorderLayout.CENTER);

// create a button component

JButton button1=new JButton("Don't Press

Me!");

c1.add( button1, BorderLayout.NORTH);

//goes at top

// add an action event to button

ButtonAction myAction = new ButtonAction();

button1.addActionListener(myAction);

// add action event to window close button

WindowDestroyer myListener = new

WindowDestroyer();

addWindowListener( myListener);

} //end of SimplApp constructor

public static void main(String[] args) {

// calls constructor

SimplApp app = new SimplApp("Zzzz");

// display window on the screen

app.setVisible(true);

System.out.println("Finished

SimplApp.main()");

} //end of SimplApp.main()

} //end of SimplApp class

Thanks, anyone/expert answer person!


}

Solutions

Expert Solution

Code

import javax.swing.*; //for JFrame, JButton, JLabel
import java.awt.*; //for Container, BorderLayout
import java.awt.event.*; //for WindowAdapter,ActionListner, ActionEvent
public class SimplApp extends JFrame
{
// define window's width and height in pixels
private static final int WIDTH = 400;
private static final int HEIGHT = 200;
// used for displaying text in the window
private JLabel infoLabel;
private JTextField inputText;
private class ButtonAction implements
ActionListener {
public void actionPerformed(ActionEvent e){
String str=inputText.getText();
if(repeatCharacter(str))
infoLabel.setText("String has repeated characters");
else
infoLabel.setText("String does not have repeated characters.");
} //end of actionPerformed
} //end of class ButtonAction
// used to destroy/close the window
public boolean repeatCharacter(String s)
{
char ch;
//check if repeated character available or not
for(int i=0; i < s.length(); ++i) {
ch = s.charAt(i);
//check if the index and the last index of the character is same or not
if(s.indexOf(ch) != s.lastIndexOf(ch)) {   
return true;
}
}
return false;
}

private class WindowDestroyer extends WindowAdapter {
public void windowClosing(WindowEvent e){
dispose();
System.exit(0);
} //end of windowClosing()
} //end of class WindowDestroyer
// Below is the constructor for the class SimplApp
public SimplApp(String windowTitle)
{
super(windowTitle);
setSize(WIDTH, HEIGHT);
// create content pane to add components to window
Container c1 = getContentPane();
c1.setLayout( new BorderLayout());
inputText=new JTextField();
c1.add(inputText,BorderLayout.NORTH);
// create a label component with the String centred   
infoLabel = new JLabel( "Initial",JLabel.CENTER);
c1.add( infoLabel, BorderLayout.SOUTH);
// create a button component
JButton button1=new JButton("Don't Press Me!");
c1.add( button1, BorderLayout.CENTER);
  

//goes at top
// add an action event to button
ButtonAction myAction = new ButtonAction();
button1.addActionListener(myAction);
// add action event to window close button
WindowDestroyer myListener = new
WindowDestroyer();
addWindowListener( myListener);
} //end of SimplApp constructor
  
public static void main(String[] args) {
// calls constructor
SimplApp app = new SimplApp("Check String Contain Repeated Characters");
// display window on the screen
app.setVisible(true);
System.out.println("Finished SimplApp.main()");
} //end of SimplApp.main()
} //end of SimplApp class

output

If you have any query regarding the code please ask me in the comment i am here for help you. Please do not direct thumbs down just ask if you have any query. And if you like my work then please appreciates with up vote. Thank You.


Related Solutions

Use the below info to create a java program A GUI interface to ensure a user...
Use the below info to create a java program A GUI interface to ensure a user is old enough to play a game. Properly formatted prompts to input name, address, phone number, and age. Remember that name, address, phone number, etc. can be broken out in additional fields. Refer to the tutorial from this week’s Reading Assignment Multiple vs. Single Field Capture for Phone Number Form Input for help with this. Instructions to ensure that the information is displayed back...
Create a program that accepts in a string of 2 or more words. The program then...
Create a program that accepts in a string of 2 or more words. The program then copies the entered string changing the alpha characters into digits representing characters with acsenders, descenders and nonascender/desender characters; uppercase characters should be treated as lower case. The characters with descenders (gjpqy) should be replaced with a 1. The characters with ascenders (dbfhklt) should be replaced with a 2. The rest of the alpha characters should be replaced with a 3. The converted string should...
Create in java an interactive GUI application program that will prompt the user to use one...
Create in java an interactive GUI application program that will prompt the user to use one of three calculators, label project name MEDCALC. You can use any color you want for your background other than grey. Be sure to label main java box with “MEDCALC” and include text of “For use only by students” On all three calculators create an alert and signature area for each user. The alert should be something like “All calculations must be confirmed by user...
in java code Modify your program as follows: Ask the user for the number of hours...
in java code Modify your program as follows: Ask the user for the number of hours worked in a week and the number of dependents as input, and then print out the same information as in the initial payroll assignment. Perform input validation to make sure the numbers entered by the user are reasonable (non-negative, not unusually large, etc). Let the calculations repeat for several employees until the user wishes to quit the program. Remember: Use variables or named constants...
Your code needs to do the following: Create a function called pigLatin that accepts a string...
Your code needs to do the following: Create a function called pigLatin that accepts a string of English words in the parameter sentence and returns a string of those words translated into Pig Latin. English is translated to Pig Latin by taking the first letter of every word, moving it to the end of the word and adding ‘ay’. For example the sentence “The quick brown fox” becomes “hetay uickqay rownbay oxfay”. You may assume the words in the parameter...
Modify the provided code to create a program that calculates the amount of change given to...
Modify the provided code to create a program that calculates the amount of change given to a customer based on their total. The program prompts the user to enter an item choice, quantity, and payment amount. Use three functions: • bool isValidChoice(char) – Takes the user choice as an argument, and returns true if it is a valid selection. Otherwise it returns false. • float calcTotal(int, float) – Takes the item cost and the quantity as arguments. Calculates the subtotal,...
Create a program with the exception example code from Hour14Exceptions.java the Hour14Exceptions shows examples of string...
Create a program with the exception example code from Hour14Exceptions.java the Hour14Exceptions shows examples of string and number exception handling, as well as a user defined exception CREATE A NEW PROGRAM that demonstrates Any 2 of the following 3 choices: 1 numeric and 1 string exception make the program require 1 input argument and throw an exception if there are no command line arguments passed in. In the exception, inform the user that an input argument is required. (You can...
Using C++ Create a program that asks the user to input a string value and then...
Using C++ Create a program that asks the user to input a string value and then outputs the string in the Pig Latin form. - If the string begins with a vowel, add the string "-way" at the end of the string. For “eye”, it will be “eye-way”. - If the string does not begin with a vowel, first add "-" at the end of the string. Then rotate the string one character at a time; that is, move the...
Instructions: Create a Java program that reads a string entered by the user and then determines...
Instructions: Create a Java program that reads a string entered by the user and then determines and prints how many of each lowercase vowel (a, e, i, o, and u) appear in the entire string. Have a separate counter for each vowel. Also, count and print the number of non-vowel characters. Example: User enters: "This house is beautiful." a: 1 e: 2 i: 3 o: 1 u: 2 non-vowel:10
Using tkinter, create a GUI interface which accepts input of annual income. Using the table below,...
Using tkinter, create a GUI interface which accepts input of annual income. Using the table below, determine and output the income tax for that income. Tax Rate Income 10% $0 to $9,875.00;  12% $9,876.01 to $40,125.00; 22% $40,126 to $85,525.00; 24% $85,526.01 to $163,300.00. Test with the following income data: 163,300.00 9,876.01 85,525.00
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT