標籤:
package helloo;import java.awt.BorderLayout;import java.awt.EventQueue;import javax.swing.JFrame;import javax.swing.JPanel;import javax.swing.border.EmptyBorder;import javax.swing.JSplitPane;import javax.swing.JLabel;import javax.swing.JTextField;import java.awt.event.MouseAdapter;import java.awt.event.MouseEvent;import javax.swing.JButton;import java.awt.event.ActionListener;import java.awt.event.ActionEvent;import java.awt.Font;import java.awt.SystemColor;import java.awt.event.KeyAdapter;import java.awt.event.KeyEvent;public class hellooo extends JFrame {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 {hellooo frame = new hellooo();frame.setVisible(true);frame.setTitle("-*-數字相加-*-");} catch (Exception e) {e.printStackTrace();}}});}/** * Create the frame. */public hellooo() {setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);setBounds(4, -30, 450, 300);contentPane = new JPanel();contentPane.addMouseListener(new MouseAdapter() {@Overridepublic void mouseClicked(MouseEvent e) {}});contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));setContentPane(contentPane);contentPane.setLayout(null);JLabel label = new JLabel("");label.setBounds(5, 5, 0, 256);contentPane.add(label);JLabel lblNewLabel = new JLabel("num1");lblNewLabel.setBounds(36, 40, 54, 15);contentPane.add(lblNewLabel);textField = new JTextField();textField.setBounds(122, 37, 66, 21);contentPane.add(textField);textField.setColumns(10);JLabel a = new JLabel("num2");a.setBounds(36, 95, 54, 15);contentPane.add(a);textField_1 = new JTextField();textField_1.setBounds(122, 92, 66, 21);contentPane.add(textField_1);textField_1.setColumns(10);JLabel lblNewLabel_2 = new JLabel("sum");lblNewLabel_2.setBounds(36, 146, 54, 15);contentPane.add(lblNewLabel_2);textField_2 = new JTextField();textField_2.addKeyListener(new KeyAdapter() {@Overridepublic void keyPressed(KeyEvent e) {if(e.getKeyChar()==KeyEvent.VK_ENTER ) { double head = Double.parseDouble(textField.getText()); double foot = Double.parseDouble(textField_1.getText()); double roosterNum = head+foot; textField_2.setText(""+roosterNum); }}});textField_2.setBounds(122, 143, 66, 21);contentPane.add(textField_2);textField_2.setColumns(10);JButton btnCal = new JButton("cal");btnCal.addKeyListener(new KeyAdapter() {@Overridepublic void keyPressed(KeyEvent e) { if(e.getKeyChar()==KeyEvent.VK_ENTER ) { double head = Double.parseDouble(textField.getText()); double foot = Double.parseDouble(textField_1.getText()); double roosterNum = head+foot; textField_2.setText(""+roosterNum); }}});btnCal.addActionListener(new ActionListener() {public void actionPerformed(ActionEvent e) {double head = Double.parseDouble(textField.getText()); double foot = Double.parseDouble(textField_1.getText()); double roosterNum = head+foot; textField_2.setText(""+roosterNum); }});btnCal.setBounds(71, 206, 93, 23);contentPane.add(btnCal);}}
第一個java程式