第七次上机

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.GridLayout;

import javax.swing.*;

public class keytef {
String[] k=new String[]{"7","8","9","/","4","5","6","*","1","2","3","-","0",".","=","+"};
JFrame j;
JPanel P1,p2;
JTextField t;
JButton[] b=new JButton[16];
public keytef(){
j=new JFrame("计算器");
t=new JTextField(100);
p2=new JPanel(new GridLayout(4,4));
p2.setBackground(Color.red);
for(int i=0;i<16;i++){
b[i]=new JButton(k[i]);
p2.add(b[i]);
}
j.add(t,BorderLayout.NORTH);
j.add(p2);
j.setSize(300, 200);
j.setLocation(300,400);
j.setVisible(true);
}

public static void main(String[] args) {
    new keytef();

}

}

import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;

public class keysst implements ActionListener{
JFrame j;
JButton b1,b2,b3,b4;
JPanel p;
public keysst(){
j=new JFrame();
p=new JPanel();
b1=new JButton("红色");
b1.addActionListener(new bread1());
b2=new JButton("绿色");
b2.addActionListener(new bread2());
b3=new JButton("蓝色");
b3.addActionListener(new bread3());
b4=new JButton("灰色");
b4.addActionListener(this);
p.setBackground(Color.lightGray);
p.add(b1);
p.add(b2);
p.add(b3);
p.add(b4);
j.add(p);
j.setSize(400, 300);
j.setLocation(200, 300);
j.setVisible(true);
}

public static void main(String[] args) {
    new keysst();

}

public class bread1 implements ActionListener{
@Override
public void actionPerformed(ActionEvent e) {
    p.setBackground(Color.red);
    
}
}

public class bread2 implements ActionListener{
@Override
public void actionPerformed(ActionEvent e) {
    p.setBackground(Color.green);
    
}
}

public class bread3 implements ActionListener{
@Override
public void actionPerformed(ActionEvent e) {
    p.setBackground(Color.blue);
    
}
}

@Override
public void actionPerformed(ActionEvent e) {
    p.setBackground(Color.lightGray);
    
}

}

猜你喜欢

转载自www.cnblogs.com/javalv/p/10925630.html