繁体   English   中英

如何获取扩展 JPanel 以显示在另一个 JPanel 类中的类的实例?

[英]How do I get an instance of a class that extends a JPanel to display in another classes JPanel?

收入面板显示在 IncomeAndSpendingPanel 内,但如果我将所有收入面板代码移动到它自己的类中(就像我使用 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);

但是 IncomeAndSpendingPanel 上也没有任何显示

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 对象时可能会遇到各种问题,因为在初始化期间您有可覆盖的方法调用。

但是,当您添加incomePanelTransactionAdder

  1. 你从来没有设置这些.setVisible(true)
  2. 你永远不会将你的IncomeAndSpendingPanel设置为.setVisible(true)
  3. 您永远不会将IncomeAndSpendingPanel设置为任何大小(使用Dimension类)
  4. 您从未使用过容器层次结构

请让那些包含在 JFrame 中并更改您的代码以使其看起来更像这样:

class IncomeAndSpendingPanel extends JFrame
{
   //Instance variables

   public IncomeAndSpendingPanel()
   {
     //initialize instance variables
     createAndShowGui();
   }

   private void createAndShowGui()
   {
     //Your code
     this.setVisible(true);
   }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM