我正在使用gridbag布局来灵活布局我的项目,但实际上它现在变得让我头疼,经过近5个小时的艰苦尝试后,我无法根据我的要求做正确的位置,plz为我弄清楚: -
public static void main(String[] args) {
JFrame.setDefaultLookAndFeelDecorated(true);
JFrame frame = new JFrame();
//frame.setExtendedState(JFrame.MAXIMIZED_BOTH);
frame.setSize(300, 300);
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setFont(f);
frame.setTitle("Natural Language Processor");
frame.setLayout(new GridBagLayout());
GridBagConstraints c = new GridBagConstraints();
// Setting the label for the nlp
l = new JLabel("Natural Language Processor");
l.setFont(f);
c.weightx = 0.5;
c.gridx = 0;
c.gridy = 0;
c.ipady = 20;
c.fill = GridBagConstraints.NONE;
c.anchor = GridBagConstraints.PAGE_START;
frame.add(l, c);
l1 = new JLabel("English");
l.setFont(f1);
c.weightx = 0.0;
c.weighty = 1.0;
c.gridy = 1;
c.gridx = 0;
//c.ipady = 20;
c.fill = GridBagConstraints.NONE;
c.anchor = GridBagConstraints.PAGE_START;
frame.add(l1, c);
在上面的图片中,我希望标签“English”低于“自然语言处理器”标签的“自然”,而不是居中。
答案 0 :(得分:3)
首先,不要忘记使用SwingUtilities.invokeLater(Runnable r)
调用所有代码来在EDT中运行Swing代码。
有几种方法可以做到这一点,哪一个最好取决于你将在UI上放置什么。这是最简单的方法之一:
方式1
import java.awt.Color;
import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
public class TitleTestFrame extends JFrame
{
public static void main(String[] args)
{
SwingUtilities.invokeLater(new Runnable()
{
public void run()
{
new TitleTestFrame();
}
});
}
public TitleTestFrame()
{
super("frame title");
JLabel titleLabel = new JLabel("This is a centered title.");
JLabel subtitleLabel = new JLabel("Subtitle");
JPanel mainPanel = new JPanel(new GridBagLayout());
GridBagConstraints c = new GridBagConstraints();
c.gridx = 0;
c.gridy = 0;
c.weightx = 0.0; //all components in the row with weightx = 0.0 keeps the components clump together in the center.
//This way the labels will be in the center of the panel.
c.anchor = GridBagConstraints.FIRST_LINE_START; //FIRST_LINE makes the title and subtitle stay at the top of the frame.
//LINE_START make them be glued to the beginning of its column. However because the weightx = 0.0 the column is centered.
mainPanel.add(titleLabel, c);
c.gridy = 1;
c.weighty = 1.0; // this weight must be put in the components under the titles so they don't get far from each other.
mainPanel.add(subtitleLabel, c);
add(mainPanel);
setSize(new Dimension(400,300));
setLocationRelativeTo(null);
setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
setVisible(true);
}
}
结果:
我不太喜欢这种方式,因为它使用weightx = 0.0
来保持标签居中,如果你需要在侧面放置更多的GUI元素,你需要将标签放在中心列中权重x 0.0和每侧的元素,权重x> 0.0。
例如,用以下代码替换上面的代码:
第2天
JPanel mainPanel = new JPanel(new GridBagLayout());
GridBagConstraints c = new GridBagConstraints();
c.gridx = 1;
c.gridy = 0;
c.weightx = 0.0;
c.anchor = GridBagConstraints.FIRST_LINE_START;
mainPanel.add(titleLabel, c);
c.gridy = 1;
mainPanel.add(subtitleLabel, c);
JPanel colorPanel = new JPanel();
colorPanel.setBackground(Color.red);
colorPanel.setOpaque(true);
JPanel colorPanel2 = new JPanel();
colorPanel2.setBackground(Color.gray);
colorPanel2.setOpaque(true);
c.weighty = 1.0;
c.gridy = 0;
c.gridx = 0;
c.weightx = 1.0;
c.gridheight = 2;
c.fill = GridBagConstraints.BOTH;
mainPanel.add(colorPanel, c);
c.gridx = 2;
mainPanel.add(colorPanel2, c);
add(mainPanel);
结果:
修改强>
使用gridbag布局进行良好布局的关键是了解如何创建网格。然后你只需要使用重量和锚来将组件放在每个单元格中。
这是我的2x3网格,在SECOND WAY(没有颜色)。
这是你的2x1网格
使用网格,您无法使用我的网格对齐标签的开头。