Write a simple JAVA GUI program. The GUI interface needs to contain 1 JLabel and
ID: 3538730 • Letter: W
Question
Write a simple JAVA GUI program. The GUI interface needs to contain 1 JLabel and 4 JButtons. The functionality of the buttons is discussed below. The GUI components need to be contained within a JFrame. Additionally a JPanel should be used to help control the layout of the buttons. Use a GridLayout for the layout manager. The buttons should be displayed in a 2 by 2 grid. Here is an approximate picture for the layout for the components:
Hello World
Uppercase Lowercase
New Phrase Reset
Functionality: Each of the buttons needs to perform a different function.
Uppercase: should display the string of the label in all uppercase letters, use a String method for this - do not hardcode a String in all upper case.
Lowercase: should display the String in the label in all lowercase letters, use a String method for this - do not hardcode a String in all lower case.
New Phrase: should get input from the user via a dialog box. Then place this input into the label. The case buttons will need to be able to alter this string!
Reset: should restore the contents of the label to (exact case): %u201CHello World%u201D
Misc. Notes:
At launch the label of the text should read %u201CHello World%u201D
Make sure to make your jar file executable. This is a GUI program. Assuming everything has been done correctly, when you double click on the jar file%u2019s icon the program should launch!
Explanation / Answer
import java.awt.Container;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
public class DemoPanel extends JFrame implements ActionListener{
JLabel content = null;
public DemoPanel(){
setSize(400,300);
setVisible(true);
setDefaultCloseOperation(EXIT_ON_CLOSE);
}
public static void main(String args[]){
DemoPanel p = new DemoPanel();
p.buildPanel();
}
public void buildPanel(){
Container c = getContentPane();
c.setLayout(new GridLayout(2, 1));
JPanel labelPanel = new JPanel();
labelPanel.setLayout(new FlowLayout(FlowLayout.CENTER));
content = new JLabel("Hello World");
labelPanel.add(content);
c.add(labelPanel);
JPanel buttonPanel = new JPanel();
buttonPanel.setLayout(new FlowLayout(FlowLayout.CENTER));
JButton uppercase = new JButton("Uppercase");
uppercase.addActionListener(this);
buttonPanel.add(uppercase);
JButton lowercase = new JButton("Lowercase");
lowercase.addActionListener(this);
buttonPanel.add(lowercase);
JButton newPhrase = new JButton("New Phrase");
newPhrase.addActionListener(this);
buttonPanel.add(newPhrase);
JButton reset = new JButton("Reset");
reset.addActionListener(this);
buttonPanel.add(reset);
c.add(buttonPanel);
}
@Override
public void actionPerformed(ActionEvent e) {
String command = e.getActionCommand();
if(command.equals("Lowercase")){
content.setText(content.getText().toLowerCase());
}else if(command.equals("Uppercase")){
content.setText(content.getText().toUpperCase());
}else if(command.equals("New Phrase")){
while(true){
String newPhrase = JOptionPane.showInputDialog("Enter New Phrase");
if(newPhrase == null || newPhrase.equals("")){
JOptionPane.showMessageDialog(this, "Invalid Input. Please try again.", "Error", JOptionPane.ERROR_MESSAGE);
continue;
}
content.setText(newPhrase);
break;
}
}else if(command.equals("Reset")){
content.setText("Hello World");
}
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.