Java第七次上机(窗口)

1.计算器

代码

 1 package Person;
 2 import javax.swing.*;
 3 import java.awt.*;
 4 import java.awt.event.ActionEvent;
 5 import java.awt.event.ActionListener;
 6 
 7 public class Calculator {
 8         JFrame f;
 9         JPanel p;
10         JButton [] b;
11         String[] title = {"7","8","9","/",
12                         "4","5","6","*",
13                         "1","2","3","-",
14                         "0","c","=","+"};
15         JTextField t;
16 
17     public Calculator(){
18         f = new JFrame("计算器");
19         p = new JPanel();
20         t = new JTextField();
21         b = new JButton[16];
22         
23         f.setVisible(true);
24         f.setLocationRelativeTo(null); 
25         f.setSize(450,450);
26         f.add(t,BorderLayout.NORTH);
27         t.setPreferredSize(new Dimension(15,50));        //设置文本框长、宽
28         p.setLayout(new GridLayout(4,4));
29         f.add(p,BorderLayout.CENTER);
30         for(int i=0;i<16;i++){
31             b[i] = new JButton(title[i]);
32             p.add(b[i]);
33         }
34         
35 }
36     public static void main(String args[]){
37         new Calculator();
38     }
39 }

运行界面

 

 

2.变色按钮

 

代码①

 1 package smy.window;
 2 import java.awt.*;
 3 import java.awt.event.ActionEvent;
 4 import java.awt.event.ActionListener;
 5 import javax.swing.*;
 6 
 7 public class Changecolor1 implements ActionListener{
 8     JFrame f;
 9     JPanel[] p;
10     JButton[] b;
11     public Changecolor1(){
12         f = new JFrame("变色按钮");
13         p = new JPanel[2];
14         for(int i=0; i<2; i++){
15             p[i] = new JPanel();
16         }
17         
18         b = new JButton[3];
19         b[0] = new JButton("红色");
20         b[1] = new JButton("绿色");
21         b[2] = new JButton("蓝色");
22         for(int i=0; i<3; i++){
23             p[0].add(b[i]);
24         }
25         f.add("North", p[0]);
26         f.add("Center", p[1]);
27         
28         f.setVisible(true);
29         f.setLocationRelativeTo(null); 
30         f.setSize(400,300);
31         
32         b[0].addActionListener( this);
33         b[1].addActionListener( this);
34         b[2].addActionListener( this);
35 
36     }
37     public static void main(String[] args) {
38         new Changecolor1();
39     }
40     public void actionPerformed(ActionEvent event) {
41         Object source=event.getSource();
42         if(source==b[0]) {
43             p[1].setBackground(Color.RED);
44         }else if(source==b[1]) {
45             p[1].setBackground(Color.GREEN);
46         }else if(source==b[2]){
47             p[1].setBackground(Color.BLUE);
48         }
49     }
50 }

代码②

 1 package Person;
 2 import java.awt.*;
 3 import java.awt.event.ActionEvent;
 4 import java.awt.event.ActionListener;
 5 
 6 import javax.swing.*;
 7 
 8 public class Changecolor{
 9     JFrame f;
10     JPanel[] p;
11     JButton[] b;
12     
13     public Changecolor(){
14         f = new JFrame();
15         p = new JPanel[2];
16         for(int i=0; i<2; i++){
17             p[i] = new JPanel();
18         }
19         
20         b = new JButton[3];
21         b[0] = new JButton("红色");
22         b[1] = new JButton("绿色");
23         b[2] = new JButton("蓝色");
24         for(int i=0; i<3; i++){
25             p[0].add(b[i]);
26         }
27         f.add("North", p[0]);
28         f.add("Center", p[1]);
29         
30         f.setVisible(true);
31         f.setLocationRelativeTo(null); 
32         f.setSize(400,300);
33         
34         b[0].addActionListener(new ActionListener(){
35             public void actionPerformed(ActionEvent e){
36                  p[1].setBackground(Color.RED);
37              }});
38          
39          b[1].addActionListener(new ActionListener(){
40              public void actionPerformed(ActionEvent e){
41                  p[1].setBackground(Color.GREEN);
42              }});
43          
44          b[2].addActionListener(new ActionListener(){
45              public void actionPerformed(ActionEvent e){
46                  p[1].setBackground(Color.BLUE);
47              }});       
48     }
49     public static void main(String[] args) {
50         new Changecolor();
51     }
52     
53 }

运行界面

 


今日小结:不会做监听 不知道在干啥 跟着系统自动改错 似懂非懂

 

posted @ 2019-05-26 23:12  Unicodee  阅读(130)  评论(0编辑  收藏  举报