[[Set the layout for the frame’s content >> Create your controls, configure them
ID: 3849714 • Letter: #
Question
[[Set the layout for the frame’s content >> Create your controls, configure them, setup any event listeners, and add them to the frame >> set the frame’s size and default window closing behavior (Can use a main method in the class itself to create an instance and display it, or have a separate “test” class to do that.)]]
Create an application with a GUI containing a JButton which you save in an instance variable. Make your application class implement the ActionListener interface and register itself as an ActionListener for the button. Your actionPerformed handler can simply print the event for now.
just this part if possible>>
Add a JTextField to your GUI. JTextField also uses the ActionListener interface to notify listeners when the user hits Return/Enter in the text field. Adjust your application so that it changes the label to whatever the user has typed when they hit return or enter. Note: JTextField has a constructor that takes a width in characters as parameter.
Explanation / Answer
JAVA CODE
package swing.GUI.example;
import java.awt.BorderLayout;
import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import java.awt.Font;
import javax.swing.JCheckBox;
import javax.swing.ButtonGroup;
import javax.swing.JButton;
import java.awt.Color;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.awt.event.ActionEvent;
import javax.swing.JTextField;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
public class SimpleGUIDemo extends JFrame implements ActionListener, ItemListener, KeyListener {
private JPanel contentPane;
private JButton btn_myBtn;
private JCheckBox chk_box1;
private JCheckBox chk_box2;
private JTextField tf_1;
private JLabel lbl_1;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
SimpleGUIDemo frame = new SimpleGUIDemo();
frame.setResizable(false);
frame.setVisible(true);
}
});
}
/**
* Create the frame.
*/
public SimpleGUIDemo() {
setTitle("MY GUI APP");
setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE); // Changing the default close behaviour
setBounds(100, 100, 489, 300);
contentPane = new JPanel();
contentPane.setBackground(new Color(218, 165, 32));
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(null);
JLabel lblNewLabel = new JLabel("GUI Demo");
lblNewLabel.setForeground(new Color(255, 0, 0));
lblNewLabel.setBackground(new Color(230, 230, 250));
lblNewLabel.setFont(new Font("Tahoma", Font.PLAIN, 14));
lblNewLabel.setBounds(196, 11, 97, 24);
contentPane.add(lblNewLabel);
chk_box1 = new JCheckBox("Check Box 1");
chk_box1.setBounds(62, 146, 97, 23);
contentPane.add(chk_box1);
chk_box2 = new JCheckBox("Check Box 2");
chk_box2.setBounds(333, 146, 97, 23);
contentPane.add(chk_box2);
chk_box1.addItemListener(this);
chk_box2.addItemListener(this);
ButtonGroup bg = new ButtonGroup();
bg.add(chk_box1);
bg.add(chk_box2);
btn_myBtn = new JButton("My Button");
btn_myBtn.setBounds(196, 72, 108, 23);
btn_myBtn.addActionListener(this);
contentPane.add(btn_myBtn);
tf_1 = new JTextField();
tf_1.setBounds(228, 210, 140, 20);
contentPane.add(tf_1);
tf_1.setColumns(10);
lbl_1 = new JLabel("Enter Something here ->");
lbl_1.setBounds(22, 213, 196, 14);
contentPane.add(lbl_1);
contentPane.requestFocusInWindow();
tf_1.addKeyListener(this);
}
@Override
public void actionPerformed(ActionEvent e) {
if(e.getSource() == btn_myBtn)
JOptionPane.showMessageDialog(null, "You Clicked 'My Button' ", "You Clicked me", JOptionPane.INFORMATION_MESSAGE);
}
@Override
public void itemStateChanged(ItemEvent arg0) {
if(arg0.getSource()==chk_box1)
{
if(chk_box1.isSelected())
{
JOptionPane.showMessageDialog(null, "You clicked Check Box1 Current Value: "+arg0.getStateChange());
}
}
if(arg0.getSource()==chk_box2)
{
if(chk_box2.isSelected())
{
JOptionPane.showMessageDialog(null, "You clicked Check Box2 Current Value: "+arg0.getStateChange());
}
}
}
@Override
public void keyPressed(KeyEvent arg0) {
if(arg0.getKeyCode() == KeyEvent.VK_ENTER)
lbl_1.setText(tf_1.getText());
}
@Override
public void keyReleased(KeyEvent arg0) {
}
@Override
public void keyTyped(KeyEvent arg0) {
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.