窗口实训

[亡魂溺海] 提交于 2020-04-07 07:24:38

1.计算器
package Demo;
import java.awt.BorderLayout;
import java.awt.GridLayout;
import javax.swing.;
public class Calucate {
JFrame f;
JPanel p;
JTextField t;
JButton b[]=new JButton[16];
GridLayout g1;
public Calucate(){
f = new JFrame ("计算器");
g1= new GridLayout(4,4);
p = new JPanel();
p.setLayout(g1);
t = new JTextField();
b[0] = new JButton("7"); b[1] = new JButton("8"); b[2] = new JButton("9"); b[3] = new JButton("/");
b[4] = new JButton("4"); b[5] = new JButton("5"); b[6] = new JButton("6"); b[7] = new JButton("");
b[8] = new JButton("1"); b[9] = new JButton("2"); b[10] = new JButton("3"); b[11] = new JButton("-");
b[12] = new JButton("0"); b[13] = new JButton("."); b[14] = new JButton("="); b[15] = new JButton("+");
for(int i=0;i<16;i++){
p.add(b[i]);
}
f.add(t,BorderLayout.NORTH);
f.add(p,BorderLayout.CENTER);
f.setSize(400,200);
f.setVisible(true);
}
public static void main(String[] args){
new Calucate();
}
}

2.窗口颜色
package A;
import java.awt.Color;
import java.awt.GridLayout;
import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.*;
public class heiheiheihei implements ActionListener {
JFrame f;
JPanel p1,p2;
JButton b1,b2,b3;
GridLayout g1;
public heiheiheihei(){
f = new JFrame();
p1 = new JPanel();
p2 = new JPanel();
b1 = new JButton("红色");
b1.addActionListener(this);
b2 = new JButton("绿色");
b2.addActionListener(this);
b3 = new JButton("蓝色");
b3.addActionListener(this);
p1.add(b1);
p1.add(b2);
p1.add(b3);
p2.setBackground(Color.WHITE);
f.add(p1,BorderLayout.NORTH);
f.add(p2,BorderLayout.CENTER);
f.setSize(400,400);
f.setVisible(true);

}
public static void main(String []args){
	new heiheiheihei();
}
public void actionPerformed(ActionEvent e){
	if(e.getSource()==b1)
		p2.setBackground(Color.RED);
	else if(e.getSource()==b2)
		p2.setBackground(Color.GREEN);
	else
		p2.setBackground(Color.BLUE);
}

}

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!