import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class ButtonTest1 extends JFrame{
private JButton aB,bB,exitButton,qrButton;
private JLabel mz,fs;
private JTextField mz1,fs1;
public String mzsz[]=new String[20];
public int fssz[]=new int[20];
public ButtonTest1(){
super("testing buttons");
Container container=getContentPane();
container.setLayout(new FlowLayout());
aB=new JButton("输入学生姓名和分数");
container.add(aB);
//ButtonHandler handler=new ButtonHandler();
bB=new JButton("退出");
container.add(bB);
ButtonHandler handler=new ButtonHandler();
aB.addActionListener(handler);
bB.addActionListener(handler);
setSize(800,256);
setVisible(true);
}
public static void main(String args[]){
ButtonTest1 application=new ButtonTest1();
application.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
void TimeTestWindow(){
JFrame frame=new JFrame("名字和分数的输入");
Container container=frame.getContentPane();
container.setLayout(new FlowLayout());
mz=new JLabel("名字:");
mz1=new JTextField(10);
container.add(mz);
container.add(mz1);
fs=new JLabel("分数:");
fs1=new JTextField(10);
container.add(fs);
container.add(fs1);
qrButton=new JButton("确认 ");
container.add(qrButton);
exitButton=new JButton("直接退出");
container.add(exitButton);
//setSize(800,256);
//setVisible(true);
frame.setSize(800,256); //修改处
frame.setVisible(true); //修改处
ButtonHandler handler=new ButtonHandler();
mz1.addActionListener(handler);
fs1.addActionListener(handler);
exitButton.addActionListener(handler);
qrButton.addActionListener(handler);
}
private class ButtonHandler implements ActionListener{
public void actionPerformed(ActionEvent event){
int i=0;
if(event.getSource()==bB)
System.exit(0);
else
if(event.getSource()==exitButton)
System.exit(0);
else
if(event.getSource()==qrButton)
//if(event.getSource()==mz1)
mzsz[i]=event.getActionCommand();
else if(event.getSource()==fs1)
{ fssz[i]=Integer.parseInt(event.getActionCommand());
i++ ;
//System.out.println(mzsz[i]+fssz[i]);
JOptionPane.showMessageDialog(null,mzsz[i]+fssz[i],"测试结果",JOptionPane.INFORMATION_MESSAGE);}
else
TimeTestWindow();
//JOptionPane.showMessageDialog(ButtonTest1.this,"you pressed:"+event.getActionCommand());
}
}
}
评论