gpt4 book ai didi

java - 将一个 JPanel 添加到另一个 JPanel

转载 作者:行者123 更新时间:2023-11-30 03:36:46 24 4
gpt4 key购买 nike

我想将多个 JPanel 添加到一个 JPanel。然后我想获取该 JPanel 并将其显示在 JTabbedPane 的选项卡中。我尝试了几次,但 Eclipse 不允许我运行该程序。它没有显示任何错误,但由于某种原因程序始终在 Debug模式下运行。是不是代码有问题? (代码有点长抱歉)

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.Serializable;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTabbedPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.ScrollPaneConstants;
import javax.swing.UIManager;
import javax.swing.UIManager.LookAndFeelInfo;

public class GuiApp {
static class shelf implements Serializable {

// declare books and variables
static shelf[] book = new shelf[1000];
Boolean overdue;
Boolean checkedOut;
int bookNum;
String personName;
String dueDate;
int month;
int date;
int year;
String dateCheckedOut;
String bookName;
}

public static void main(String args[]) {
// set L&F
try {
for (LookAndFeelInfo info : UIManager.getInstalledLookAndFeels()) {
if ("Nimbus".equals(info.getName())) {
UIManager.setLookAndFeel(info.getClassName());
break;
}
}
} catch (Exception e) {

}
JFrame main = new JFrame();
JTabbedPane tabs = new JTabbedPane();
JPanel checkOutPanel = new JPanel(new GridBagLayout());
JPanel checkInPanel = new JPanel(new GridBagLayout());
JPanel checkedOutOverduePanel = new JPanel(new BorderLayout());
JPanel assignNamesPanel = new JPanel(new BorderLayout());
JPanel refreshSavePanel = new JPanel(new GridBagLayout());

//misc
Font f = new Font("Header", Font.BOLD, 24);

GridBagConstraints gbc = new GridBagConstraints();

//check out
JLabel checkOutLabel = new JLabel("CheckOut");
JLabel bookNumLabel = new JLabel("Book Number");
JLabel personNameLabel = new JLabel("Person Name");

final JTextField bookNumEntry = new JTextField(20);
final JTextField personNameEntry = new JTextField(20);
JButton checkOutButton = new JButton("Check out");

checkOutLabel.setFont(f);

gbc.gridx = 1;
checkOutPanel.add(checkOutLabel,gbc);
gbc.gridx = 0;
gbc.gridy = 1;
checkOutPanel.add(bookNumLabel,gbc);
gbc.gridx = 1;
checkOutPanel.add(bookNumEntry,gbc);
gbc.gridx = 0;
gbc.gridy = 2;
checkOutPanel.add(personNameLabel,gbc);
gbc.gridx = 1;
checkOutPanel.add(personNameEntry,gbc);
gbc.gridx = 2;
checkOutPanel.add(checkOutPanel,gbc);
tabs.addTab("Check Out", checkOutPanel);


//check in
JLabel checkInLabel = new JLabel("Check In");
JLabel bookNumCheckInLabel = new JLabel("Book Number");
final JTextField bookNumCheckIn = new JTextField(20);
JButton checkInButton = new JButton("Check In");

checkInLabel.setFont(f);

gbc.gridx = 1;
gbc.gridy = 0;
checkInPanel.add(checkInLabel,gbc);
gbc.gridx = 0;
gbc.gridy = 1;
checkInPanel.add(bookNumCheckInLabel,gbc);
gbc.gridx = 1;
checkInPanel.add(bookNumCheckIn,gbc);
gbc.gridy = 2;
checkInPanel.add(checkInButton,gbc);
tabs.addTab("Check In",checkInPanel);


//checked out overdue
JPanel co = new JPanel(new GridBagLayout());
JPanel co2 = new JPanel(new GridBagLayout());
JPanel co3 = new JPanel();

JLabel booksOutOverdueLabel = new JLabel("BOOKS OUT/OVERDUE");
JLabel checkedOutLabel = new JLabel("Checked out");
JLabel overdueLabel = new JLabel("Overdue");

booksOutOverdueLabel.setFont(f);

JTextArea checkedOutBooks = new JTextArea(50,53);
JTextArea overdueBooks = new JTextArea(50,48);
JScrollPane checkedOutTA = new JScrollPane(checkedOutBooks);
JScrollPane overdueTA = new JScrollPane(overdueBooks);
checkedOutTA.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
overdueTA.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
checkedOutBooks.setEditable(false);
overdueBooks.setEditable(false);

gbc.gridy = 0;
co.add(checkedOutLabel,gbc);
gbc.gridy = 1;
co.add(checkedOutTA,gbc);
gbc.gridy = 0;
co2.add(overdueLabel,gbc);
gbc.gridy = 1;
co2.add(overdueTA,gbc);

co3.add(booksOutOverdueLabel);

checkedOutOverduePanel.add(co3,BorderLayout.NORTH);
checkedOutOverduePanel.add(co,BorderLayout.WEST);
checkedOutOverduePanel.add(co2,BorderLayout.EAST);

tabs.addTab("Checked Out/Overdue",checkedOutOverduePanel);


//assign book names
JPanel an = new JPanel(new GridBagLayout());
JPanel an2 = new JPanel();

JLabel assignNamesLabel = new JLabel("Assign Book Names");
JLabel bookNumberLabel = new JLabel("Book Number");
JLabel nameOfBookLabel = new JLabel("Book Name");
final JTextField bookNumber = new JTextField(20);
final JTextField bookName = new JTextField(20);
JButton assignName = new JButton("Assign");

assignNamesLabel.setFont(f);

gbc.gridy = 0;
an.add(bookNumberLabel,gbc);
an.add(bookNumber,gbc);
gbc.gridy = 1;
an.add(nameOfBookLabel,gbc);
an.add(bookName,gbc);
gbc.gridy = 2;
gbc.gridx = 1;
an.add(assignName,gbc);

an2.add(assignNamesLabel);

assignNamesPanel.add(an2,BorderLayout.NORTH);
assignNamesPanel.add(an,BorderLayout.CENTER);

tabs.addTab("Assign Book Names",assignNamesPanel);

//refresh and save
JButton saveButton = new JButton("Save");
JButton refreshButton = new JButton("Refresh");
refreshSavePanel.add(saveButton);
refreshSavePanel.add(refreshButton);
tabs.addTab("Refresh/save",refreshSavePanel);

tabs.setTabPlacement(JTabbedPane.LEFT);

main.add(tabs);
main.setSize(1300,1100);
main.setVisible(true);

}
}

最佳答案

这是您将面板添加到自身时遇到的问题。将容器的父级添加到自身

checkOutPanel.add(checkOutPanel,gbc);//error
tabs.addTab("Check Out", checkOutPanel);

您应该将组件添加到容器。但是您正在将容器添加到容器。可能您正在尝试添加一个按钮。

checkOutPanel.add(checkOutButton,gbc);
tabs.addTab("Check Out", checkOutPanel);

关于java - 将一个 JPanel 添加到另一个 JPanel,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27718294/

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