gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-29 04:57:24 25 4
gpt4 key购买 nike

我正在制作 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 中创建 2 个简单的 JFrame,每个 JFrame 都有一个新的 ExperimentPanel,所以问题不是来自调用者。

它适用于一个 JFrame 调用一个 experiementPanel。

这是 1 和 2 调用的显示:

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

我怎么称呼他们:

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

最佳答案

您创建了一个 Panel 类 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();
...

关于Java Swing : Can't show several instances of my JPanel,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33255309/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com