简体   繁体   English

将一个JPanel设置为另一个类中的新JPanel

[英]Setting a JPanel as a new JPanel from another class

I have a main Java GUI which looks like this . 我有一个看起来像这样的主Java GUI。

在此处输入图片说明

Basically there is a containerPanel which contains a cardPanel of cards (the right hand side), and then a GridLayout tabPanel on the left hand side. 基本上有一个containerPanel,其中包含cards的cardPanel(右侧),然后是左侧的GridLayout tabPanel。

The cardPanel contains many cards, one of which is a patientCard. cardPanel包含许多卡,其中之一是PatientCard。 This basically shows a JPanel which contains a JTable of patients. 这基本上显示了包含患者JTable的JPanel。

I want to call code like: 我想像这样调用代码:

cards = new CardLayout(); 
cardPanel = new JPanel();
containerPanel.add(cardPanel, BorderLayout.CENTER);
cardPanel.setLayout(cards); 
cards.show(cardPanel, null);

patientsCard = new CSVTable();
cardPanel.add(patientsCard, "View Patients Panel");

And then call it to view via: 然后调用它以通过以下方式查看:

viewButton = new JButton("View Patients"); 
        tabsPanel.add(viewButton);
        viewButton.addActionListener(new ActionListener() { 
            @Override public void actionPerformed(ActionEvent event) {
                cards.show(cardPanel,"View Patients Panel");
            } 
        });

Not that this code actually works for my registration panel which is not called from another class, but just like: 并不是该代码实际上适用于我的注册面板,而该注册面板不是从另一个类调用的,而是像:

registrationCard = new JPanel()

For some reason, this CSVTable panel when called from within the main GUI is not displaying at all, even if I do simple things like change the background. 由于某些原因,即使我做一些简单的事情(例如更改背景),从主GUI中调用时,此CSVTable面板也根本不会显示。 I have tried various steps including trying to include a new Runnable in CSVTable in the try method, changing the type of patientCard to type CSVTable object, changing the variables to public , putting the CSVTable() code in a public static void main(String[] args) method rather than a constructor. 我尝试了各种步骤,包括尝试在try方法中的CSVTable中包含新的Runnable ,将CSVTable的类型patientCardCSVTable对象的类型,将变量更改为public ,将CSVTable()代码放入public static void main(String[] args) method而不是构造函数。

However, I know the CSVTable code works, because I tried creating a new swing application window via the windowbuilder with exactly the same code (but just with a JFrame included, not just a JPanel), and it runs and looks like this . 但是,我知道CSVTable代码可以工作,因为我尝试通过windowbuilder使用完全相同的代码(但仅包含一个JFrame,而不仅仅是一个JPanel)通过windowbuilder创建一个新的swing应用程序窗口,它可以运行并看起来像这样

在此处输入图片说明

For info, here is my CSVTable() class code: 有关信息,这是我的CSVTable()类代码:

package hospitalsystem;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.io.FileReader;
import java.io.Reader;
import java.util.Arrays;
import java.util.Scanner;
import java.util.Vector;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextPane;
import javax.swing.table.DefaultTableModel;


public class CSVTable extends JPanel {

    /*
     * These are all static because I initially had these being referred to in a main class in CSVTable
     */
    public static String datafile;
    public static DefaultTableModel m;
    public static JPanel viewCard;
    public static FileReader fin;
    public static JScrollPane patientScrollPane;
    public static JTable patientTable; 
    public static Dimension patientPaneDimension;


     public DefaultTableModel createTableModel(Reader in, Vector<Object> headers) {
            DefaultTableModel model = null;
            Scanner s = null;
            Vector<Object> c;
            try {
                Vector<Vector<Object>> rows = new Vector<Vector<Object>>();
                s = new Scanner(in);
                while (s.hasNextLine()) {
                    rows.add(new Vector<Object>(
                            Arrays.asList(s.nextLine().split("\\s*,\\s*", -1))));
                }
                if (headers == null) {
                    headers = rows.remove(0);
                    model = new DefaultTableModel(rows, headers);
                } else {
                    model = new DefaultTableModel(rows, headers);
                }
                return model;
            } finally {
                s.close();
            }
        }


    public CSVTable(){
        try {    
            datafile = "[my file location, kept private]";
            fin = new FileReader(datafile);
            m = createTableModel(fin, null);//This uses the method above
            System.out.println(m);
            viewCard = new JPanel();
            viewCard.setLayout(new BorderLayout());
            viewCard.setBackground(Color.BLACK);//This was not being called at all
            patientTable = new JTable(m);
            patientTable.setPreferredScrollableViewportSize(new Dimension(700, 70));
            patientTable.setFillsViewportHeight(true);
            patientScrollPane = new JScrollPane(patientTable);
            patientScrollPane.setBackground(Color.WHITE);
            patientScrollPane.setOpaque(true);
            patientTable.setBackground(Color.WHITE);
            viewCard.add(patientScrollPane, BorderLayout.CENTER);
            viewCard.add(patientTable, BorderLayout.CENTER);
            viewCard.setVisible(true);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}

I have worked this out myself. 我自己解决了这个问题。

I was calling 我在打电话

patientsCard = new CSVTable();

Which should generate a new JPanel automatically. 哪个应该自动生成一个新的JPanel However, within the CSVTable class, I also generated a new JPanel via: 但是,在CSVTable类中,我还通过以下方式生成了一个新的JPanel:

viewCard = new JPanel();

When I remove all references to viewCard , this new JPanel, in the CSVTable class, my panel correctly renders in the main GUI. 当我删除所有新的JPanel对viewCard引用时,在CSVTable类中,我的面板会在主GUI中正确呈现。

Hope this helps others in the same situation. 希望这可以帮助处于相同情况的其他人。

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

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