import java.awt.Color;
import java.awt.Container;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JTextArea;
public class Frame extends JFrame {
private JButton b[];
private JTextArea show1,show2,show3;
// 构造方法
public Frame(String s) {
// 设置标题栏内容
setTitle(s);
// 设置初始化窗口位置
setBounds(100, 100, 500, 350);
show1 = new JTextArea(1,8);
add(show1);
show2 = new JTextArea(1,8);
add(show2);
show3 = new JTextArea(1,8);
add(show3);
// 设置窗口布局
setLayout(new FlowLayout());
// 创建按钮对象
b=new JButton[14];
FlowLayout flow=new FlowLayout();
flow.setAlignment(FlowLayout.LEFT);
flow.setHgap(2);
flow.setVgap(8);
setLayout(flow);
b[1]=new JButton("X");
b[2]=new JButton("q");
b[3]=new JButton("x");
b[4]=new JButton("0");
b[5]=new JButton("1");
b[6]=new JButton("b");
b[7]=new JButton("o");
b[8]=new JButton("I");
b[9]=new JButton("J");
b[10]=new JButton("j");
b[11]=new JButton("Q");
b[12]=new JButton("O");
b[13]=new JButton("l");
for(int i=1;i<b.length;i++){
// b[i]=new JButton(""+i);
// 把按钮容器添加到JFrame容器上
add(b[i]);
}
// 添加事件监听器对象(面向对象思想)
// b[1].addActionListener(new EventListener1());
// b[12].addActionListener(new EventListener2());
// b[13].addActionListener(new EventListener3());
// for(int i=2;i<=11;i++){
// b[i].addActionListener(new EventListener4());
// }
for(int i=1;i<=13;i++){
b[i].addActionListener(new EventListener1());
}
validate();
// 设置窗口可视化
setVisible(true);
// 设置窗口关闭
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
class EventListener1 implements ActionListener {
@Override
public void actionPerformed(ActionEvent e) {
if(e.getSource()==b[1])
{
show1.setText("Success");
b[1].setBackground(Color.RED);
b[1].setForeground(Color.red);
}
else if(e.getSource()==b[12])
{
show2.setText("Success");
b[12].setBackground(Color.RED);
b[12].setForeground(Color.red);
}
else if(e.getSource()==b[13])
{
show3.setText("Success");
b[13].setBackground(Color.RED);
b[13].setForeground(Color.red);
}
else
{
show1.setText("Fault");
show2.setText("Fault");
show3.setText("Fault");
}
}
}
// class EventListener1 implements ActionListener {
// @Override
// public void actionPerformed(ActionEvent e) {
// show.setText("Success");
// b[1].setBackground(Color.RED);
b[1].setForeground(Color.red);
// }
// class EventListener2 implements ActionListener {
// @Override
// public void actionPerformed(ActionEvent e) {
// show.setText("Success");
// b[12].setBackground(Color.RED);
b[12].setForeground(Color.red);
// }
// }
// class EventListener3 implements ActionListener {
// @Override
// public void actionPerformed(ActionEvent e) {
// show.setText("Success");
// b[13].setBackground(Color.RED);
b[13].setForeground(Color.red);
// }
// }
// class EventListener4 implements ActionListener {
// @Override
// public void actionPerformed(ActionEvent e) {
// show.setText("Fault");
// }
// }
//}
public static void main(String[] args) {
new Frame("验证");
}
}
java的Swing button和textarea的运用
最新推荐文章于 2024-05-26 15:18:36 发布