GUI

import java.awt.Button; import java.awt.Frame; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JTextArea; import javax.swing.JTextField; public class GUItest extends JFrame{ public static void main(String[] args) { new GUItest(); } public GUItest() { this.setTitle("Guiii"); this.setSize(300,400); this.setLayout(null); this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); JLabel lb1 = new JLabel("Nhap a"); lb1.setBounds(20, 20, 50, 30); this.add(lb1); JLabel lb2 = new JLabel("Nhap b"); lb2.setBounds(20, 60, 50, 30); this.add(lb2); JTextField tf1 = new JTextField(); tf1.setBounds(60, 20, 150, 30); this.add(tf1); JTextField tf2 = new JTextField(); tf2.setBounds(60, 60, 150, 30); this.add(tf2); JButton btn_add = new JButton("+"); btn_add.setBounds(20, 100, 50, 30); this.add(btn_add); JButton btn_sub = new JButton("-"); btn_sub.setBounds(80, 100, 50, 30); this.add(btn_sub); JButton btn_mul = new JButton("*"); btn_mul.setBounds(140, 100, 50, 30); this.add(btn_mul); JButton btn_div = new JButton("/"); btn_div.setBounds(200, 100, 50, 30); this.add(btn_div); JLabel kq = new JLabel("Ket qua"); kq.setBounds(20, 140, 50, 30); this.add(kq); JTextField result = new JTextField(); result.setBounds(80, 140, 100, 30); this.add(result); btn_add.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { try{ int a = Integer.parseInt(tf1.getText()); int b = Integer.parseInt(tf2.getText()); result.setText(Integer.toString(a+b)); } catch (Exception ee){ result.setText("Nhap sai"); }; } }); btn_sub.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { try{ int a = Integer.parseInt(tf1.getText()); int b = Integer.parseInt(tf2.getText()); result.setText(Integer.toString(a-b)); } catch (Exception ee){ result.setText("Nhap sai"); }; } }); btn_mul.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { try{ int a = Integer.parseInt(tf1.getText()); int b = Integer.parseInt(tf2.getText()); result.setText(Integer.toString(a*b)); } catch (Exception ee){ result.setText("Nhap sai"); }; } }); btn_div.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { try{ Double a = Double.parseDouble(tf1.getText()); Double b = Double.parseDouble(tf2.getText()); result.setText(Double.toString(a/b)); } catch (Exception ee){ result.setText("Nhap sai"); }; } }); this.setVisible(true); } }

Be the first to comment

You can use [html][/html], [css][/css], [php][/php] and more to embed the code. Urls are automatically hyperlinked. Line breaks and paragraphs are automatically generated.