/*
学java的一些类和方法要搞清楚的层次和结构
*/
package niminglei;
import java.awt.*;
import java.awt.event.*;
public class Test_3{
public static void main(String[] args)
{
aa a=new aa();
}
}
class aa extends Frame{
Label l1,l2,l3;
Button b1,b2,b3,b4,b5,b6;
TextField tf1,tf2,tf3;
int count1,count2,count3;
Color c;
private class changeText implements ActionListener,TextListener
//设置监听器,实现按钮的功能和文本框的功能
{
public void actionPerformed(ActionEvent e){ //实现按钮的功能
if(e.getSource()==b1){tf1.setText((count1++)+"");}
if(e.getSource()==b2){tf2.setText((count2++)+"");}
if(e.getSource()==b3){tf3.setText((count3++)+"");}
if(e.getSource()==b4){tf1.setText((count1--)+"");}
if(e.getSource()==b5){tf2.setText((count2--)+"");}
if(e.getSource()==b6){tf3.setText((count3--)+"");}
int c1=Integer.parseInt(tf1.getText());
int c2=Integer.parseInt(tf2.getText());
int c3=Integer.parseInt(tf3.getText());
if(c1>=255) {c1=0;count1=0;}
if(c2>=255) {c2=0;count2=0;}
if(c3>=255) {c3=0;count3=0;}
c=new Color(c1,c2,c3);
p1.setBackground(c);
}
//文本框的功能
public void textValueChanged(TextEvent e) {
c=new Color(Integer.parseInt(tf1.getText()),Integer.parseInt(tf2.getText()),Integer.parseInt(tf3.getText()));
p1.setBackground(c);
}
}
Panel p=new Panel(); //设置两个中间容器
Panel p1=new Panel();
GridBagLayout gbl=new GridBagLayout(); //gbl是网格包布局管理器
GridBagConstraints gbc=new GridBagConstraints(); //gbc是网格包约束条件
public aa() //一个构造函数
{
setSize(500, 400);
p.setLayout(gbl);
gbc.fill=GridBagConstraints.BOTH;
this.setLabel(); //设置标签
this.setButton_add(); //设置增加数值的按钮
this.setTxetField(); // 设置文本框
this.setButton_sub(); // 设置减少数值的按钮
this.setPanel(); // 设置中间容器做调色板
add(p); //这个整体放在一个容器里面
setVisible(true); //可见
//窗口的关闭功能
this.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e)
{
System.exit(0);
}
});
}
private void setPanel() {
gbc.weightx=10; // x的权重
gbc.weighty=10; // y的权重
gbc.gridx=0; // x距离左上角的距离
gbc.gridy=4; // y距离左上角的距离
gbc.gridwidth=4; // 横向跨几个网格
//gbc.gridheight=4; 纵向跨几个网格
gbc.insets=new Insets(10,10,10,10);// 组件之间间距
/*
这些都是对约束条件而言的
想要采用它还必须要调用网格包的布局管理器的setConstraints来绑定p1和gbc;
*/
p1.setBackground(new Color(220,220,220));
gbl.setConstraints(p1, gbc);
p.add(p1);
}
private void setButton_sub() {//添加减按钮
b4=new Button("-");
b5=new Button("-");
b6=new Button("-");
b4.setFont(new Font("宋体",Font.PLAIN,20));
b5.setFont(new Font("宋体",Font.PLAIN,20));
b6.setFont(new Font("宋体",Font.PLAIN,20));
gbc.gridx=3;
gbc.gridy=0;
gbl.setConstraints(b4, gbc); //设置网格包布局管理器的约束
gbc.gridy=1;
gbl.setConstraints(b5, gbc);
gbc.gridy=2;
gbl.setConstraints(b6, gbc);
p.add(b4);
p.add(b5);
p.add(b6);
b4.addActionListener(new changeText()); //添加监听器,添加监听器的参数为监听器对象
b5.addActionListener(new changeText());
b6.addActionListener(new changeText());
}
private void setTxetField(){ //添加文本框
tf1=new TextField(5);
tf2=new TextField(5);
tf3=new TextField(5);
tf1.setText("220");
tf2.setText("220");
tf3.setText("220");
count1=Integer.parseInt(tf1.getText()); //用包装类实现把字符串转换成int类型
count2=Integer.parseInt(tf2.getText());
count3=Integer.parseInt(tf3.getText());
gbc.weightx=1;
gbc.weighty=1;
gbc.gridx=2;
gbc.gridy=0;
gbl.setConstraints(tf1, gbc);
gbc.gridy=1;
gbl.setConstraints(tf2, gbc);
gbc.gridy=2;
gbl.setConstraints(tf3, gbc);
p.add(tf1);
p.add(tf2);
p.add(tf3);
tf1.addTextListener(new changeText());//添加文本框的监听器,方法的参数为一个监听器的对象
tf2.addTextListener(new changeText());
tf3.addTextListener(new changeText());
this.setKeyPress(tf1);//把添加监听器写成了一个方法这个方法可以传参数
this.setKeyPress(tf2);// 之前我这个功能实现不了
this.setKeyPress(tf3);//是什么东西传参我搞错了,方法传参而不是类传参。
}
private void setKeyPress(TextField tf)
{
tf.addKeyListener(new KeyAdapter() { //键盘监听器
public void keyPressed(KeyEvent e) //键控的
{
char ch = e.getKeyChar(); //得到键盘上的值
if(!(ch>='0'&&ch<='9'))
{
e.consume();//不是0~9的数字就不读取
}
}
});
}
private void setButton_add() {
b1=new Button("+");
b2=new Button("+");
b3=new Button("+");
b1.setFont(new Font("宋体",Font.PLAIN,20));
b2.setFont(new Font("宋体",Font.PLAIN,20));
b3.setFont(new Font("宋体",Font.PLAIN,20));
gbc.insets=new Insets(10,80,10,10);
gbc.gridx=1;
gbc.gridy=0;
gbc.weightx=1;
gbc.weighty=1;
gbl.setConstraints(b1, gbc);
gbc.gridy=1;
gbl.setConstraints(b2, gbc);
gbc.gridy=2;
gbl.setConstraints(b3, gbc);
p.add(b1);
p.add(b2);
p.add(b3);
b1.addActionListener(new changeText());
b2.addActionListener(new changeText());
b3.addActionListener(new changeText());
}
private void setLabel()
{
l1=new Label("红色",Label.CENTER);
l2=new Label("绿色",Label.CENTER);
l3=new Label("蓝色",Label.CENTER);
l1.setBackground(Color.RED);
l2.setBackground(Color.GREEN);
l3.setBackground(Color.BLUE);
gbc.gridx=0;
gbc.gridy=0;
gbc.weightx=1;
gbc.weighty=1;
gbc.insets=new Insets(10,10,10,10);
gbl.setConstraints(l1, gbc);
gbc.gridy=1;
gbl.setConstraints(l2, gbc);
gbc.gridy=2;
gbl.setConstraints(l3, gbc);
p.add(l1);
p.add(l2);
p.add(l3);
}
}