gpt4 book ai didi

java - 组件未显示在 JFrame 中

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

每当我运行此代码时,JFrame 都会显示,但我的任何组件都不会出现。我以前做过一些GUI项目,但以前从未遇到过这个问题。

public class LibraryGUI {

int x;
Library library;
ActionListener AL = null;
BorderLayout mainLayout = new BorderLayout();
GridLayout buttonLayout = new GridLayout(1, 4, 30, 30);
GridLayout entryLayout = new GridLayout(2, 2, 1, 30);
// below items will be in the main JFrame, where the library will be
// displayed
static JFrame mainLibrary;
static JButton addButton = new JButton("Add an Item");
static JButton checkInButton = new JButton("Check in an Item");
static JButton checkOutButton = new JButton("Check out an Item");
static JButton saveButton = new JButton("Save current library");
static JList<Item> list;
// below items will be in the "Add Item" JFrame
static JFrame newItemFrame;
static JButton addButton1 = new JButton("Add this Item");
static JTextField titleJTF = new JTextField("Title of media");
static JTextField formatJTF = new JTextField("Format of media");
static JLabel titleJL = new JLabel("Title:");
static JLabel formatJL = new JLabel("Format:");
// below items will be in the "Check Out" JFrame
static JFrame checkOutFrame;
static JButton checkOutButton1 = new JButton("Check Out");
static JTextField nameJTF = new JTextField("Name of Person");
static JTextField dateJTF = new JTextField("Date of Check out");
static JLabel nameJL = new JLabel("Name:");
static JLabel dateJL = new JLabel("Date:");
static JLabel titleJL1 = new JLabel("");
// below items will be in the "Delete Confirmation" JFrame
static JFrame deleteFrame;
static JButton confirmButton = new JButton("Delete this Item");
static JButton cancelDeleteButton = new JButton("Don't delete this Item");
static JLabel confirmationJL = new JLabel();

private void mainLibraryFrame() {

mainLibrary = new JFrame("Lending Library");
mainLibrary.setLayout(mainLayout);
mainLibrary.setSize(500, 600);
addButton.setActionCommand("open add");
addButton.addActionListener(AL);
checkInButton.setActionCommand("check in");
checkInButton.addActionListener(AL);
checkOutButton.setActionCommand("open check out");
checkOutButton.addActionListener(AL);
saveButton.setActionCommand("save");
saveButton.addActionListener(AL);
JPanel buttonHolder = new JPanel(buttonLayout);
buttonHolder.add(addButton);
buttonHolder.add(checkInButton);
buttonHolder.add(checkOutButton);
buttonHolder.add(saveButton);
JPanel overViewHolder = new JPanel(mainLayout);
mainLibrary.add(list, BorderLayout.NORTH);
mainLibrary.add(buttonHolder, BorderLayout.SOUTH);
mainLibrary.setLocation(400, 200);
}

private void newItemFrame() {

newItemFrame = new JFrame("Add Item");
newItemFrame.setSize(200, 200);
newItemFrame.setLocation(500, 300);
addButton1.setActionCommand("add");
addButton1.setSize(50, 30);
addButton1.addActionListener(AL);
JPanel newItemPanel = new JPanel(mainLayout);
addButton1.setActionCommand("create");
addButton1.setSize(50, 30);
addButton1.addActionListener(AL);
JPanel entryPanel = new JPanel(entryLayout);
entryPanel.add(titleJL);
entryPanel.add(titleJTF);
entryPanel.add(formatJL);
entryPanel.add(formatJTF);
newItemPanel.add(entryPanel, BorderLayout.NORTH);
newItemPanel.add(addButton1, BorderLayout.SOUTH);
newItemFrame.add(newItemPanel);
}

private void checkOutFrame() {

checkOutFrame = new JFrame("Check Out");
checkOutFrame.setSize(200, 200);
checkOutFrame.setLocation(500, 300);
JPanel checkOutEntryPanel = new JPanel(entryLayout);
checkOutEntryPanel.add(nameJL);
checkOutEntryPanel.add(nameJTF);
checkOutEntryPanel.add(dateJL);
checkOutEntryPanel.add(dateJTF);
JPanel checkOutMainPanel = new JPanel(mainLayout);
checkOutMainPanel.add(checkOutEntryPanel, BorderLayout.NORTH);
checkOutMainPanel.add(checkOutButton1, BorderLayout.SOUTH);
}

private void deleteFrame() {

JPanel deletePanel = new JPanel(mainLayout);
deletePanel.add(confirmationJL, BorderLayout.NORTH);
deletePanel.add(confirmButton, BorderLayout.SOUTH);
}

public LibraryGUI(Library library) {

this.library = library;
list = new JList(this.library.container.toArray());
mainLibraryFrame();
newItemFrame();
checkOutFrame();
deleteFrame();
mainLibrary.setVisible(true);
}
}

我一直在检查这段代码,但找不到任何可以 .setVisible(false) 的内容,并且我尝试对所有内容执行 .setVisible(true) ,但似乎没有任何作用。

最佳答案

没有显示任何内容,因为您没有在主框架中为 BorderLayout 指定 CENTER 组件。 BorderLayout 相对于中心组件放置侧面组件,如果您不指定中心组件,您将看不到任何内容。

话虽如此,您所做的许多事情看起来并不好(无论是视觉上还是代码方面)。首先制作一个草图(即使是在“画图”中),将您想要的内容清晰地​​划分并标有它们需要包含的组件的部分。然后按照这个方法工作,一点一点地我们可以让它工作得更好。

至于您正在创建的大量字段:仅当您需要保留字段的引用以供以后使用时才声明字段。例如,大多数标签可能不需要是字段,只需局部变量即可。

此外,正如评论中提到的,您不需要那么多框架,但在我理解您想要实现的目标之前,我无法说出您需要更改什么。

关于java - 组件未显示在 JFrame 中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23458661/

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