Java窗体的应用

计算机的界面

import java.awt.*;
import javax.swing.*;

public class ComputerDemo extends JFrame {
    JFrame f;
    JTextField t;
    JPanel p;
    GridLayout gl;
    JButton[] b;
    String[] title = {"7","8","9","/",
                      "4","5","6","*",
                      "1","2","3","-",
                      "0",".","=","+"};
    public ComputerDemo() {
        f = new JFrame();
        t = new JTextField();
        p = new JPanel();
        p.setBackground(Color.cyan );
        gl = new GridLayout(4,4,5,5);
        p.setLayout(gl);
        b = new JButton[16];
        for(int i= 0;i<16;i++) {
            b[i] = new JButton(title[i]);
            p.add(b[i]);
        }
        f.add(t,BorderLayout.NORTH);
        f.add(p,BorderLayout.CENTER);
        f.setSize(400, 400);
        f.setVisible(true);
    }
    
    public static void main(String[] args) {
        // TODO 自动生成的方法存根
        new ComputerDemo();
    }

}

窗体改变颜色

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class Colcor implements ActionListener {
    JFrame f;
    JPanel p;
    JButton b1;
    JButton b2;
    JButton b3;
    public Colcor() {
        f = new JFrame();
        p = new JPanel();
        b1 = new JButton("红色");
        b1.addActionListener(this);
        b2 = new JButton("蓝色");
        b2.addActionListener(this);
        b3 = new JButton("绿色");
        b3.addActionListener(this);
        f.add(p);
        p.add(b1);
        p.add(b2);
        p.add(b3);
        f.setSize(400, 400);
        f.setVisible(true);
    }
    
    public static void main(String[] args) {
        // TODO 自动生成的方法存根
        new Colcor();
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        // TODO 自动生成的方法存根
        if(e.getSource()==b1) {
            p.setBackground(Color.red);
        }else if(e.getSource()==b2) {
            p.setBackground(Color.blue);
        }else {
            p.setBackground(Color.green);
        }
        
        
        
    }
}

 

posted @ 2019-05-26 22:26  吕志琪  阅读(358)  评论(0编辑  收藏  举报