我正在使用GridBagLayout
来设计界面。界面在北方设置了一个JTabbedPane
,在我调整大小时填充了两个方向,在JTabbedPane
下方有两个JButton
s。
我想要实现的是将这两个按钮放在东并使用仅 GridBagLayout
功能(不引入额外的JPanel
),但我没有这样做。
| - [标签] -------------------------- |
| --------------------------------- |
| --------------------------------- |
| --------------------------------- |
| --------------------------------- |
| ------------- [按钮] [按钮] |
当我将两个按钮的布局约束设置为WEST(而不是EAST)时。两个都正确地向西走了!
c.anchor = GridBagConstraints.WEST;
但是当我将两个按钮的布局约束设置为东
时 c.anchor = GridBagConstraints.EAST;
其中一人往东走,另一人往西走!
要解决此问题,我添加了JPanel
,其中包含两个按钮并将此JPanel
添加到界面中,并将其布局约束设置为c.anchor = GridBagConstraints.EAST;
。效果很好。
但是,是否可以在不使用额外GridBagLayout
作为容器的情况下构建相同的JPanel
界面?
以下两个类格式为 SSCCE ;第一个显示问题 GridBagTest
,另一个显示使用额外GridBagTest_solved
JPanel
>
问题类:
import javax.swing.*;
import java.awt.*;
public class GridBagTest extends JPanel {
JFrame frame;
JTabbedPane tabbedPane;
JPanel panel_tab;
JButton btn_previous;
JButton btn_next;
private void create_and_layout() {
this.setLayout(new GridBagLayout());
tabbedPane = new JTabbedPane() {
@Override
public Dimension getPreferredSize() {
return new Dimension(300, 150);
}
};
panel_tab = new JPanel();
tabbedPane.addTab("Tab 1", panel_tab);
btn_previous = new JButton("previous");
btn_next = new JButton(" next ");
GridBagConstraints c;
c = new GridBagConstraints();
c.gridx = 0;
c.gridy = 0;
c.weightx = 1;
c.gridwidth = 2;
c.weighty = 1;
c.fill = GridBagConstraints.BOTH;
c.anchor = GridBagConstraints.NORTH;
this.add(tabbedPane, c);
c = new GridBagConstraints();
c.gridx = 0;
c.gridy = 1;
c.anchor = GridBagConstraints.EAST;
c.insets = new Insets(2, 0, 2, 2);
this.add(btn_previous, c);
c = new GridBagConstraints();
c.gridx = 1;
c.gridy = 1;
c.anchor = GridBagConstraints.EAST;
c.insets = new Insets(2, 0, 2, 2);
this.add(btn_next, c);
}
private void initGUI() {
create_and_layout();
frame = new JFrame("GridBagTest");
frame.getContentPane().add(this);
frame.pack();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
public static void main(String[] args) {
java.awt.EventQueue.invokeLater(new GridBagTest()::initGUI);
}
}
解决方案类:
import javax.swing.*;
import java.awt.*;
public class GridBagTest_solved extends JPanel {
JFrame frame;
JTabbedPane tabbedPane;
JPanel panel_tab;
JPanel panel_buttons;
JButton btn_previous;
JButton btn_next;
private void create_and_layout() {
this.setLayout(new GridBagLayout());
tabbedPane = new JTabbedPane() {
@Override
public Dimension getPreferredSize() {
return new Dimension(300, 150);
}
};
panel_tab = new JPanel();
tabbedPane.addTab("Tab 1", panel_tab);
panel_buttons = new JPanel(new GridBagLayout());
btn_previous = new JButton("previous");
btn_next = new JButton(" next ");
GridBagConstraints c;
// Adding buttons to panel_buttons
c = new GridBagConstraints();
c.gridx = 0;
c.gridy = 0;
c.insets = new Insets(2, 0, 2, 2);
panel_buttons.add(btn_previous, c);
c = new GridBagConstraints();
c.gridx = 1;
c.gridy = 0;
c.insets = new Insets(2, 0, 2, 2);
panel_buttons.add(btn_next, c);
// Adding tabbedPane & panel_buttons to this (GridBagTest_solved)
c = new GridBagConstraints();
c.gridx = 0;
c.gridy = 0;
c.weightx = 1;
c.weighty = 1;
c.fill = GridBagConstraints.BOTH;
c.anchor = GridBagConstraints.NORTH;
this.add(tabbedPane, c);
c = new GridBagConstraints();
c.gridx = 0;
c.gridy = 1;
c.anchor = GridBagConstraints.EAST;
this.add(panel_buttons, c);
}
private void initGUI() {
create_and_layout();
frame = new JFrame("GridBagTest_solved");
frame.getContentPane().add(this);
frame.pack();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
public static void main(String[] args) {
java.awt.EventQueue.invokeLater(new GridBagTest_solved()::initGUI);
}
}
答案 0 :(得分:1)
如果你只有三个组件--JTabbedPane和两个JButton,你应该考虑使用比GridBag更简单的布局。只需使用BorderLayout作为主面板,将JTabbedPane放在CENTER中,将一个JPanel放在一个具有TRAILING对齐的FlowLayout的JP中,然后将两个按钮添加到该JPanel。