起初,我很抱歉在Java,Swing和英语中都是noob。
我已经阅读了很多部分,但仍然不了解如何使用这三行......
removeAll();
revalidate(),
repaint();
这是我的问题。 (因为大多数人都在谈论这个) 我想刷新我的面板以删除JtextAreas并根据comboBox选择的项目添加新的。 我创建了一个开关来做到这一点。 我尝试了不同的地方来放置这些命令,但没有任何正常工作
我真的很抱歉,我当然会错过一些明显的东西,但我太初衷了,不能看到它。
这是我的代码:(我只是从我的主电话中调用它)
`public final class TestPane extends JFrame {
public TestPane() throws ParseException {
/********************* def layouts **********************/
JFrame desktop = new JFrame();
desktop.setSize(new Dimension(1200, 200));
JPanel principalLayout = new JPanel();
principalLayout.setLayout(new GridBagLayout());
GridBagConstraints principalGrid = new GridBagConstraints();
principalGrid.fill=GridBagConstraints.BOTH;
desktop.add(principalLayout);
/********************* def des contraintes **********************/
principalLayout.removeAll();
principalGrid.insets = new Insets(5, 5, 5, 5);
principalGrid.ipady = principalGrid.anchor = GridBagConstraints.CENTER;
principalGrid.weightx = 10;
principalGrid.weighty = 10;
/**************** objets dans le gridBagLayout ******************/
String[] items = new String[4];
items[0] = ("DejAffaire");
items[1] = ("Hebergement");
items[2] = ("Kilometre");
items[3] = ("Taxi");
JComboBox<String> comboChoixAdd = new JComboBox<>(items);
//comboChoixAdd.addActionListener(this);
JButton validAddFrais = new JButton("valider");
//validAddFrais.addActionListener(this);
JCheckBox checkKM = new JCheckBox("afficher frais kilométrique");
JCheckBox checkTaxi = new JCheckBox("afficher taxi");
JCheckBox checkDej = new JCheckBox("afficher frais repas");
JCheckBox checkHotel = new JCheckBox("afficher frais d'hébergement");
/********* ajout dans le gridBagLayout + contraintes ************/
principalGrid.gridx = 0;
principalGrid.gridy = 0;
principalLayout.add(new JLabel("choix du type de frais :"), principalGrid);
//principalGrid.weightx = 2;
//principalGrid.weighty = 1;
principalGrid.gridx = 0;
principalGrid.gridy = 1;
principalLayout.add(comboChoixAdd, principalGrid);
principalGrid.gridx = 0;
principalGrid.gridy = 2;
principalLayout.add(validAddFrais, principalGrid);
principalGrid.gridx = 1;
principalGrid.gridy = 0;
principalGrid.gridwidth = 7;
principalLayout.add(new JLabel("entrer les champs du frais :"), principalGrid);
principalGrid.gridwidth = 1;
principalGrid.gridx = 0;
principalGrid.gridy = 3;
principalLayout.add(new JLabel("affichage des frais :"), principalGrid);
principalGrid.gridx = 2;
principalGrid.gridy = 3;
principalLayout.add(checkKM, principalGrid);
principalGrid.gridx = 3;
principalGrid.gridy = 3;
principalLayout.add(checkDej, principalGrid);
principalGrid.gridx = 4;
principalGrid.gridy = 3;
principalLayout.add(checkHotel, principalGrid);
principalGrid.gridx = 5;
principalGrid.gridy = 3;
principalLayout.add(checkTaxi, principalGrid);
desktop.setTitle("tests");
desktop.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
desktop.setVisible(true);
comboChoixAdd.addActionListener(new ActionListener() {//add actionlistner to listen for change
@Override
public void actionPerformed(ActionEvent e) {
String s = (String) comboChoixAdd.getSelectedItem();//get the selected item
switch (s) {
case "DejAffaire":
principalGrid.gridx = 1;
principalGrid.gridy = 1;
JTextArea text = new JTextArea();
text.setText("test");
text.setBounds(50, 50, 50, 50);
System.out.println(".actionPerformed()");
principalLayout.add(text, principalGrid);
principalLayout.revalidate();
principalLayout.repaint();
break;
case "Hebergement":
principalGrid.gridx = 1;
principalGrid.gridy = 1;
JTextArea textHotel = new JTextArea();
textHotel.setText("azzzzzzzzzzzzzzzzeazeazeaze");
textHotel.setBounds(50, 50, 50, 50);
System.out.println(".actionPerformed()");
principalLayout.add(textHotel, principalGrid);
//principalLayout.removeAll();
principalLayout.revalidate();
principalLayout.repaint();
System.out.println(".actionPerformed()");
break;
case "Kilometre":
System.out.println(".actionPerformed()");
break;
case "Taxi":
System.out.println(".actionPerformed()");
break;
}
System.out.println(".actionPerformed2()");
}
});
comboChoixAdd.setSelectedItem(items[3]);
}
}`