简体   繁体   English

Java Swing:无法显示我的JPanel的多个实例

[英]Java Swing: Can't show several instances of my JPanel

I'm making a GUI and having trouble with a JPanel. 我正在制作GUI并且遇到了JPanel问题。

First of all here is my 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);               
      }
};
}

The problem is that if i create several instances of that panel they won't show like intended. 问题是,如果我创建该面板的多个实例,它们将不会显示出预期的效果。

I tried creating 2 simple JFrames in my main with a new ExperimentPanel for each so the problem is not from the caller. 我尝试在我的main中创建两个简单的JFrame,每个都有一个新的ExperimentPanel,因此问题不是来自调用者。

It works well with one JFrame calling one experiementPanel. 它适用于一个调用一个experiementPanel的JFrame。

here is the display for one and 2 calls: 这是一个和两个电话的显示:

http://imgur.com/a/4DHJn http://imgur.com/a/4DHJn

And how i call them: 我怎么称呼他们:

    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);

You create a Panel class ExperimentPanel which itself consists of several components which are stored in class fields of ExperimentPanel . 您创建一个Panel类ExperimentPanel ,它本身由几个组件组成,这些组件存储在ExperimentPanel类字段中。

Since you declare these class fields as static there is only one instance of them. 由于您将这些类字段声明为静态,因此只有一个实例。 When you instantiate several ExperimentPanel objects they all want to share these fields, which leads to the effects you have seen. 当您实例化几个ExperimentPanel对象时,他们都希望共享这些字段,这会导致您看到的效果。

Therefore remove the static modifier from these fields: 因此,从这些字段中删除static修饰符:

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();
    ...

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

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