incomePanel显示在IncomeAndSpendingPanel内部,但是如果我将所有IncomePanel代码移到它自己的类中(就像我使用SpendingPanel和TransactionAdder所拥有的一样),然后尝试将它们添加到IncomeAndSpendingPanel中,则不会显示任何内容。我只是想清理我的代码并将其分成不同的部分。
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构建的类,当我尝试将其添加到IncomeAndSpendingPanel时没有任何显示,我尝试创建TransactionAdder transactionAdder = new TransactionAdder();。然后像这样添加TransactionAdder的实例
add(transactionAdder, BorderLayout.NORTH);
但是收入或支出面板上没有任何显示方式
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,因此请阅读有关顶级容器here的信息。
同样,由于您正在使用继承,因此您肯定需要将所有这些基类方法调用移至某个方法,并且仅使用构造函数来初始化实例变量。如果不这样做,那么在创建任何JPanel对象时可能会遇到各种各样的问题,因为在初始化期间有可重写的方法调用。
但是,当您添加incomePanel
和TransactionAdder
您从未设置任何一个.setVisible(true)
您从未将IncomeAndSpendingPanel
设置为.setVisible(true)
永远不要将IncomeAndSpendingPanel
设置为任何大小(使用Dimension
类)
您从未使用过容器hierarchy
请制作包含在JFrame中的代码,然后更改代码,使其看起来像这样:
class IncomeAndSpendingPanel extends JFrame
{
//Instance variables
public IncomeAndSpendingPanel()
{
//initialize instance variables
createAndShowGui();
}
private void createAndShowGui()
{
//Your code
this.setVisible(true);
}