编程的乐趣

闲来无事,自学了java的GUI,用windowsbuilder做了一个小型计算器,哈哈,开心!

package javatest;

import java.awt.BorderLayout;
import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;

import org.omg.CORBA.PUBLIC_MEMBER;

import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.JComboBox;
import javax.swing.JButton;
import javax.swing.DefaultComboBoxModel;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.event.ItemListener;
import java.nio.file.StandardCopyOption;
import java.awt.event.ItemEvent;

public class computer extends JFrame {
 public String s="";
 private JPanel contentPane;
 private JTextField textField;
 private JTextField textField_1;
 private JTextField textField_2;

 /**
  * Launch the application.
  */
 public static void main(String[] args) {
  EventQueue.invokeLater(new Runnable() {
   public void run() {
    try {
     computer frame = new computer();
     frame.setVisible(true);
    } catch (Exception e) {
     e.printStackTrace();
    }
   }
  });
 }

 /**
  * Create the frame.
  */
 public computer() {
  setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  setBounds(100, 100, 491, 373);
  contentPane = new JPanel();
  contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
  setContentPane(contentPane);
  contentPane.setLayout(null);
  
  JLabel lblA = new JLabel("A");
  lblA.setBounds(79, 35, 36, 21);
  contentPane.add(lblA);
  
  JLabel label = new JLabel("\u8FD0\u7B97\u7B26");
  label.setBounds(188, 25, 81, 21);
  contentPane.add(label);
  
  JLabel lblB = new JLabel("B");
  lblB.setBounds(291, 25, 104, 21);
  contentPane.add(lblB);
  
  textField = new JTextField();
  textField.setBounds(69, 71, 115, 43);
  contentPane.add(textField);
  textField.setColumns(10);
  
  textField_1 = new JTextField();
  textField_1.setBounds(291, 71, 112, 43);
  contentPane.add(textField_1);
  textField_1.setColumns(10);
  
  JComboBox comboBox = new JComboBox();
  comboBox.addItemListener(new ItemListener() {
   public void itemStateChanged(ItemEvent e) {
    if(e.getStateChange()==ItemEvent.SELECTED){
     s=comboBox.getSelectedItem().toString();
    }
   }
  });
  comboBox.setModel(new DefaultComboBoxModel(new String[] {"+", "-", "*", "/"}));
  comboBox.setBounds(180, 71, 112, 43);
  contentPane.add(comboBox);
  
  textField_2 = new JTextField();
  textField_2.setBounds(69, 113, 334, 83);
  contentPane.add(textField_2);
  textField_2.setColumns(10);
  
  JButton btnNewButton = new JButton("\u8BA1\u7B97");
  btnNewButton.addActionListener(new ActionListener() {
   public void actionPerformed(ActionEvent e) {
    int ai=Integer.parseInt(textField.getText());
    int bi=Integer.parseInt(textField_1.getText());
    if(s.equals("+")){
     String result=String.valueOf(ai+bi);
     textField_2.setText("A+B= "+result);
    }
    if(s.equals("-")){
     String result=String.valueOf(ai-bi);
     textField_2.setText("A-B= "+result);
    }
    if(s.equals("*")){
     String result=String.valueOf(ai*bi);
     textField_2.setText("A*B= "+result);
    }
    if(s.equals("/")){
     String result=String.valueOf(ai/bi);
     textField_2.setText("A/B= "+result);
    }
    
   }
  });
  btnNewButton.setBounds(69, 196, 123, 29);
  contentPane.add(btnNewButton);
  
  JButton btnNewButton_1 = new JButton("\u9000\u51FA");
  btnNewButton_1.addActionListener(new ActionListener() {
   public void actionPerformed(ActionEvent e) {
    System.exit(0);
   }
  });
  btnNewButton_1.setBounds(280, 196, 123, 29);
  contentPane.add(btnNewButton_1);
 }
}

posted @ 2016-05-01 16:36  左岸繁华右岸殇  阅读(245)  评论(1编辑  收藏  举报