F
Fencer
Hello, I have a problem with BorderLayout, and I bet it's really simple.
I have JPanel with a TitledBorder (like a "group" widget) and this
JPanel contains a button.
Now I want to displays this JPanel centered horizontally and vertically
and I don't want it to occupy all the space of the client area of the
JFrame.
I tried this:
package main;
import java.awt.BorderLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.TitledBorder;
public class CenteredGroup {
CenteredGroup() {
frame.setSize(1024, 768);
frame.setLocationRelativeTo(null);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel contentPane = (JPanel)frame.getContentPane();
BorderLayout borderLayout = new BorderLayout(50, 50);
frame.setLayout(borderLayout);
contentPane.setLayout(borderLayout);
JPanel groupPanel = new JPanel();
groupPanel.setBorder(new TitledBorder("This is my group text."));
JButton button = new JButton("A button");
groupPanel.add(button);
contentPane.add(groupPanel, BorderLayout.CENTER);
frame.setVisible(true);
}
public static void main(String[] args) {
new CenteredGroup();
}
JFrame frame = new JFrame("Centered Group");
}
However, the "group control" occupies the entire client area of the
frame. Why?
I can provide a screenshot of what it looks like and how I want it to
look like if you have trouble understanding my problem description.
- F
I have JPanel with a TitledBorder (like a "group" widget) and this
JPanel contains a button.
Now I want to displays this JPanel centered horizontally and vertically
and I don't want it to occupy all the space of the client area of the
JFrame.
I tried this:
package main;
import java.awt.BorderLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.TitledBorder;
public class CenteredGroup {
CenteredGroup() {
frame.setSize(1024, 768);
frame.setLocationRelativeTo(null);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel contentPane = (JPanel)frame.getContentPane();
BorderLayout borderLayout = new BorderLayout(50, 50);
frame.setLayout(borderLayout);
contentPane.setLayout(borderLayout);
JPanel groupPanel = new JPanel();
groupPanel.setBorder(new TitledBorder("This is my group text."));
JButton button = new JButton("A button");
groupPanel.add(button);
contentPane.add(groupPanel, BorderLayout.CENTER);
frame.setVisible(true);
}
public static void main(String[] args) {
new CenteredGroup();
}
JFrame frame = new JFrame("Centered Group");
}
However, the "group control" occupies the entire client area of the
frame. Why?
I can provide a screenshot of what it looks like and how I want it to
look like if you have trouble understanding my problem description.
- F