- import javax.swing.*;
- import java.awt.event.ActionEvent;
- import java.awt.event.ActionListener;
- public class Lab09_FirstGUI {
- private static JFrame jf;
- private static JPanel mp;
- private static JLabel helloLabel;
- private static JButton b;
- private static JTextField textField;
- private static ActionListener bClick = new ActionListener() {
- @Override
- public void actionPerformed(ActionEvent e) {
- String name = textField.getText();
- if (name.equals("money")){
- ImageIcon icon2 = new ImageIcon("icons\\Cash-icon.png");
- helloLabel.setSize(icon2.getIconWidth(), icon2.getIconHeight());
- helloLabel.setIcon(icon2);
- } else if (name.equals("car")) {
- ImageIcon icon2 = new ImageIcon("icons\\Car.png");
- helloLabel.setSize(icon2.getIconWidth(), icon2.getIconHeight());
- helloLabel.setIcon(icon2);
- } else {
- helloLabel.setIcon(null);
- helloLabel.setSize(250,30);
- helloLabel.setText("Hello " + name);
- }
- }
- };
- public static void main(String[] args) {
- try {
- UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
- //UIManager.setLookAndFeel("com.sun.java.swing.plaf.metal.MetalLookAndFeel");
- //UIManager.setLookAndFeel("com.sun.java.swing.plaf.mac.MacLookAndFeel");
- //UIManager.setLookAndFeel("com.sun.java.swing.plaf.motif.MotifLookAndFeel");
- }
- catch(Exception e){}
- //
- jf = new JFrame();
- jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
- jf.setSize(800, 600);
- jf.setResizable(true);
- jf.setLocationRelativeTo(null);
- jf.setTitle("My first GUI");
- //
- ImageIcon icon = new ImageIcon("icons\\FirstGUI.png");
- jf.setIconImage(icon.getImage());
- //
- mp = new JPanel();
- jf.setContentPane(mp);
- mp.setLayout(null);
- //
- helloLabel = new JLabel();
- helloLabel.setText("Hello my dear friend");
- helloLabel.setLocation(20,140);
- helloLabel.setSize(200,50);
- mp.add(helloLabel);
- //
- b = new JButton();
- b.setText("Action");
- b.setLocation(20,80);
- b.setSize(70,30);
- b.addActionListener(bClick);
- mp.add(b);
- //
- textField = new JTextField();
- textField.setText("Enter your name here");
- textField.setLocation(20, 20);
- textField.setSize(200, 30);
- mp.add(textField);
- jf.setVisible(true);
- }
- }
[text] code
Viewer
*** This page was generated with the meta tag "noindex, nofollow". This happened because you selected this option before saving or the system detected it as spam. This means that this page will never get into the search engines and the search bot will not crawl it. There is nothing to worry about, you can still share it with anyone.
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:
Latest Code Pastes