import javax.swing.*; import javax.swing.border.*; import java.awt.*; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.event.KeyEvent; import java.awt.event.KeyListener; public class JFrameJButtonJLabelJTextField1 extends JFrame implements ActionListener, KeyListener { private static final int WIDTH = 500, HEIGHT = 250; private static final Font STANDARD_FONT = new Font("Arial", Font.BOLD, 24); private static final Color DARKGREEN = new Color(0, 140, 0); // Objects that need to be referenced from outside the constructor // must be defined here (so that they are global to the class) private JButton button2; private JLabel label2, label3, label4; private JTextField textInput; public static void main(String[] args) { new JFrameJButtonJLabelJTextField1(); } // Constructor public JFrameJButtonJLabelJTextField1() { // Note that it is NOT necessary to explicitly create a 'JFrame' object // here, since extending the 'JFrame' class does that automatically // Allow objects to be placed anywhere on the JFrame setLayout(null); // Allow the JFrame to be closed via the "X" setDefaultCloseOperation(EXIT_ON_CLOSE); // Set the JFrame size, title, and background color setSize(WIDTH, HEIGHT); setTitle("Demo #1: JFrame & JButtons & JLabels & JTextField"); getContentPane().setBackground(Color.BLACK); // Position the JFrame in the center of the screen setLocationRelativeTo(null); // Don't let the JFrame be resized setResizable(false); // Create and set up a JLabel JLabel label1 = new JLabel(); label1.setSize(250, 30); label1.setLocation(10, 10); label1.setFont(STANDARD_FONT); label1.setText("Click this button --->"); label1.setOpaque(true); label1.setBackground(Color.BLACK); label1.setForeground(Color.WHITE); label1.setVisible(true); // Create and set up a JButton JButton button1 = new JButton("Click Me!"); button1.setFont(new Font("Arial", Font.BOLD, 28)); button1.setForeground(Color.BLUE); button1.setSize(160, 30); button1.setLocation(260, 10); button1.setFocusable(false); // Don't let the button be pressed via ENTER or SPACE button1.setVisible(true); // Create and set up a second JLabel label2 = new JLabel(); label2.setSize(180, 30); label2.setLocation(310, 70); label2.setFont(new Font("Arial", Font.BOLD, 14)); label2.setText("<--- Now Type Something"); label2.setOpaque(true); label2.setBackground(Color.BLACK); label2.setForeground(Color.RED); label2.setVisible(false); // Make this label initially hidden // Create and set up a JTextField textInput = new JTextField(20); textInput.setSize(150, 25); textInput.setLocation(145, 70); textInput.setFont(new Font("Arial", Font.BOLD, 16)); textInput.setBackground(Color.WHITE); textInput.setForeground(Color.BLACK); textInput.setHorizontalAlignment(JTextField.LEFT); textInput.setVisible(false); // Make this textfield initially hidden textInput.addKeyListener(this); // Allow this textfield to respond to keypresses // Create and set up a second JButton button2 = new JButton("Click Here When Done Typing"); button2.setFont(new Font("Arial", Font.BOLD, 15)); button2.setForeground(DARKGREEN); button2.setSize(250, 30); button2.setLocation(10, 125); button2.setFocusable(false); // Don't let the button be pressed via ENTER or SPACE button2.setVisible(false); // Create and set up a third JLabel label3 = new JLabel(); label3.setSize(130, 30); label3.setLocation(10, 186); label3.setFont(STANDARD_FONT); label3.setText("You Typed:"); label3.setOpaque(true); label3.setBackground(Color.BLACK); label3.setForeground(Color.ORANGE); label3.setVisible(false); // Make this label initially hidden // Create and set up a fourth JLabel label4 = new JLabel(); label4.setSize(160, 25); label4.setLocation(155, 190); label4.setFont(new Font("Arial", Font.PLAIN, 12)); label4.setHorizontalAlignment(JLabel.CENTER); label4.setText("blah blah"); label4.setOpaque(true); label4.setBorder(LineBorder.createGrayLineBorder()); label4.setBackground(Color.WHITE); label4.setForeground(Color.BLACK); label4.setVisible(false); // Make this label initially hidden // Create and set up a third JButton JButton button3 = new JButton("Quit Program"); button3.setFont(new Font("Arial", Font.BOLD, 18)); button3.setForeground(Color.RED); button3.setSize(150, 35); button3.setLocation(334, 180); button3.setFocusable(true); // Allow this button to have the focus button3.setVisible(true); // Allow the buttons to respond to being pressed button1.addActionListener(this); button2.addActionListener(this); button3.addActionListener(this); // Add the JButtons, JLabels, and JTextField to the JFrame add(button1); add(button2); add(button3); add(label1); add(label2); add(label3); add(label4); add(textInput); // Make the JFrame (and everything on it) visible setVisible(true); } public void actionPerformed(ActionEvent e) { // See if the TOP button was clicked if (e.getActionCommand().equals("Click Me!")) { label2.setVisible(true); textInput.setVisible(true); textInput.requestFocus(); // Put the cursor in the textfield } // See if the MIDDLE button was clicked if (e.getActionCommand().equals("Click Here When Done Typing")) { label3.setVisible(true); label4.setText(textInput.getText()); label4.setVisible(true); } // See if the EXIT button was clicked if (e.getActionCommand().equals("Quit Program")) System.exit(0); } // Since 'KeyListener' is implemented, this method must exist to avoid errors public void keyPressed(KeyEvent e) { } public void keyReleased(KeyEvent e) { // Since the 'keyPressed' method receives the keypress before the textfield is // updated, it is necessary to put this code here (in the 'keyReleased' method) if (textInput.getText().length() > 0) button2.setVisible(true); } // Since 'KeyListener' is implemented, this method must exist to avoid errors public void keyTyped(KeyEvent e) { } }