[Java ] JTextField, Grid Layout
package thanhcs.blogspot.com; import java.awt.BorderLayout; import java.awt.Button; import java.awt.Color; import java.awt.GridLayout; 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.JOptionPane; import javax.swing.JPanel; import javax.swing.JTextField; import javax.swing.SwingConstants; public class GUIDEMO extends JFrame implements ActionListener{ JLabel mjLabel, mjLabel1, mjLabel2; JTextField mJtext, mJtext1, mJtext2; public GUIDEMO() { setLayout(new GridLayout(3, 3, 5, 5)); add(mjLabel = new JLabel("Enter numerator", SwingConstants.RIGHT)); add(mJtext = new JTextField()); add(mjLabel1 = new JLabel("Enter denominator and press enter",SwingConstants.RIGHT)); add(mJtext1 = new JTextField()); add(mjLabel2 = new JLabel("RESULT",SwingConstants.RIGHT)); add(mJtext2 = new JTextField()); mJtext1.addActionListener(this); setVisible(true); setDefaultCloseOperation(EXIT_ON_CLOSE); setSize(420, 150); setLocationRelativeTo(null); } @Override public void actionPerformed(ActionEvent arg0) { int temp1 = 0 ; int temp2 = 0 ; try { temp1 = Integer.parseInt(mJtext.getText()); temp2 = Integer.parseInt(mJtext1.getText()); } catch (Exception e) { JOptionPane.showMessageDialog(null, "Number only. Please ! :)"); } mJtext2.setText(""+(temp1+temp2)); } }
test.java
package thanhcs.blogspot.com; public class test { public static void main(String[] args) { GUIDEMO a = new GUIDEMO(); } }
No comments: