In: Computer Science
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!
}
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.