Java 如何获得一个类的实例,该类扩展了一个JPanel以显示在另一个类JPanel中?
incomePanel显示在IncomeAndPendingPanel的内部,但如果我将所有incomePanel代码移动到它自己的类中(就像我使用SpendingPanel和TransactionAdder一样),并尝试将它们添加到IncomeAndPendingPanel中,则不会显示任何内容。我只是想整理一下我的代码,把它分成不同的部分Java 如何获得一个类的实例,该类扩展了一个JPanel以显示在另一个类JPanel中?,java,swing,jpanel,Java,Swing,Jpanel,incomePanel显示在IncomeAndPendingPanel的内部,但如果我将所有incomePanel代码移动到它自己的类中(就像我使用SpendingPanel和TransactionAdder一样),并尝试将它们添加到IncomeAndPendingPanel中,则不会显示任何内容。我只是想整理一下我的代码,把它分成不同的部分 public class IncomeAndSpendingPanel extends JPanel { private ColorX cx =
public class IncomeAndSpendingPanel extends JPanel {
private ColorX cx = new ColorX();
DefaultTableModel incomeTableModel;
DefaultTableModel spendingTableModel;
private String[] incomeColumnHeadings = {"Date","Description","Amount"};
private String[] spendingColumnHeadings = {"Date","Description","Amount","Category"};
Border empty = BorderFactory.createEmptyBorder(10, 10, 10, 10);
Border blackLine = BorderFactory.createLineBorder(Color.black);
CompoundBorder line = new CompoundBorder(empty, blackLine);
public IncomeAndSpendingPanel() {
setLayout(new BorderLayout());
add(new TransactionAdder(), BorderLayout.NORTH);
add(new SpendingPanel(), BorderLayout.EAST);
JPanel incomePanel;
Border incomePanelBorder = BorderFactory.createTitledBorder(line, "INCOME");
JTable incomeTransactionInputTable;
incomePanel = new JPanel(new GridLayout());
incomePanel.setBorder(incomePanelBorder);
incomeTableModel = new DefaultTableModel(50, incomeColumnHeadings.length);
incomeTableModel.setColumnIdentifiers(incomeColumnHeadings);
incomeTransactionInputTable = new JTable(incomeTableModel);
incomeTransactionInputTable.getColumnModel().getColumn(0).setPreferredWidth(1);
incomeTransactionInputTable.getColumnModel().getColumn(1).setPreferredWidth(200);
incomeTransactionInputTable.getColumnModel().getColumn(2).setPreferredWidth(10);
incomePanel.add(new JScrollPane(incomeTransactionInputTable));
add(incomePanel, BorderLayout.CENTER);
}
这是我为TransactionAdder构建的类,当我尝试将其添加到IncomeandPendingPanel时,没有显示任何内容,我尝试创建TransactionAdder TransactionAdder=new TransactionAdder();然后像这样添加TransactionAdder的实例
add(transactionAdder, BorderLayout.NORTH);
但IncomeandPendingPanel上也不会以这种方式显示任何内容
public class TransactionAdder extends JPanel {
DefaultTableModel model3;
DefaultTableModel model4;
private String[] incomeColumnHeadings = {"Date","Description","Amount"};
private String[] spendingColumnHeadings = {"Date","Description","Amount","Category"};
Border empty = BorderFactory.createEmptyBorder(10, 10, 10, 10);
Border blackLine = BorderFactory.createLineBorder(Color.black);
CompoundBorder line = new CompoundBorder(empty, blackLine);
JTable incomeTransactionTable;
JTable spendingTransactionTable;
JPanel transactionAdderPanel;
Border transactionAdderPanelBorder = BorderFactory.createTitledBorder(line, "ADD TRANSACTION");
JScrollPane jScrollPane;
JScrollPane jScrollPane1;
public TransactionAdder() {
transactionAdderPanel = new JPanel(new GridLayout());
transactionAdderPanel.setBorder(transactionAdderPanelBorder);
model3 = new DefaultTableModel(1, incomeColumnHeadings.length);
model3.setColumnIdentifiers(incomeColumnHeadings);
model4 = new DefaultTableModel(1, spendingColumnHeadings.length);
model4.setColumnIdentifiers(spendingColumnHeadings);
transactionAdderPanel.add(new JButton("Add New Income"));
incomeTransactionTable = new JTable(model3);
jScrollPane = new JScrollPane(incomeTransactionTable);
jScrollPane.setPreferredSize(new Dimension(300, 38));
transactionAdderPanel.add(jScrollPane);
transactionAdderPanel.add(new JButton("Add New Spending"));
spendingTransactionTable = new JTable(model4);
jScrollPane1 = new JScrollPane(spendingTransactionTable);
jScrollPane1.setPreferredSize(new Dimension(300, 38));
transactionAdderPanel.add(jScrollPane1);
}
}
这里有很多要说的…首先,您可能需要使用JFrame,所以请阅读关于顶级容器的内容 另外,因为您使用的是继承,所以您肯定需要将所有这些基类方法调用移动到一个方法,并且只使用构造函数初始化实例变量。如果不这样做,则在创建任何JPanel对象时可能会出现各种问题,因为在初始化期间有可重写的方法调用 但是,当您添加
incomePanel
和TransactionAdder
IncomeandPendingPanel
设置为.setVisible(true)
IncomeAndSpendingPanel
设置为任何大小(使用维度
类)class IncomeAndSpendingPanel extends JFrame
{
//Instance variables
public IncomeAndSpendingPanel()
{
//initialize instance variables
createAndShowGui();
}
private void createAndShowGui()
{
//Your code
this.setVisible(true);
}
很抱歉,我还不熟悉堆栈溢出,我想我的问题可能没有什么背景。这是我正在进行的一个更大预算项目中的一小段类代码。我在另一个类中有一个JFrame,这些面板被添加到这个类中。我猜最终的结果是我需要添加实际的面板(transactionAdderPanel),而不是尝试添加该类的新实例。它现在显示。谢谢你的建议!