每次迭代清除 JPanel

时间:2021-06-11 10:07:30

标签: java swing jpanel

如何在每次循环再次开始时清除面板?如果我在品牌面板上输入“hersheys”并在产品面板上输入“chocolate”然后按回车键,“hersheys”和“chocolate”这个词仍然存在。如何清除?

import javax.swing.*;

public class Test {
   public static void main(String[] args) {
        JTextField f1 = new JTextField(5);
        JTextField f2 = new JTextField(5);

        JPanel myPanel = new JPanel();
        myPanel.setLayout(new BoxLayout(myPanel, BoxLayout.Y_AXIS));
        myPanel.add(new JLabel("Brand:"));
        myPanel.add(f1);
        myPanel.add(new JLabel("Product:"));
        myPanel.add(f2);


        for(int i=0;i<2;i++){
            int result = JOptionPane.showConfirmDialog(null, myPanel, 
            "Please Enter X and Y Values", JOptionPane.OK_CANCEL_OPTION);
        }
    }
 }

0 个答案:

没有答案