如何获取扩展 JPanel 以显示在另一个 classes JPanel 中的 class 实例?
How do I get an instance of a class that extends a JPanel to display in another classes JPanel?
incomePanel 显示在 IncomeAndSpendingPanel 内部,但如果我将所有 incomePanel 代码移动到它自己的 class(就像我对 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 构建的 class,当我尝试将它添加到 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。
此外,由于您正在使用继承,因此您肯定需要将所有这些基本 class 方法调用移动到一个方法中,并且仅使用构造函数来初始化实例变量。如果你不这样做,你可能会在创建任何 JPanel 对象时遇到各种问题,因为你在初始化期间有可覆盖的方法调用。
但是,当您添加 incomePanel
和 TransactionAdder
- 您从未设置过其中任何一个
.setVisible(true)
- 您从未将
IncomeAndSpendingPanel
设置为 .setVisible(true)
- 您从未将
IncomeAndSpendingPanel
设置为任何大小(使用 Dimension
class)
- 您从未使用过容器hierarchy
请将它们包含在 JFrame 中并将您的代码更改为更像这样:
class IncomeAndSpendingPanel extends JFrame
{
//Instance variables
public IncomeAndSpendingPanel()
{
//initialize instance variables
createAndShowGui();
}
private void createAndShowGui()
{
//Your code
this.setVisible(true);
}
incomePanel 显示在 IncomeAndSpendingPanel 内部,但如果我将所有 incomePanel 代码移动到它自己的 class(就像我对 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 构建的 class,当我尝试将它添加到 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。
此外,由于您正在使用继承,因此您肯定需要将所有这些基本 class 方法调用移动到一个方法中,并且仅使用构造函数来初始化实例变量。如果你不这样做,你可能会在创建任何 JPanel 对象时遇到各种问题,因为你在初始化期间有可覆盖的方法调用。
但是,当您添加 incomePanel
和 TransactionAdder
- 您从未设置过其中任何一个
.setVisible(true)
- 您从未将
IncomeAndSpendingPanel
设置为.setVisible(true)
- 您从未将
IncomeAndSpendingPanel
设置为任何大小(使用Dimension
class) - 您从未使用过容器hierarchy
请将它们包含在 JFrame 中并将您的代码更改为更像这样:
class IncomeAndSpendingPanel extends JFrame
{
//Instance variables
public IncomeAndSpendingPanel()
{
//initialize instance variables
createAndShowGui();
}
private void createAndShowGui()
{
//Your code
this.setVisible(true);
}