Center « Component « Java Swing Q&A





1. Placing a component at center both vertically and horizontally    coderanch.com

Hi Friends, This code displays a textfield and button at the top of the frame .How could I put these two component just at the center of frame both horizontally and vertically. Thanks in adv. Sonu public class mainapplic extends JFrame { public mainapplic() { TestPanel tp=new TestPanel(); Container c=getContentPane(); c.add(tp); setSize(400,400); setVisible(true); addWindowListener(new WindowAdapter(){ public void windowClosing(WindowEvent evt) { System.exit(-1); ...

2. component centering    java-forums.org

import java.awt.Color; import java.awt.Dimension; import java.awt.GridBagLayout; import javax.swing.*; import javax.swing.border.LineBorder; public class CenterComponent { private static void createAndShowUI() { JLabel centerLabel = new JLabel("Centered"); centerLabel.setBorder(new LineBorder(Color.black)); JPanel panel = new JPanel(new GridBagLayout()); panel.setPreferredSize(new Dimension(500, 500)); panel.add(centerLabel); JFrame frame = new JFrame("CenterComponent"); frame.getContentPane().add(panel); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.pack(); frame.setLocationRelativeTo(null); frame.setVisible(true); } public static void main(String[] args) { java.awt.EventQueue.invokeLater(new Runnable() { public void run() { createAndShowUI(); ...

3. Centering GUI Components    forums.oracle.com