窗口实训1
1、设计一个如图所示的界面,不需要提供组件的功能。
package ck;
import java.awt.*;
import javax.swing.*;
public class jsuanji {
JFrame f;
JPanel p;
JTextField t;
JButton b,b1,b2,b3,b4,b5,b6,b7,b8,b9,bi,bj,bk,bl,bm,bn;
GridLayout g;
public jsuanji(){
f=new JFrame(" ");
p=new JPanel();
t=new JTextField();
g=new GridLayout(4,4);
b=new JButton("7"); b1=new JButton("8");b2=new JButton("9");b3=new JButton("/");
b4=new JButton("4");b5=new JButton("5");b6=new JButton("6");b7=new JButton("*");
b8=new JButton("1");b9=new JButton("2");bi=new JButton("3");bj=new JButton("-");
bk=new JButton("0");bl=new JButton(".");bm=new JButton("=");bn=new JButton("+");
p.add(b);p.add(b1);p.add(b2);p.add(b3);p.add(b4);p.add(b5);p.add(b6);p.add(b7);
p.add(b8);p.add(b9);p.add(bi);p.add(bj);p.add(bk);p.add(bl);p.add(bm);p.add(bn);
f.add(t,BorderLayout.NORTH);
f.add(p,BorderLayout.CENTER);
p.setLayout(g);
f.add(p);
f.setSize(400,300);
f.setVisible(true);
}
public static void main(String[] args) {
new jsuanji();
}
}
2、编写可改变背景颜色的窗口
package practice;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class colorb implements ActionListener {
JFrame jf;
JPanel jl, jl1;
JButton jb,jb1,jb2;
GridLayout g;
public colorb (){
jf= new JFrame("color");
jl=new JPanel();
jl1=new JPanel();
jb=new JButton("red");
jb.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e){
jl1.setBackground(Color.red);
}
});
jb.addActionListener(this);
jb1=new JButton("green");
jb1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e){
jl1.setBackground(Color.green);
}
});
jb2=new JButton("blue");
jb2.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e){
jl1.setBackground(Color.blue);
}
});
jf.add(jl);jl.add(jb);jl.add(jb1);jl.add(jb2);
jf.add(jl1);
jf.add(jl,BorderLayout.NORTH);
jf.add(jl1,BorderLayout.CENTER);
jf.setVisible(true);
jf.setSize(400,400);
}
public static void main(String[] args) {
new colorb();
}
public void actionPerformed(ActionEvent e) {
}
}