Created
May 21, 2012 10:13
-
-
Save stevennathaniel/2761660 to your computer and use it in GitHub Desktop.
Membuat Layout Menggunakan Java SE
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
/* | |
* To change this template, choose Tools | Templates | |
* and open the template in the editor. | |
*/ | |
package FrameSwing; | |
import java.awt.Component; | |
import java.awt.Container; | |
import javax.swing.BoxLayout; | |
import javax.swing.JButton; | |
import javax.swing.JFrame; | |
/** | |
* | |
* @author Steven Nathaniel | |
*/ | |
public class KotakLayout1 { | |
public static void addComponentsToPane(Container pane){ | |
pane.setLayout(new BoxLayout(pane, BoxLayout.Y_AXIS)); | |
addAButton("Button 1", pane); | |
addAButton("Button 2", pane); | |
addAButton("Button 3", pane); | |
addAButton("Long-Named Button 4", pane); | |
addAButton("5", pane); | |
} | |
/* Di bawah ini adalah kode untuk addAButton diatas | |
* Jadi tanpa kode di bawah ini maka addAButton di atas akan error | |
* Perlu diingat bahwa dalam konsep OOP segala sesuatu nya harus | |
* dideklarasikan | |
*/ | |
private static void addAButton(String text, Container container){ | |
JButton button = new JButton(text); | |
button.setAlignmentX(Component.CENTER_ALIGNMENT); | |
container.add(button); | |
} | |
/** | |
* Create the GUI and show it. For thread safety, | |
* this method should be invoked from the | |
* event-dispatching thread. | |
*/ | |
private static void createAndShowGUI(){ | |
// Create and setup the window. | |
JFrame frame = new JFrame ("Demo Border Layout"); | |
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); | |
// set up the content pane. | |
addComponentsToPane(frame.getContentPane()); | |
// Display the window. | |
frame.pack(); | |
frame.setVisible(true); | |
} | |
public static void main (String[]args){ | |
// Schedule a job for the event-dispatching thread | |
// creating and showing this application's GUI | |
javax.swing.SwingUtilities.invokeLater(new Runnable(){ | |
public void run(){ | |
createAndShowGUI(); | |
} | |
}); | |
} | |
} | |
/* selanjutnya mau bikin source code untuk JTextField | |
* dengan menggunakan dasar source code ini. | |
* juga ingin buat beberapa variasi berbasis source code ini | |
*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment