⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 testifelse.java

📁 还不错的java基本实例
💻 JAVA
字号:
/*
 * 创建日期 2006-1-24
 *
 * TODO 要更改此生成的文件的模板,请转至
 * 窗口 - 首选项 - Java - 代码样式 - 代码模板
 */
package ch4;

import javax.swing.*;

import java.awt.*;
import java.awt.event.*;
//import java.text.NumberFormat;
public class TestIfElse extends JFrame{
   private JLabel idLabel;
   private JLabel monthsalaryLabel;
   private JLabel commissionLabel;
   private JTextField idText;
   private JTextField monthsalaryText;
   private JTextField commissionText;   
   private JTextArea text1;   
    private JButton btnShow;
    private JButton btnClear;
    private JButton btnClearInputField;
    
    String out="编号\t"+"基本工资\t"+"销售额\t"+"工资\t"+"\n";

    public TestIfElse() {
        createComponents();
        registerEventHandlers();
        layoutComponents();
         setTitle("工资发放程序");
         setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
         pack();
    }

     private void createComponents() {  //组件实例化    
          idLabel=new JLabel("              编号");
          monthsalaryLabel=new JLabel("     基本工资");
          commissionLabel=new JLabel("         销售额");
          idText=new JTextField(10);
          monthsalaryText=new JTextField(10);
          commissionText=new JTextField(10);
          text1 = new JTextArea(); 
         // text1.setAutoscrolls(true);
          //text1.setPreferredSize(new Dimension(350,200));
         
          btnClearInputField=new JButton("清除输入");
          btnShow = new JButton("工资计算");
          btnClear = new JButton("清除显示框内容"); 
     }

     private void registerEventHandlers() {  //注册事件
          btnShowActionEventHander hander1=new btnShowActionEventHander();
          btnShow.addActionListener(hander1);
         
          btnClearActionEventHander hander2=new btnClearActionEventHander();
          btnClear.addActionListener(hander2);
          
          btnClearInputFieldActionEventHander hander3=new btnClearInputFieldActionEventHander();
          btnClearInputField.addActionListener(hander3);
     }

     private void layoutComponents() {  //布局
        Container c=this.getContentPane();
      //  c.setLayout(new GridLayout(1, 2));
        c.setLayout(new FlowLayout());
        //存放文本框和标签
        JPanel idPane = new JPanel();
        idPane.add(idLabel);
        idPane.add(idText);
        //存放文本框和标签
        JPanel monthsalaryPane = new JPanel();
        monthsalaryPane.add(monthsalaryLabel);
        monthsalaryPane.add(monthsalaryText);
        //存放文本框和标签
        JPanel commissionPane = new JPanel();
        commissionPane.add(commissionLabel);
        commissionPane.add(commissionText);
        
        //存放按钮
        JPanel buttonPane1 = new JPanel();
        JPanel buttonPane2 = new JPanel();
        buttonPane1.add(btnShow);       
        buttonPane1.add(btnClearInputField);
        buttonPane2.add(btnClear);
         
     JPanel leftPane = new JPanel(new GridLayout(5,1));
        leftPane.add(idPane);
        leftPane.add(monthsalaryPane);
        leftPane.add(commissionPane);
        leftPane.add(buttonPane1);
        leftPane.add(buttonPane2);
              
       JScrollPane scrollPane=new JScrollPane(text1);
       scrollPane.setPreferredSize(new Dimension(350,200));
       c.add(leftPane);
       c.add(scrollPane);           
    }

      private class btnShowActionEventHander implements ActionListener{
          String id;
          double monthSalary;
          double commission;
          double salary;
   
          public void actionPerformed(ActionEvent e){
           monthSalary=Double.parseDouble(monthsalaryText.getText());
           commission=Double.parseDouble(commissionText.getText());
           id=idText.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+"\t"
                +monthSalary+"\t"
                +commission+"\t"
                +salary+"\t\n";
           text1.setText(out);
          }
    }
      private class btnClearInputFieldActionEventHander implements ActionListener{
        public void actionPerformed(ActionEvent e) {
         idText.setText("");
            monthsalaryText.setText("");
            commissionText.setText("");
        }
    }
     private class btnClearActionEventHander implements ActionListener{
        public void actionPerformed(ActionEvent e) {
             text1.setText("");
             out="";
        }
    }     
    public static void main(String[] args) {
        JFrame.setDefaultLookAndFeelDecorated(true);
        TestIfElse frame=new TestIfElse();
        frame.setVisible(true);
       }
    
}





/* package ch4;

import javax.swing.*;

import java.awt.*;
import java.awt.event.*;
//import java.text.NumberFormat;
public class TestIfElse extends JFrame{
   private JLabel idLabel;
   private JLabel monthsalaryLabel;
   private JLabel commissionLabel;
   private JTextField idText;
   private JTextField monthsalaryText;
   private JTextField commissionText;   
   private JTextArea text1;   
    private JButton btnShow;
    private JButton btnClear;
    private JButton btnClearInputField;
    
    String out="编号\t"+"基本工资\t"+"销售额\t"+"工资\t"+"\n";

    public TestIfElse() {
        createComponents();
        registerEventHandlers();
        layoutComponents();
         setTitle("工资发放图形界面程序");
         setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
         pack();
    }

     private void createComponents() {  //组件实例化    
          idLabel=new JLabel("              编号");
          monthsalaryLabel=new JLabel("     基本工资");
          commissionLabel=new JLabel("         销售额");
          idText=new JTextField(10);
          monthsalaryText=new JTextField(10);
          commissionText=new JTextField(10);
          text1 = new JTextArea(); 
          btnClearInputField=new JButton("清除输入");
          btnShow = new JButton("贷款计算");
          btnClear = new JButton("清除显示框内容");       
     }

     private void registerEventHandlers() {  //注册事件
          btnShowActionEventHander hander1=new btnShowActionEventHander();
          btnShow.addActionListener(hander1);
         
          btnClearActionEventHander hander2=new btnClearActionEventHander();
          btnClear.addActionListener(hander2);
          
          btnClearInputFieldActionEventHander hander3=new btnClearInputFieldActionEventHander();
          btnClearInputField.addActionListener(hander3);
     }

     private void layoutComponents() {  //布局
        Container c=this.getContentPane();
        c.setLayout(new GridLayout(1, 2));
        //存放文本框和标签
        JPanel idPane = new JPanel();
        idPane.add(idLabel);
        idPane.add(idText);
        //存放文本框和标签
        JPanel monthsalaryPane = new JPanel();
        monthsalaryPane.add(monthsalaryLabel);
        monthsalaryPane.add(monthsalaryText);
        //存放文本框和标签
        JPanel commissionPane = new JPanel();
        commissionPane.add(commissionLabel);
        commissionPane.add(commissionText);
        //存放文本输入区域
        JPanel textAndLabelFieldPane = new JPanel(new GridLayout(3,1));
        textAndLabelFieldPane.add(idPane);
        textAndLabelFieldPane.add(monthsalaryPane);
        textAndLabelFieldPane.add(commissionPane);
        
        //存放按钮
        JPanel buttonPane = new JPanel(new GridLayout(2,1));
        JPanel buttonPane1 = new JPanel();
        JPanel buttonPane2 = new JPanel();
        buttonPane1.add(btnShow);       
        buttonPane1.add(btnClearInputField);
        buttonPane2.add(btnClear);
        buttonPane.add(buttonPane1);
        buttonPane.add(buttonPane2);  
        //存放按钮区域
        JPanel leftPane = new JPanel(new GridLayout(2,1));
        leftPane.add(textAndLabelFieldPane);
        leftPane.add(buttonPane);
        
        //JPanel leftPane = new JPanel(new GridLayout(1,1));
        //leftPane.add(loanCalculateFieldPane);
        
       // JPanel rightPane = new JPanel(new GridLayout(1,1));
       // rightPane.add(new JScrollPane(text1));        
   
        c.add(leftPane);
        c.add( new JScrollPane(text1));  
             
    }

      private class btnShowActionEventHander implements ActionListener{
          String id;
          double monthSalary;
          double commission;
          double salary;
         
          public void actionPerformed(ActionEvent e){
           monthSalary=Double.parseDouble(monthsalaryText.getText());
           commission=Double.parseDouble(commissionText.getText());
           id=idText.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+"\t"
                +monthSalary+"\t"
                +commission+"\t"
                +salary+"\t\n";
           text1.setText(out);
          }
    }
      private class btnClearInputFieldActionEventHander implements ActionListener{
        public void actionPerformed(ActionEvent e) {
         idText.setText("");
            monthsalaryText.setText("");
            commissionText.setText("");
        }
    }
     private class btnClearActionEventHander implements ActionListener{
        public void actionPerformed(ActionEvent e) {
             text1.setText("");
             out="";
        }
    }     
    public static void main(String[] args) {
        JFrame.setDefaultLookAndFeelDecorated(true);
        TestIfElse frame=new TestIfElse();
        frame.setVisible(true);
       }
} */


⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -