gpt4 book ai didi

java - 是否可以构建相同的 GridBagLayout 界面但不使用额外的 JPanel 作为容器?

转载 作者:行者123 更新时间:2023-12-01 09:31:02 25 4
gpt4 key购买 nike

我正在使用GridBagLayout来设计界面。接口(interface)有一个JTabbedPane设置为北并在我调整大小时填充两个方向,就在 JTabbedPane 的下方有两个JButton s。

我想要实现的是将这两个按钮放在处,并使用GridBagLayout功能(没有引入额外的 JPanel ),但我没有这样做。

|-[选项卡]--------------------------|
|--------------------------------|
|--------------------------------|
|--------------------------------|
|--------------------------------|
|-------------[按钮][按钮]|

当我将两个按钮的布局约束设置为“西”(而不是“东”)时。两者都正确地向西!

c.anchor = GridBagConstraints.WEST;

但是当我将两个按钮的布局约束设置为东时

c.anchor = GridBagConstraints.EAST;

其中一个向东,另一个向西!

为了解决这个问题,我添加了 JPanel按住两个按钮并添加此 JPanel进入界面,并将其布局约束设置为 c.anchor = GridBagConstraints.EAST; 。效果很好。

但是是否可以构建相同的 GridBagLayout无需使用额外的接口(interface)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);
}
}

最佳答案

如果您只有三个组件 - JTabbedPane 和两个 JButton,您应该考虑使用比 GridBag 更简单的布局。只需使用主面板的 BorderLayout,将 JTabbedPane 放置在 CENTER 中,将 JPanel 放置在 SOUTH 中,该 JPanel 具有 TRAILING 对齐的 FlowLayout,然后将两个按钮添加到该 JPanel 中。

关于java - 是否可以构建相同的 GridBagLayout 界面但不使用额外的 JPanel 作为容器?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39401007/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com