[text] code

Viewer

  1. import javax.swing.*;
  2. import java.awt.event.ActionEvent;
  3. import java.awt.event.ActionListener;
  4.  
  5. public class Lab09_FirstGUI {
  6.  
  7.     private static JFrame jf;
  8.     private static JPanel mp;
  9.     private static JLabel helloLabel;
  10.     private static JButton b;
  11.     private static JTextField textField;
  12.  
  13.  
  14.     private static ActionListener bClick = new ActionListener() {
  15.         @Override
  16.         public void actionPerformed(ActionEvent e) {
  17.             String name = textField.getText();
  18.             if (name.equals("money")){
  19.                 ImageIcon icon2 = new ImageIcon("icons\\Cash-icon.png");
  20.                 helloLabel.setSize(icon2.getIconWidth(), icon2.getIconHeight());
  21.                 helloLabel.setIcon(icon2);
  22.             } else if (name.equals("car")) {
  23.                 ImageIcon icon2 = new ImageIcon("icons\\Car.png");
  24.                 helloLabel.setSize(icon2.getIconWidth(), icon2.getIconHeight());
  25.                 helloLabel.setIcon(icon2);
  26.             } else {
  27.                 helloLabel.setIcon(null);
  28.                 helloLabel.setSize(250,30);
  29.                 helloLabel.setText("Hello " + name);
  30.             }
  31.         }
  32.     };
  33.  
  34.     public static void main(String[] args) {
  35.         try {
  36.             UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
  37.             //UIManager.setLookAndFeel("com.sun.java.swing.plaf.metal.MetalLookAndFeel");
  38.             //UIManager.setLookAndFeel("com.sun.java.swing.plaf.mac.MacLookAndFeel");
  39.             //UIManager.setLookAndFeel("com.sun.java.swing.plaf.motif.MotifLookAndFeel");
  40.         }
  41.         catch(Exception e){}
  42.  
  43.         //
  44.         jf = new JFrame();
  45.         jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  46.         jf.setSize(800, 600);
  47.         jf.setResizable(true);
  48.         jf.setLocationRelativeTo(null);
  49.         jf.setTitle("My first GUI");
  50.         //
  51.         ImageIcon icon = new ImageIcon("icons\\FirstGUI.png");
  52.         jf.setIconImage(icon.getImage());
  53.         //
  54.         mp = new JPanel();
  55.         jf.setContentPane(mp);
  56.         mp.setLayout(null);
  57.         //
  58.         helloLabel = new JLabel();
  59.         helloLabel.setText("Hello my dear friend");
  60.         helloLabel.setLocation(20,140);
  61.         helloLabel.setSize(200,50);
  62.         mp.add(helloLabel);
  63.         //
  64.         b = new JButton();
  65.         b.setText("Action");
  66.         b.setLocation(20,80);
  67.         b.setSize(70,30);
  68.         b.addActionListener(bClick);
  69.         mp.add(b);
  70.         //
  71.         textField = new JTextField();
  72.         textField.setText("Enter your name here");
  73.         textField.setLocation(20, 20);
  74.         textField.setSize(200, 30);
  75.         mp.add(textField);
  76.  
  77.         jf.setVisible(true);
  78.  
  79.  
  80.     }
  81.  
  82. }
  83.  

Editor

You can edit this paste and save as new:


File Description
  • code
  • Paste Code
  • 25 Feb-2021
  • 2.69 Kb
You can Share it: