Results 1 to 3 of 3
Thread: Centering withen a JPanel
- 10-01-2010, 07:24 PM #1
Member
- Join Date
- Sep 2010
- Posts
- 7
- Rep Power
- 0
Centering withen a JPanel
This is my code
Java Code:public class FaciltyLogIn extends JPanel implements ActionListener { public static JPasswordField codeInput; public static JDialog dialog; public static boolean loggedIn = false; public static String user = ""; public FaciltyLogIn() { if (!loggedIn) { JLabel label = new JLabel("Password"); codeInput = new JPasswordField(20); JButton button = new JButton("OK"); button.setActionCommand("OK"); button.addActionListener(this); add(label); add(codeInput); add(button); } else if (loggedIn) { Calendar calendar = Calendar.getInstance(); SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); JLabel timeLabel = new JLabel("You timed in at " + dateFormat.format(calendar.getTime())); JLabel nameLabel = new JLabel("Welcome back " + user); JButton button = new JButton("CLOSE"); button.setActionCommand("CLOSE"); button.addActionListener(this); add(timeLabel); add(nameLabel); add(button); } } ... private static void GUI() { dialog = new JDialog(); FaciltyLogIn contentPane = new FaciltyLogIn(); contentPane.setOpaque(true); dialog.setContentPane(contentPane); Dimension size = Toolkit.getDefaultToolkit().getScreenSize(); size.setSize(size.getWidth(), size.getHeight()); dialog.toFront(); dialog.setSize(size); dialog.setDefaultCloseOperation(JDialog.DO_NOTHING_ON_CLOSE); dialog.setResizable(false); dialog.setUndecorated(true); dialog.setAlwaysOnTop(true); dialog.setVisible(true); }
- 10-01-2010, 07:37 PM #2
Senior Member
- Join Date
- Jun 2008
- Posts
- 2,568
- Rep Power
- 15
Try GridBagLayout
- 10-01-2010, 08:00 PM #3
Member
- Join Date
- Sep 2010
- Posts
- 7
- Rep Power
- 0
That work nicely thank you very very much.
Java Code:GridBagLayout gridBag = new GridBagLayout(); GridBagConstraints constraints = new GridBagConstraints(); setLayout(gridBag); constraints.fill = GridBagConstraints.BOTH; constraints.weightx = 1.0; constraints.gridwidth = GridBagConstraints.REMAINDER; gridBag.setConstraints(timeLabel, constraints); constraints.gridwidth = GridBagConstraints.REMAINDER; gridBag.setConstraints(nameLabel, constraints); constraints.gridwidth = GridBagConstraints.REMAINDER; gridBag.setConstraints(button, constraints);
Similar Threads
-
Centering things a content pane
By RKhadder in forum New To JavaReplies: 1Last Post: 09-28-2010, 01:43 AM -
component centering
By BigBear in forum AWT / SwingReplies: 3Last Post: 01-24-2010, 11:48 PM -
centering a label inside a rectangle
By Brain_Child in forum New To JavaReplies: 3Last Post: 11-19-2009, 05:22 PM -
Centering inside a JFrame
By kahaj in forum AWT / SwingReplies: 9Last Post: 09-23-2009, 07:23 PM -
Centering text of output
By dch414 in forum New To JavaReplies: 2Last Post: 10-02-2008, 10:08 PM
Bookmarks