窗口实训1

1、设计一个如图所示的界面,不需要提供组件的功能。

package Mituzhifan;
import javax.swing.*;
public class yanse {


JFrame jf;
JPanel jp;
JTextField t1;
JButton x1;
JButton x2;
JButton x3;
JButton x4;
JButton x5;
JButton x6;
JButton x7;
JButton x8;
JButton x9;
JButton x10;
JButton x11;
JButton x12;
JButton x13;
JButton x14;
JButton x15;
JButton x16;
public yanse(){
jf = new JFrame("计算器");
jp = new JPanel();
t1 = new JTextField(30);
x1 =new JButton("7");
x2 =new JButton("8");
x3 =new JButton("9");
x4 =new JButton("/");
x5 =new JButton("4");
x6 =new JButton("5");
x7 =new JButton("6");
x8 =new JButton("*");
x9 =new JButton("1");
x10 =new JButton("2");
x11 =new JButton("3");
x12 =new JButton("-");
x13 =new JButton("0");
x14 =new JButton(".");
x15 =new JButton("=");
x16 =new JButton("+");
jp.add(t1);
jp.add(x1);
jp.add(x2);
jp.add(x3);
jp.add(x4);
jp.add(x5);
jp.add(x6);
jp.add(x7);
jp.add(x8);
jp.add(x9);
jp.add(x10);
jp.add(x11);
jp.add(x12);
jp.add(x13);
jp.add(x14);
jp.add(x15);
jp.add(x16);
jf.add(jp);
jf.setVisible(true);
jf.setSize(600,350);
}
public static void main(String[] args){
new yanse();
}

}

 

2、编写可改变背景颜色的窗口。

package Mituzhifan;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
public class jianting {
JFrame jf;
JPanel jp1,jp2;
JButton x1,x2,x3;
public jianting(){
jf=new JFrame();
jp1=new JPanel();
jp2=new JPanel();
x1=new JButton("红色");
x2=new JButton("绿色");
x3=new JButton("蓝色");
jf.add(jp1,BorderLayout.NORTH);
jf.add(jp2,BorderLayout.CENTER);
jp1.add(x1);
jp1.add(x2);
jp1.add(x3);
x1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
jp2.setBackground(Color.red);
}
});
x2.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
jp2.setBackground(Color.green);
}
});
x3.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
jp2.setBackground(Color.blue);
}
});
jf.setLocation(200,250);
jf.setSize(400,200);
jf.setVisible(true);



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

}

}

在本次实训中,因为对监听的不熟悉,因此不知道怎么对第二个下手,逐渐在熟悉了解后,已可以掌握对窗口变颜色等操作,相较之前有很大提升

posted @ 2019-05-29 21:44  肖芳啟  阅读(146)  评论(0编辑  收藏  举报