分类: Java
2006-06-28 15:27:43
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class LaborageFrane extends JFrame{
JTextField text2;
JTextField text3;
JTextField text1;
JButton btn2;
JButton btn1;
JTextArea t1;
JLabel label1,label2,label3;
//构造函数
LaborageFrane(){
createComponents();
layoutComponents();
registerEventHandlers();
setTitle("工资程序");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
pack();
}
//创建组件
private void createComponents(){
label1=new JLabel("编号",JLabel.LEFT);
label2=new JLabel("基本工资",JLabel.LEFT);
label3=new JLabel("销售额",JLabel.LEFT);
text1=new JTextField("001",5);
text2=new JTextField("0.0",5);
text3=new JTextField("0.0",5);
t1=new JTextArea(3,5);
}
//布局
private void layoutComponents() {
Container contentPane=this.getContentPane();
contentPane.setLayout(new GridLayout(1,2));
JPanel jp1=new JPanel();
jp1.setLayout(new GridLayout(2,1));
contentPane.add(jp1);
contentPane.add(t1);
JPanel jp2=new JPanel();
jp2.setLayout(new GridLayout(3,2));
jp1.add(jp2);
JPanel jp4=new JPanel();
jp4.setLayout(new GridLayout(3,1));
jp1.add(jp4);
JPanel jp3=new JPanel();
jp3.setLayout(new BorderLayout());
jp4.add(jp3);
jp2.add(label1);
jp2.add(text1);
jp2.add(label2);
jp2.add(text2);
jp2.add(label3);
jp2.add(text3);
btn1=new JButton("确定");
btn2=new JButton("清除");
jp3.add(btn1,BorderLayout.WEST);
jp3.add(btn2,BorderLayout.EAST);
}
//为按钮注册事件
private void registerEventHandlers(){
btnShowActionEventHander showHander=new btnShowActionEventHander();
btn1.addActionListener(showHander);
btnClearActionEventHander clearHander=new btnClearActionEventHander();
btn2.addActionListener(clearHander);
}
//确定按钮事件
private class btnShowActionEventHander implements ActionListener{
String id;
double monthSalary;
double commission;
double salary;
String out=new String();
public void actionPerformed(ActionEvent e){
monthSalary=Double.parseDouble(text2.getText());
commission=Double.parseDouble(text3.getText());
id=text1.getText();
if(commission>=30.0&&commission<=299.99)
salary=commission*0.05+monthSalary;
else if(commission>=300.0&&commission<=999.99)
salary=commission*0.1+monthSalary;
else if(commission>=1000.0)
salary=commission*0.15+monthSalary;
else
salary=monthSalary;
out="编号: "+id+"\n"
+"基本工资:"+monthSalary+"\n"
+"销售额:"+commission+"\n"
+"工资:"+salary+"\n";
t1.setText(out);
}
}
//清除按钮事件
private class btnClearActionEventHander implements ActionListener{
public void actionPerformed(ActionEvent e){
text1.setText("");
text2.setText("");
text3.setText("");
t1.setText("");
}
}
public static void main(String[] args){
JFrame.setDefaultLookAndFeelDecorated(true);
LaborageFrane laboragefrane=new LaborageFrane();
laboragefrane.setVisible(true);
}
}