Java Swing:Can';t显示我的JPanel的几个实例

Java Swing:Can';t显示我的JPanel的几个实例,java,swing,jpanel,Java,Swing,Jpanel,我正在做一个GUI,在使用JPanel时遇到了麻烦 首先是我的JPanel: public class ExperimentPanel extends JPanel{ private static File file1,file2=null; private static DefaultListModel model = new DefaultListModel(); private static JList list = new JList(model); private static

我正在做一个GUI,在使用JPanel时遇到了麻烦

首先是我的JPanel:

public class ExperimentPanel extends JPanel{

private static File file1,file2=null;

private static DefaultListModel model = new DefaultListModel();
private static JList list = new JList(model);

private static JPanel mainpanel = new JPanel();
private static JPanel leftpanel = new JPanel();
private static JPanel rightpanel = new JPanel();
private static JPanel twoFiles = new SelectTwoFiles();
private static JPanel folderOrFile = new SelectFolderOrFile();
private static JPanel foldersOrFiles = new SelectTwoFoldersOrFiles();

public ExperimentPanel(int selectID){

    this.setBorder(new EmptyBorder(10, 10, 10, 10));

    if(selectID==Constants.SelectTwoFiles){
    this.add(twoFiles, BorderLayout.NORTH);
    }
    else if(selectID==Constants.SelectFolderOrFile){
    this.add(folderOrFile, BorderLayout.NORTH);
    }
    else if(selectID==Constants.SelectTwoFoldersOrFiles){
    this.add(foldersOrFiles,BorderLayout.NORTH);
    }

    JButton remove =new JButton("Remove Method");
    JButton add = new JButton("Add Method");
    JButton save = new JButton("Save list");
    JButton load = new JButton("Load list");

    leftpanel.add(new JScrollPane(list));
    Box listOptions = Box.createVerticalBox();
    listOptions.add(add);
    listOptions.add(remove);
    listOptions.add(save);
    listOptions.add(load);

    rightpanel.add(listOptions);

    Box mainBox = Box.createHorizontalBox();

    mainBox.add(leftpanel);
    mainBox.add(rightpanel);
    //mainBox.add(leftleft);

    this.add(mainBox, BorderLayout.CENTER);

    //start jobs
    JButton start = new JButton("Launch experiment");
    this.add(start,BorderLayout.PAGE_END);

    start.addActionListener(launch);
    add.addActionListener(adding);
    remove.addActionListener(delete);
}

public static ActionListener launch = new ActionListener(){
      public void actionPerformed(ActionEvent event){
          //check the files
          if((file1==null)||(file2==null)){
              JOptionPane.showMessageDialog(null,
                        "A graph file is missing",
                        "Wrong files",
                        JOptionPane.ERROR_MESSAGE);
          }

            //checks the list
      }
};

public static ActionListener delete = new ActionListener() {
      public void actionPerformed(ActionEvent event) {
        ListSelectionModel selmodel = list.getSelectionModel();
        int index = selmodel.getMinSelectionIndex();
        if (index >= 0)
          model.remove(index);
      }
 };

public static ActionListener adding = new ActionListener(){
      public void actionPerformed(ActionEvent event){

          JComboBox combo = new JComboBox();
          final JPanel cards = new JPanel(new CardLayout());

          JPanel form = new JPanel();
          JPanel methode1 = new JPanel();
          methode1.add(new JLabel("meth1"));
          methode1.setBackground(Color.BLUE);
          methode1.setName("meth1");
          JPanel methode2 = new JPanel();
          methode2.add(new JLabel("meth2"));
          methode2.setBackground(Color.GREEN);
          methode1.setName("meth2");
          combo.addItem("meth1");

          combo.addItem("meth2");
          cards.add(methode1,"meth1");
          cards.add(methode2,"meth2");

          JPanel control = new JPanel();
          combo.addActionListener(new ActionListener() {

                public void actionPerformed(ActionEvent e) {
                    JComboBox jcb = (JComboBox) e.getSource();

                    CardLayout cl = (CardLayout) cards.getLayout();
                    cl.show(cards, jcb.getSelectedItem().toString());
                }
            });
            control.add(combo);

            form.add(cards, BorderLayout.CENTER);
            form.add(control, BorderLayout.SOUTH);

            JOptionPane.showMessageDialog(null,form,"Select a method",JOptionPane.PLAIN_MESSAGE);               
      }
};
}
问题是,如果我创建了该面板的多个实例,它们将不会像预期的那样显示

我试着在main中创建两个简单的JFrames,每个JFrames都有一个新的实验面板,这样问题就不是来自调用者了

它在一个JFrame调用一个experiementPanel时运行良好

以下是一次和两次通话的显示:

我如何称呼他们:

    JFrame test = new JFrame();
    test.add(new ExperimentPanel(3));
    Dimension dim = Toolkit.getDefaultToolkit().getScreenSize();
    test.setLocation(dim.width/3 - test.getWidth()/3, dim.height/3 - test.getHeight()/3);
    test.setSize(550,300);
    test.setVisible(true);

    JFrame test2 = new JFrame();
    test2.add(new ExperimentPanel(3));
    test2.setLocation(dim.width/3 - test.getWidth()/3, dim.height/3 - test.getHeight()/3);
    test2.setSize(550,300);
    test2.setVisible(true);

您可以创建一个面板类
ExperimentPanel
,它本身由几个组件组成,这些组件存储在
ExperimentPanel
的类字段中

由于您将这些类字段声明为静态字段,因此它们只有一个实例。当您实例化几个
ExperimentPanel
对象时,它们都希望共享这些字段,从而产生您看到的效果

因此,请从这些字段中删除静态修改器:

public class ExperimentPanel extends JPanel{
    private File file1,file2=null;
    private DefaultListModel model = new DefaultListModel();
    private JList list = new JList(model);
    private JPanel mainpanel = new JPanel();
    private JPanel leftpanel = new JPanel();
    ...

您是否使用任何布局来组织您的gui?这里没有,我尝试使用cardLayout在面板之间切换,但问题仍然与我所想的相同,但忘记尝试,谢谢mvp。