gpt4 book ai didi

java - 使用 Swing 将 Jpanels 正确添加到 BoxLayout 时遇到问题

转载 作者:行者123 更新时间:2023-12-01 10:48:53 25 4
gpt4 key购买 nike

我正在尝试为我之前编写的程序创建 GUI,但在按照我想要的方式对齐某些面板时遇到一些问题。目前我有这段代码。

package javaapplication10;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.border.EmptyBorder;

public class GUITest extends JFrame {

public static final int WIDTH = 425;
public static final int HEIGHT = 250;

JPanel addPanel;
JComboBox referenceList;
JTextField callNoText;

public static void main(String[] args) {
GUITest gui = new GUITest( );
gui.setVisible(true);
}

public GUITest( )
{
super("Library Search");
setSize(WIDTH, HEIGHT);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLayout(new BoxLayout(getContentPane(), BoxLayout.X_AXIS));

addPanel = new JPanel( );
addPanel.setBackground(Color.WHITE);
addPanel.setVisible(true);
addPanel.setLayout(new BorderLayout());

JPanel info = new JPanel();
info.setBackground(Color.WHITE);
info.setLayout(new BoxLayout(info, BoxLayout.Y_AXIS));

JLabel message_add = new JLabel("Adding a reference.");
message_add.setBorder(new EmptyBorder(10, 10, 0, 0));
message_add.setAlignmentX(Component.LEFT_ALIGNMENT);

JPanel typeSelect = new JPanel(new FlowLayout());
typeSelect.add(new JLabel("Type: "));
String[] references = {"Book", "Journal"};
referenceList = new JComboBox(references);
typeSelect.add(referenceList);
typeSelect.setAlignmentX(Component.LEFT_ALIGNMENT);

JPanel callNoSelect = new JPanel();
callNoText = new JTextField(20);
callNoSelect.add(new JLabel("Call No:\t"));
callNoSelect.add(callNoText);
callNoSelect.setAlignmentX( Component.LEFT_ALIGNMENT );

info.add(message_add);
info.add(typeSelect);
info.add(callNoSelect);

addPanel.add(info, BorderLayout.CENTER);
add(addPanel);

JMenu commandMenu = new JMenu("Commands");
JMenuItem addChoice = new JMenuItem("Add");
commandMenu.add(addChoice);
JMenuItem searchChoice = new JMenuItem("Search");
commandMenu.add(searchChoice);
JMenuItem quitChoice = new JMenuItem("Quit");
commandMenu.add(quitChoice);

JMenuBar bar = new JMenuBar( );
bar.add(commandMenu);
setJMenuBar(bar);
}

}

这将创建一个如下所示的 GUI:Current GUI

如您所见,两个面板 typeSelectcallNoSelect 已添加到 addPanel 中,但它们看起来与标签 不同message_add 我在它们之前添加的。我已经测试过添加另一个标签,它看起来像我想要的那样,但由于某种原因我无法让面板执行相同的操作。我希望页面看起来像这样:

Correct GUI *

另外,我将这一切都放在 addPanel 中的原因是,我将有其他想要向用户显示的面板,我将根据需要关闭和打开这些面板的可见性。我将 addPanel 作为 BorderLayout 的原因是,一旦我弄清楚如何按照我想要的方式获得中心,我最终将在东部和南部象限拥有更多控件。

*我在绘画中创建了正确的 GUI,以尝试更好地展示我想要的内容

最佳答案

JLabel 是透明的,而 JPanel 则不是。因此,JLabel 显示 info 面板的背景色,即白色,而 typeSelectcallNoText使用自己的默认颜色。您可以使用 setOpaque(false) 使这些其他面板透明,或更改其背景颜色以匹配 info 面板

至于您的布局,您可以考虑使用更灵活的东西,例如GridBagLayout,请参阅How to Use GridBagLayoutLaying Out Components Within a Container了解更多详情

关于java - 使用 Swing 将 Jpanels 正确添加到 BoxLayout 时遇到问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34032431/

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