package salary;
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import java.text.NumberFormat;
public class Salary extends JFrame{
//声明标签
private JLabel numl;
private JLabel basel;
private JLabel salel;
//声明文本框
private JTextField numf;
private JTextField basef;
private JTextField salef;
//声明滚动条
private TextArea sumt;
//声明按钮
private JButton btnsure;
private JButton btnclean;
private JButton btnout;
//构造方法------------------------------------------------------------------
public Salary(){
q();
w();
r();
setSize(300, 400);
setTitle("工资发放程序");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
pack();
}
//实例化组件----------------------------------------------------------------------
private void q(){
numl=new JLabel("编号");
basel=new JLabel("基本工资");
salel=new JLabel("销售额");
numf=new JTextField();
numf.setColumns(10);
basef=new JTextField();
basef.setColumns(10);
salef=new JTextField();
salef.setColumns(10);
sumt = new TextArea();
sumt.setColumns(30);
btnsure=new JButton("工资计算");
btnclean=new JButton("清除输入");
btnout=new JButton("清除显示框内容");
}
//布局-----------------------------------------------------------------------
private void w(){
Container a = getContentPane();
//存放标签
JPanel panel1 = new JPanel(new GridLayout(3, 1));
panel1.add(numl);
panel1.add(basel);
panel1.add(salel);
//存放文本框
JPanel panel2 = new JPanel(new GridLayout(3, 1));
panel2.add(numf,new FlowLayout(FlowLayout.LEFT,10,10));
panel2.add(basef,new FlowLayout(FlowLayout.LEFT,10,10));
panel2.add(salef,new FlowLayout(FlowLayout.LEFT,10,10));
//存放文本框与标签
JPanel panel3=new JPanel(new BorderLayout());
panel3.setBorder(BorderFactory.createEmptyBorder(10, 10, 5, 10));
panel3.add(panel1,BorderLayout.LINE_START); //西
panel3.add(panel2,BorderLayout.LINE_END); //东
//存放按钮
JPanel panelBtn= new JPanel(new FlowLayout(FlowLayout.CENTER,20,20));
panelBtn.add(btnsure);
panelBtn.add(btnclean);
//面板——1
JPanel panel=new JPanel(new BorderLayout());
panel.setBorder(BorderFactory.createEmptyBorder(10, 10, 5, 10));
panel.add(panel3,BorderLayout.PAGE_START); //北
panel.add(panelBtn,BorderLayout.CENTER);
panel.add(btnout,BorderLayout.PAGE_END); //南
//总面板
JPanel pan=new JPanel(new BorderLayout());
pan.setBorder(BorderFactory.createEmptyBorder(10, 10, 5, 10));
pan.add(panel,BorderLayout.CENTER);
pan.add(sumt,BorderLayout.LINE_END);
//
a.add(pan);
}
//--------------------------------------------------------------------------------------
private String t(){
double sum,s,b;
s=Double.parseDouble(salef.getText());
b=Double.parseDouble(basef.getText());
if(s>=30.0 && s<=299.99)
sum=s*0.05+b;
else if(s>=300.0 && s<=999.9)
sum=s*0.1+b;
else if(s>=1000.0)
sum=s*0.15+b;
else
sum=b;
String out="";
out +=String.format("%s\t%.1f\t%.1f\t%.1f\n", numf.getText(),b,s,sum);
return out;
}
//----------------------------------------------------------------------------------
private class btnsure implements ActionListener {
public void actionPerformed(ActionEvent e) {
String s="编号 \t"+"基本工资 \t"+"销售额 \t"+"工资 \n";
String s1 =s+sumt.getText();
s1 +=t();
sumt.setText(s1);
}
}
//---------------------------------------------------------------------------------------
private class btnclean implements ActionListener {
public void actionPerformed(ActionEvent e) {
numf.setText("");
basef.setText("");
salef.setText("");
}
}
//---------------------------------------------------------------------------------------
private class btnout implements ActionListener {
public void actionPerformed(ActionEvent e) {
sumt.setText("");
}
}
//---------------------------------------------------------------------------------------
private void r() {
btnsure h1 = new btnsure();
btnsure.addActionListener(h1);
btnclean h2=new btnclean();
btnclean.addActionListener(h2);
btnout h3 = new btnout();
btnout.addActionListener(h3);
}
public static void main(String[] args) {
JFrame.setDefaultLookAndFeelDecorated(true); // 窗体装饰
Salary frame = new Salary();
frame.setVisible(true);
}
}
添加回答
举报
0/150
提交
取消