Chào mừng đến với Diễn đàn lập trình - Cộng đồng lập trình.
Kết quả 1 đến 2 của 2
  1. #1
    Ngày tham gia
    Sep 2015
    Bài viết
    0

    Xử lý sự kiện trong javax.swing

    Mã:
    public class abc {
    
    	private JFrame frmCalculate;
    	private JTextField textField;
    
    	/**
    	 * Launch the application.
    	 */
    	public static void main(String[] args) {
    		EventQueue.invokeLater(new Runnable() {
    			public void run() {
    				try {
    					abc window = new abc();
    					window.frmCalculate.setVisible(true);
    				} catch (Exception e) {
    					e.printStackTrace();
    				}
    			}
    		});
    	}
    
    	/**
    	 * Create the application.
    	 */
    	public abc() {
    		initialize();
    	}
    
    	/**
    	 * Initialize the contents of the frame.
    	 */
    	private void initialize() {		
    		frmCalculate = new JFrame();
    		frmCalculate.setAlwaysOnTop(true);
    		frmCalculate.setTitle("Calculate");
    		frmCalculate.setBounds(100, 100, 271, 337);
    		frmCalculate.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		frmCalculate.getContentPane().setLayout(null);
    		
    		
    		JButton btn1 = new JButton("1");
    		btn1.addActionListener(new ActionListener() {
    			public void actionPerformed(ActionEvent arg0) {
    				String gt = textField.getText();
    				if(gt.length() < 12)
    					if(gt.equals("0"))
    						gt = "";
    				long so = Long.parseLong(gt + btn1.getText());
    				textField.setText(String.valueOf(so));
    			}
    		});
    		btn1.setBounds(0, 51, 64, 49);
    		frmCalculate.getContentPane().add(btn1);
    		
    		JButton btn2 = new JButton("2");
    		btn2.addActionListener(new ActionListener() {
    			public void actionPerformed(ActionEvent e) {
    				String gt = textField.getText();
    				if(gt.length() < 12)
    					if(gt.equals("0"))
    						gt = "";
    				long so = Long.parseLong(gt + btn2.getText());
    				textField.setText(String.valueOf(so));
    			}
    		});
    		btn2.setBounds(64, 51, 64, 49);
    		frmCalculate.getContentPane().add(btn2);
    		
    		JButton btn3 = new JButton("3");
    		btn3.addActionListener(new ActionListener() {
    			public void actionPerformed(ActionEvent e) {
    				String gt = textField.getText();
    				if(gt.length() < 12)
    					if(gt.equals("0"))
    						gt = "";
    				long so = Long.parseLong(gt + btn3.getText());
    				textField.setText(String.valueOf(so));
    			}
    		});
    		btn3.setBounds(128, 51, 64, 49);
    		frmCalculate.getContentPane().add(btn3);
    	
    		JButton btnCong = new JButton("+");
    		btnCong.addActionListener(new ActionListener() {
    			public void actionPerformed(ActionEvent e) {
    			}
    		});
    		btnCong.setBounds(192, 51, 64, 49);
    		frmCalculate.getContentPane().add(btnCong);
    		
    		JButton btn4 = new JButton("4");
    		btn4.addActionListener(new ActionListener() {
    			public void actionPerformed(ActionEvent e) {
    				String gt = textField.getText();
    				if(gt.length() < 12)
    					if(gt.equals("0"))
    						gt = "";
    				long so = Long.parseLong(gt + btn4.getText());
    				textField.setText(String.valueOf(so));
    			}
    		});
    		btn4.setBounds(0, 100, 64, 49);
    		frmCalculate.getContentPane().add(btn4);
    		
    		JButton btn5 = new JButton("5");
    		btn5.addActionListener(new ActionListener() {
    			public void actionPerformed(ActionEvent e) {
    				String gt = textField.getText();
    				if(gt.length() < 12)
    					if(gt.equals("0"))
    						gt = "";
    				long so = Long.parseLong(gt + btn5.getText());
    				textField.setText(String.valueOf(so));
    			}
    		});
    		btn5.setBounds(64, 100, 64, 49);
    		frmCalculate.getContentPane().add(btn5);
    		
    		JButton btn6 = new JButton("6");
    		btn6.addActionListener(new ActionListener() {
    			public void actionPerformed(ActionEvent e) {
    				String gt = textField.getText();
    				if(gt.length() < 12)
    					if(gt.equals("0"))
    						gt = "";
    				long so = Long.parseLong(gt + btn6.getText());
    				textField.setText(String.valueOf(so));
    			}
    		});
    		btn6.setBounds(128, 100, 64, 49);
    		frmCalculate.getContentPane().add(btn6);
    		
    		JButton btnTru = new JButton("-");
    		btnTru.setBounds(192, 100, 64, 49);
    		frmCalculate.getContentPane().add(btnTru);
    		
    		JButton btn7 = new JButton("7");
    		btn7.addActionListener(new ActionListener() {
    			public void actionPerformed(ActionEvent e) {
    				String gt = textField.getText();
    				if(gt.length() < 12)
    					if(gt.equals("0"))
    						gt = "";
    				long so = Long.parseLong(gt + btn7.getText());
    				textField.setText(String.valueOf(so));
    			}
    		});
    		btn7.setBounds(0, 149, 64, 49);
    		frmCalculate.getContentPane().add(btn7);
    		
    		JButton btn8 = new JButton("8");
    		btn8.addActionListener(new ActionListener() {
    			public void actionPerformed(ActionEvent e) {
    				String gt = textField.getText();
    				if(gt.length() < 12)
    					if(gt.equals("0"))
    						gt = "";
    				long so = Long.parseLong(gt + btn8.getText());
    				textField.setText(String.valueOf(so));
    			}
    		});
    		btn8.setBounds(64, 149, 64, 49);
    		frmCalculate.getContentPane().add(btn8);
    		
    		JButton btn9 = new JButton("9");
    		btn9.addActionListener(new ActionListener() {
    			public void actionPerformed(ActionEvent e) {
    				String gt = textField.getText();
    				if(gt.length() < 12)
    					if(gt.equals("0"))
    						gt = "";
    				long so = Long.parseLong(gt + btn9.getText());
    				textField.setText(String.valueOf(so));
    			}
    		});
    		btn9.setBounds(128, 149, 64, 49);
    		frmCalculate.getContentPane().add(btn9);
    		
    		JButton btnNhan = new JButton("*");
    		btnNhan.setBounds(192, 149, 64, 49);
    		frmCalculate.getContentPane().add(btnNhan);
    		
    		JButton btn0 = new JButton("0");
    		btn0.addActionListener(new ActionListener() {
    			public void actionPerformed(ActionEvent e) {
    				String gt = textField.getText();
    				if(gt.length() < 12)
    					if(gt.equals("0"))
    						gt = "";
    				long so = Long.parseLong(gt + btn0.getText());
    				textField.setText(String.valueOf(so));
    			}
    		});
    		btn0.setBounds(0, 198, 64, 49);
    		frmCalculate.getContentPane().add(btn0);
    		
    		JButton btnCham = new JButton(".");
    		btnCham.setBounds(64, 198, 64, 49);
    		frmCalculate.getContentPane().add(btnCham);
    		
    		JButton btnBang = new JButton("=");
    		btnBang.addActionListener(new ActionListener() {
    			public void actionPerformed(ActionEvent e) {
    			}
    		});
    		btnBang.setBounds(128, 198, 64, 49);
    		frmCalculate.getContentPane().add(btnBang);
    		
    		JButton btnChia = new JButton("/");
    		btnChia.setBounds(192, 198, 64, 49);
    		frmCalculate.getContentPane().add(btnChia);
    		
    		textField = new JTextField();
    		textField.setEditable(false);
    		textField.setBounds(0, 2, 256, 49);
    		frmCalculate.getContentPane().add(textField);
    		textField.setColumns(10);
    		
    		JButton btnXoa = new JButton("C");
    		btnXoa.addActionListener(new ActionListener() {
    			public void actionPerformed(ActionEvent e) {
    				textField.setText(null);
    			}
    		});
    		btnXoa.setBounds(0, 247, 256, 49);
    		frmCalculate.getContentPane().add(btnXoa);
    	}
    }

    Em mới học java, đang làm chương trình máy tính bỏ túi nhưng không biết xử lý sự kiện của dấu "+" và dấu "=" như thế nào, các anh hướng dẫn giúp em. Em làm kéo thả trong eclipse ạ. [IMG]images/smilies/smile.png[/IMG]

  2. #2
    Ngày tham gia
    Sep 2015
    Bài viết
    0
    Cái này mình thêm 1 hàm, và 3 thuộc tính để xử lý, và chưa check sự kiện, nói chung chỉ đơn giản để tính +
    Mã:
    /*
     * To change this license header, choose License Headers in Project Properties.
     * To change this template file, choose Tools | Templates
     * and open the template in the editor.
     */
    package times;
    
    import com.sun.corba.se.spi.orbutil.fsm.Guard;
    import java.awt.EventQueue;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import javax.swing.JButton;
    import javax.swing.JComponent;
    import javax.swing.JFrame;
    import javax.swing.JTextField;
    
    public class abc {
    
    	private JFrame frmCalculate;
    	private JTextField textField;
            
            private Double value1 = 0d;
            private Double value2 = 0d;
            private String regular = "";
    
    	/**
    	 * Launch the application.
    	 */
    	public static void main(String[] args) {
    		EventQueue.invokeLater(new Runnable() {
    			public void run() {
    				try {
    					abc window = new abc();
    					window.frmCalculate.setVisible(true);
    				} catch (Exception e) {
    					e.printStackTrace();
    				}
    			}
    		});
    	}
    
    	/**
    	 * Create the application.
    	 */
    	public abc() {
    		initialize();
                    
    	}
    
    	/**
    	 * Initialize the contents of the frame.
    	 */
    	private void initialize() {		
    		frmCalculate = new JFrame();
    		frmCalculate.setAlwaysOnTop(true);
    		frmCalculate.setTitle("Calculate");
    		frmCalculate.setBounds(100, 100, 271, 337);
    		frmCalculate.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		frmCalculate.getContentPane().setLayout(null);
    		
    		
    		JButton btn1 = new JButton("1");
    		btn1.addActionListener(new ActionListener() {
    			public void actionPerformed(ActionEvent arg0) {
    				String gt = textField.getText();
    				if(gt.length() < 12)
    					if(gt.equals("0"))
    						gt = "";
    				long so = Long.parseLong(gt + btn1.getText());
    				textField.setText(String.valueOf(so));
    			}
    
    		});
    		btn1.setBounds(0, 51, 64, 49);
    		frmCalculate.getContentPane().add(btn1);
    		
    		JButton btn2 = new JButton("2");
    		btn2.addActionListener(new ActionListener() {
    			public void actionPerformed(ActionEvent e) {
    				String gt = textField.getText();
    				if(gt.length() < 12)
    					if(gt.equals("0"))
    						gt = "";
    				long so = Long.parseLong(gt + btn2.getText());
    				textField.setText(String.valueOf(so));
    			}
    		});
    		btn2.setBounds(64, 51, 64, 49);
    		frmCalculate.getContentPane().add(btn2);
    		
    		JButton btn3 = new JButton("3");
    		btn3.addActionListener(new ActionListener() {
    			public void actionPerformed(ActionEvent e) {
    				String gt = textField.getText();
    				if(gt.length() < 12)
    					if(gt.equals("0"))
    						gt = "";
    				long so = Long.parseLong(gt + btn3.getText());
    				textField.setText(String.valueOf(so));
    			}
    		});
    		btn3.setBounds(128, 51, 64, 49);
    		frmCalculate.getContentPane().add(btn3);
    	
    		JButton btnCong = new JButton("+");
    		btnCong.addActionListener(new ActionListener() {
    			public void actionPerformed(ActionEvent e) {
                                
    			}
    		});
    		btnCong.setBounds(192, 51, 64, 49);
    		frmCalculate.getContentPane().add(btnCong);
    		
    		JButton btn4 = new JButton("4");
    		btn4.addActionListener(new ActionListener() {
    			public void actionPerformed(ActionEvent e) {
    				String gt = textField.getText();
    				if(gt.length() < 12)
    					if(gt.equals("0"))
    						gt = "";
    				long so = Long.parseLong(gt + btn4.getText());
    				textField.setText(String.valueOf(so));
    			}
    		});
    		btn4.setBounds(0, 100, 64, 49);
    		frmCalculate.getContentPane().add(btn4);
    		
    		JButton btn5 = new JButton("5");
    		btn5.addActionListener(new ActionListener() {
    			public void actionPerformed(ActionEvent e) {
    				String gt = textField.getText();
    				if(gt.length() < 12)
    					if(gt.equals("0"))
    						gt = "";
    				long so = Long.parseLong(gt + btn5.getText());
    				textField.setText(String.valueOf(so));
    			}
    		});
    		btn5.setBounds(64, 100, 64, 49);
    		frmCalculate.getContentPane().add(btn5);
    		
    		JButton btn6 = new JButton("6");
    		btn6.addActionListener(new ActionListener() {
    			public void actionPerformed(ActionEvent e) {
    				String gt = textField.getText();
    				if(gt.length() < 12)
    					if(gt.equals("0"))
    						gt = "";
    				long so = Long.parseLong(gt + btn6.getText());
    				textField.setText(String.valueOf(so));
    			}
    		});
    		btn6.setBounds(128, 100, 64, 49);
    		frmCalculate.getContentPane().add(btn6);
    		
    		JButton btnTru = new JButton("-");
    		btnTru.setBounds(192, 100, 64, 49);
    		frmCalculate.getContentPane().add(btnTru);
    		
    		JButton btn7 = new JButton("7");
    		btn7.addActionListener(new ActionListener() {
    			public void actionPerformed(ActionEvent e) {
    				String gt = textField.getText();
    				if(gt.length() < 12)
    					if(gt.equals("0"))
    						gt = "";
    				long so = Long.parseLong(gt + btn7.getText());
    				textField.setText(String.valueOf(so));
    			}
    		});
    		btn7.setBounds(0, 149, 64, 49);
    		frmCalculate.getContentPane().add(btn7);
    		
    		JButton btn8 = new JButton("8");
    		btn8.addActionListener(new ActionListener() {
    			public void actionPerformed(ActionEvent e) {
    				String gt = textField.getText();
    				if(gt.length() < 12)
    					if(gt.equals("0"))
    						gt = "";
    				long so = Long.parseLong(gt + btn8.getText());
    				textField.setText(String.valueOf(so));
    			}
    		});
    		btn8.setBounds(64, 149, 64, 49);
    		frmCalculate.getContentPane().add(btn8);
    		
    		JButton btn9 = new JButton("9");
    		btn9.addActionListener(new ActionListener() {
    			public void actionPerformed(ActionEvent e) {
    				String gt = textField.getText();
    				if(gt.length() < 12)
    					if(gt.equals("0"))
    						gt = "";
    				long so = Long.parseLong(gt + btn9.getText());
    				textField.setText(String.valueOf(so));
    			}
    		});
    		btn9.setBounds(128, 149, 64, 49);
    		frmCalculate.getContentPane().add(btn9);
    		
    		JButton btnNhan = new JButton("*");
    		btnNhan.setBounds(192, 149, 64, 49);
    		frmCalculate.getContentPane().add(btnNhan);
    		
    		JButton btn0 = new JButton("0");
    		btn0.addActionListener(new ActionListener() {
    			public void actionPerformed(ActionEvent e) {
    				String gt = textField.getText();
    				if(gt.length() < 12)
    					if(gt.equals("0"))
    						gt = "";
    				long so = Long.parseLong(gt + btn0.getText());
    				textField.setText(String.valueOf(so));
    			}
    		});
    		btn0.setBounds(0, 198, 64, 49);
    		frmCalculate.getContentPane().add(btn0);
    		
    		JButton btnCham = new JButton(".");
    		btnCham.setBounds(64, 198, 64, 49);
    		frmCalculate.getContentPane().add(btnCham);
    		
    		JButton btnBang = new JButton("=");
    		btnBang.addActionListener(new ActionListener() {
    			public void actionPerformed(ActionEvent e) {
    			}
    		});
    		btnBang.setBounds(128, 198, 64, 49);
    		frmCalculate.getContentPane().add(btnBang);
    		
    		JButton btnChia = new JButton("/");
    		btnChia.setBounds(192, 198, 64, 49);
    		frmCalculate.getContentPane().add(btnChia);
    		
    		textField = new JTextField();
    		textField.setEditable(false);
    		textField.setBounds(0, 2, 256, 49);
    		frmCalculate.getContentPane().add(textField);
    		textField.setColumns(10);
    		
    		JButton btnXoa = new JButton("C");
    		btnXoa.addActionListener(new ActionListener() {
    			public void actionPerformed(ActionEvent e) {
    				textField.setText(null);
    			}
    		});
    		btnXoa.setBounds(0, 247, 256, 49);
    		frmCalculate.getContentPane().add(btnXoa);
                    
                    addMouseListener(btnCong);
                    addMouseListener(btnBang);
    	}
            
            private void addMouseListener(final JComponent component) {
            component.addMouseListener(new java.awt.event.MouseAdapter() {
                @Override
                public void mouseClicked(java.awt.event.MouseEvent event) {
                    if(!regular.equals("")){
                        value2 = Double.parseDouble(textField.getText());
                        textField.setText(value1+value2 +"");
                    }
                    if(((JButton) component).getText().equals("+")){
                        regular = "+";
                        value1 = Double.parseDouble(textField.getText());
                        textField.setText(null);
                    }
                    
                }
            });
        }
    }

 

 

Quyền viết bài

  • Bạn Không thể gửi Chủ đề mới
  • Bạn Không thể Gửi trả lời
  • Bạn Không thể Gửi file đính kèm
  • Bạn Không thể Sửa bài viết của mình
  •