⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 firstswingapplication.java

📁 还不错的java基本实例
💻 JAVA
字号:

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class FirstSwingApplication extends JFrame{
    private JTextField text1;
    private JButton btnShow;
    private JButton btnClear;

    public FirstSwingApplication() {
        createComponents();
        registerEventHandlers();
        layoutComponents();

        //this.setSize(300,200);
        this.setTitle("第一个图形界面程序");
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        this.pack();
      
    }

     private void createComponents() {
         // text1
               text1 = new JTextField(10);
              // text1.setHorizontalAlignment();

               btnShow = new JButton("显示");

              btnClear = new JButton("清除");
     }

     private void registerEventHandlers() {

          btnShowActionEventHander hander1=new btnShowActionEventHander();
          btnShow.addActionListener(hander1);
         
          btnClearActionEventHander hander2=new btnClearActionEventHander();
          btnClear.addActionListener(hander2);
     }

     private void layoutComponents() {
        Container c=this.getContentPane();
        c.setLayout(new GridLayout(2, 1)); 
        JPanel pane1 = new JPanel(new FlowLayout());
        pane1.add(text1);

        JPanel pane2 = new JPanel(new FlowLayout());
        pane2.add( btnShow);
        pane2.add( btnClear);

      // this.setBorder(BorderFactory.createEmptyBorder(20, 20, 20, 20));
        c.add(pane1);
        c.add(pane2);
    }

      private class btnShowActionEventHander implements ActionListener{
        public void actionPerformed(ActionEvent e) {
             text1.setText("Hello, World");
        }
    }

     private class btnClearActionEventHander implements ActionListener{
        public void actionPerformed(ActionEvent e) {
             text1.setText("");
        }
    }

    public static void main(String[] args) {
        JFrame.setDefaultLookAndFeelDecorated(true);
        FirstSwingApplication frame=new FirstSwingApplication();
        frame.setVisible(true);
      //  frame.show();
       }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -