51,411
社区成员
发帖
与我相关
我的任务
分享import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.FocusEvent;
import java.awt.event.FocusListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.util.regex.Pattern;
import javax.swing.JOptionPane;
public class Test1 extends Frame implements ActionListener,KeyListener,FocusListener{
TextField name = new TextField(10);
TextField capital = new TextField(20);
TextField employees = new TextField(10);
CheckboxGroup chBtnGrp = new CheckboxGroup();
Checkbox organization = new Checkbox("机构产业",false,chBtnGrp),industrial = new Checkbox("信息产业",false,chBtnGrp),
medicine = new Checkbox("医药卫生",false,chBtnGrp),Mechatronics = new Checkbox("机械机电",false,chBtnGrp);
TextField income = new TextField(20);
TextField rate = new TextField(20);
Button btn1 = new Button("确认");
Button btn2 = new Button("取消");
Button btn3 = new Button("退出");
Label l = new Label("企业信息调查表");
Label l1 = new Label("企业名称"),l2 = new Label("注册资金"),l3 = new Label("员工数量"),l4 = new Label("从事行业"),
l5 = new Label("年收入"),l6 = new Label("利润率");
public Test1(String title){
super(title);
this.setSize(600,600);
this.setLayout(null);
l.setBounds(150,50,100,20);
l1.setBounds(20,100,60,20);
name.setBounds(90,100,100,20);
l2.setBounds(210,100,60,20);
capital.setBounds(270,100,100,20);
l3.setBounds(20,150,60,20);
employees.setBounds(90,150,100,20);
l4.setBounds(20,200,60,20);
organization.setBounds(90,200,100,20);
industrial.setBounds(200,200,100,20);
medicine.setBounds(310,200,100,20);
Mechatronics.setBounds(420,200,100,20);
l5.setBounds(210,150,60,20);
income.setBounds(270,150,100,20);
l6.setBounds(20,250,60,20);
rate.setBounds(90,250,100,20);
btn1.setBounds(50,330,70,20);
btn2.setBounds(250,330,70,20);
btn3.setBounds(450,330,70,20);
this.add(l);
this.add(l1);
this.add(name);
this.add(l2);
this.add(capital);
this.add(l3);
this.add(employees);
this.add(l4);
this.add(organization);
this.add(industrial);
this.add(medicine);
this.add(Mechatronics);
this.add(l5);
this.add(income);
this.add(l6);
this.add(rate);
this.add(btn1);
this.add(btn2);
this.add(btn3);
setLocationRelativeTo(null);
btn1.addActionListener(this);
btn2.addActionListener(this);
btn3.addActionListener(this);
capital.addKeyListener(this);
capital.addFocusListener(this);
employees.addKeyListener(this);
employees.addFocusListener(this);
income.addKeyListener(this);
income.addFocusListener(this);
rate.addKeyListener(this);
rate.addFocusListener(this);
}
public static void main(String[] args) {
// TODO Auto-generated method stub
Test1 app = new Test1("企业信息调查表");
app.setVisible(true);
}
@Override
public void focusGained(FocusEvent e) {
// TODO Auto-generated method stub
}
@Override
public void focusLost(FocusEvent e) {
// TODO Auto-generated method stub
String str1 = capital.getText();
String str2 = employees.getText();
boolean result1 = true;
boolean result2 = true;
Pattern pattern = Pattern.compile("^[-\\+]?[\\d]*$");
result1 = pattern.matcher(str1).matches();
result2 = pattern.matcher(str2).matches();
if(result1 == false||result2 == false){
JOptionPane.showMessageDialog(null, "输入有误,请重新输入","错误提示",JOptionPane.ERROR_MESSAGE);
capital.setText("");
employees.setText("");
capital.requestFocus();
employees.requestFocus();
}
}
@Override
public void keyTyped(KeyEvent e) {
// TODO Auto-generated method stub
if(((e.getKeyChar()<=0x39)&&(e.getKeyChar()>=0x30))||(e.getKeyChar()==127)||(e.getKeyChar()==46)){
e.setKeyChar(e.getKeyChar());
}else{
e.setKeyChar((char)0);
}
}
@Override
public void keyPressed(KeyEvent e) {
// TODO Auto-generated method stub
}
@Override
public void keyReleased(KeyEvent e) {
// TODO Auto-generated method stub
}
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
Object ob = e.getSource();
if(ob == btn1){
System.out.println("企业名称: "+name.getText());
System.out.println("注册资金: "+capital.getText());
System.out.println("员工数量: "+employees.getText());
System.out.println("从事行业: "+chBtnGrp.getSelectedCheckbox());
System.out.println("年营业额: "+income.getText());
System.out.println("利润率:"+rate.getText());
}else if(ob == btn2){
name.setText("");
chBtnGrp.setSelectedCheckbox(organization);
employees.setText("");
income.setText("");
capital.setText("");
rate.setText("");
organization.setState(false);
industrial.setState(false);
medicine.setState(false);
Mechatronics.setState(false);
}else if(ob == btn3){
System.exit(0);
}
}
}