import javax.swing.*; import java.awt.*; import java.awt.event.*; public class JFrameJButtonJLabelJTextField2 implements ActionListener, KeyListener { public static void main(String[] args) { // Create a new instance of a JFrame; note that because the 'JFrame' class // is not being extended, it IS necessary to create a 'JFrame' object here; // note, also, that since a constructor is not being used, it is necessary // to give the 'JFrame' object a name (instead of just creating a JFrame // with "new JFrame()") and use that name throughout the 'main' method JFrame window = new JFrame(); // Allow objects to be placed anywhere on the JFrame window.setLayout(null); // Don't let the JFrame be resized window.setResizable(false); // Allow the JFrame to be closed via the "X"; note that because the 'JFrame' // class has not been extended, the "JFrame" prefix must be used here window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // Set the size, title, and background color of the JFrame window.setSize(400, 200); window.setTitle("Demo #2: JFrame, JButton, JLabel, JTextField"); window.getContentPane().setBackground(Color.BLACK); // Position the JFrame in the center of the screen window.setLocationRelativeTo(null); //=============================================================================// // Create and set up a JButton JButton button1 = new JButton("I am a Button!"); button1.setFont(new Font("Arial", Font.BOLD, 20)); button1.setBackground(Color.GREEN); button1.setForeground(Color.BLUE); button1.setSize(180, 30); button1.setLocation(110, 15); // Don't let the button be pressed via ENTER or SPACE button1.setFocusable(false); // Allow this JButton to respond to mouse clicks button1.addActionListener(new JFrameJButtonJLabelJTextField2()); // Make the JButton visible and add it to the JFrame button1.setVisible(true); window.add(button1); //=============================================================================// // Create and set up a JLabel JLabel label1 = new JLabel("This is a Label."); label1.setSize(180, 30); label1.setLocation(110, 75); label1.setFont(new Font("Arial", Font.BOLD, 24)); label1.setOpaque(true); label1.setBackground(Color.BLACK); label1.setForeground(Color.YELLOW); // Make the JLabel visible and add it to the JFrame label1.setVisible(true); window.add(label1); //=============================================================================// // Create and set up a JTextField JTextField text1 = new JTextField(20); text1.setSize(180, 25); text1.setLocation(110, 135); text1.setFont(new Font("Arial", Font.BOLD, 16)); text1.setBackground(Color.WHITE); text1.setForeground(Color.BLACK); text1.setHorizontalAlignment(JTextField.LEFT); // Allow this JTextField to respond to keypresses text1.addKeyListener(new JFrameJButtonJLabelJTextField2()); // Make the JTextField visible and add it to the JFrame text1.setVisible(true); window.add(text1); //=============================================================================// // Make the JFrame (and everything on it) visible; note that this line // is executed AFTER the above objects have been added to the JFrame // to ensure that all of the above added objects appear on the JFrame window.setVisible(true); } //================================================================================// // This method is called whenever the JButton is clicked public void actionPerformed(ActionEvent event) { System.out.println("The \"" + event.getActionCommand() + "\" button was clicked!"); } //================================================================================// // Since 'KeyListener' is implemented, this method must exist to avoid errors public void keyPressed(KeyEvent event) { } // Since 'KeyListener' is implemented, this method must exist to avoid errors public void keyReleased(KeyEvent event) { } // This method is called whenever a key is pressed in the JTextField public void keyTyped(KeyEvent event) { System.out.println("You pressed the \"" + event.getKeyChar() + "\" key."); } }