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

60.1K

Verified Solution

Question

Programming

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


N.B. most of the modification needs to occur in the constructor andthe actionPerformed() methods. So you should spend time working outexactly what these two methods are doing, so that you can make thenecessary 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 repeatedcharacters\");
}
}
// 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 ornot
if(s.indexOf(ch) != s.lastIndexOf(ch)) {   
return true;
}
}
// no character repetition return false
return false;
}

CODE TO MODIFY THE GUI: (Please modify thisone)

//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!


}

Answer & Explanation Solved by verified expert
4.3 Ratings (709 Votes)
Codeimport javaxswing for JFrame JButton JLabelimport javaawt for Container BorderLayoutimport javaawtevent for WindowAdapterActionListnerActionEventpublic class SimplApp extends JFrame define windows width and height in pixelsprivate static final int WIDTH 400private static final int HEIGHT 200 used for displaying text in the windowprivate JLabel infoLabelprivate JTextField inputTextprivate class ButtonAction implementsActionListener public void    See Answer
Get Answers to Unlimited Questions

Join us to gain access to millions of questions and expert answers. Enjoy exclusive benefits tailored just for you!

Membership Benefits:
  • Unlimited Question Access with detailed Answers
  • Zin AI - 3 Million Words
  • 10 Dall-E 3 Images
  • 20 Plot Generations
  • Conversation with Dialogue Memory
  • No Ads, Ever!
  • Access to Our Best AI Platform: Flex AI - Your personal assistant for all your inquiries!
Become a Member

Other questions asked by students