制作一個簡單的企業資訊調查表,其主要內容有:企業名稱,注冊資金(只能為整數),員工數量(只能為整數),從事行業(機構組織、資訊產業、醫藥衛生、機械機電、只能選擇其一)、年營業額(浮點數)、利潤率(浮點數)。
uj5u.com熱心網友回復:
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);
}
}
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/143762.html
標籤:Java相關
下一篇:求助,介面資訊入庫,怎么整
