zoukankan      html  css  js  c++  java
  • 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);
            }
            
            
            
        }
    }
  • 相关阅读:
    Linux 终端常用快捷键
    问题集
    数据库
    mysql数据库知识点
    IntelliJ IDEAj集成开发环境
    Windows最全DOS的CMD命令
    DB2移植到Oracle数据库完整的图解教程
    169.254是什么IP地址 169.254的解决方法(添加局域网地址时)
    解析xml时报错
    童年乐趣
  • 原文地址:https://www.cnblogs.com/lvzhiqi/p/10928048.html
Copyright © 2011-2022 走看看