Created
August 10, 2012 23:44
-
-
Save arbo77/3318990 to your computer and use it in GitHub Desktop.
Step by step simple Java GUI example
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import javax.swing.*; | |
import java.awt.*; | |
import java.awt.event.*; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class MyFrame extends JFrame { | |
public MyFrame(){ | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class MyFrame extends JFrame { | |
public MyFrame(){ | |
initComponent(); | |
initEvent(); | |
} | |
private void initComponent(){ | |
} | |
private void initEvent(){ | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class MyFrame extends JFrame { | |
private JButton btnTutup = new JButton("Tutup"); | |
private JButton btnTambah = new JButton("Tambah"); | |
private JTextField txtA = new JTextField(); | |
private JTextField txtB = new JTextField(); | |
private JTextField txtC = new JTextField(); | |
private JLabel lblA = new JLabel("A :"); | |
private JLabel lblB = new JLabel("B :"); | |
private JLabel lblC = new JLabel("C :"); | |
public MyFrame(){ | |
initComponent(); | |
initEvent(); | |
} | |
private void initComponent(){ | |
} | |
private void initEvent(){ | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
... | |
... | |
private void initComponent(){ | |
btnTutup.setBounds(300,130, 80,25); | |
btnTambah.setBounds(300,100, 80,25); | |
txtA.setBounds(100,10,100,20); | |
txtB.setBounds(100,35,100,20); | |
txtC.setBounds(100,65,100,20); | |
lblA.setBounds(20,10,100,20); | |
lblB.setBounds(20,35,100,20); | |
lblC.setBounds(20,65,100,20); | |
add(btnTutup); | |
add(btnTambah); | |
add(lblA); | |
add(lblB); | |
add(lblC); | |
add(txtA); | |
add(txtB); | |
add(txtC); | |
} | |
... | |
... |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
... | |
... | |
private void initEvent(){ | |
this.addWindowListener(new WindowAdapter() { | |
public void windowClosing(WindowEvent e){ | |
System.exit(1); | |
} | |
}); | |
btnTutup.addActionListener(new ActionListener() { | |
public void actionPerformed(ActionEvent e) { | |
btnTutupClick(e); | |
} | |
}); | |
btnTambah.addActionListener(new ActionListener() { | |
public void actionPerformed(ActionEvent e) { | |
btnTambahClick(e); | |
} | |
}); | |
} | |
... | |
... |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
... | |
... | |
private void btnTutupClick(ActionEvent evt){ | |
System.exit(0); | |
} | |
private void btnTambahClick(ActionEvent evt){ | |
Integer x,y,z; | |
try{ | |
x = Integer.parseInt(txtA.getText()); | |
y = Integer.parseInt(txtB.getText()); | |
z = x + y; | |
txtC.setText(z.toString()); | |
}catch(Exception e){ | |
System.out.println(e); | |
JOptionPane.showMessageDialog(null, | |
e.toString(), | |
"Error", | |
JOptionPane.ERROR_MESSAGE); | |
} | |
} | |
... | |
... |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment