java java更新Jpanel组件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5033496/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 09:08:09  来源:igfitidea点击:

java update Jpanel component

javaswingjpanelvalidationrepaint

提问by Qaiser Mehmood

I am using a Custome jPanel in my Gui Builder JFram Class A, the problem i am facing is to update the components (Lable) in my JPanel when I click button in JFrame.here is the button in Gui Builder JFrame ClassA: it changes the color of Jpl and also remove all the labels but not update the new labels.

我在 Gui Builder JFram Class A 中使用 Custome jPanel,我面临的问题是当我单击 JFrame 中的按钮时更新 JPanel 中的组件(Lable)。这里是 Gui Builder JFrame ClassA 中的按钮:它改变了Jpl 的颜色,并删除所有标签但不更新新标签。

private void btnShowActionPerformed(java.awt.event.ActionEvent evt) {
    // TODO add your handling code here:

            Random randomGenerator = new Random();
            for (int idx = 1; idx <= 10; ++idx) {
                q = randomGenerator.nextInt(100);
            }
            jpl1.removeAll();
            new Jpl().printMe(ClassA.q);
            jpl1.revalidate();
            jpl1.setBackground(Color.BLUE);
            jpl1.repaint();
}

here is Jpl class that is used as a custome component in GuiBuilder JFrame Class A.

这是在 GuiBuilder JFrame Class A 中用作客户组件的 Jpl 类。

public class Jpl extends JPanel {

public Jpl() {
    printMe(ClassA.q);
}


public void printMe(int q) {

    for (int i = 0; i <q; i++) {
        System.out.println(i+"rinting lable");
        String htmlLabel = "<html><font color=\"#A01070\">" + i + " New Lable </font></html>";
        JLabel lbl = new JLabel(htmlLabel);
        setLayout(new GridLayout(0, 1));
        add(lbl, Jpl.RIGHT_ALIGNMENT);
        lbl.setForeground(Color.BLUE);
        Border border = BorderFactory.createLineBorder(Color.lightGray);
        lbl.setBorder(border);
        lbl.add(new JSeparator(SwingConstants.HORIZONTAL));

        lbl.addMouseListener(new MouseAdapter() {

            @Override
            public void mousePressed(MouseEvent e) {
                JLabel label = (JLabel) e.getSource();
                JOptionPane.showMessageDialog(null, "You Slected");
                System.out.println(label.getText() + "NO AKKA is Selected");
            }
        });
    }

}

回答by JPelletier

You are calling printMe() on a new instance of Jpl, try that:

您正在 Jpl 的新实例上调用 printMe(),请尝试:

private void btnShowActionPerformed(java.awt.event.ActionEvent evt) {
    // TODO add your handling code here:

            Random randomGenerator = new Random();
            for (int idx = 1; idx <= 10; ++idx) {
                q = randomGenerator.nextInt(100);
            }
            jpl1.removeAll();
            jpl1.printMe(ClassA.q); // HERE - REMOVED new and using jpl1 instance
            jpl1.setBackground(Color.BLUE);
            jpl1.revalidate();
            jpl1.repaint();
}

In don't understand why you loop 10 times for your random number. Only the last result will be kept, maybe a you wanted to use q += randomGenerator.nextInt(100);. Also, ClassA.qshould be replaced by qif it's the same variable.

不明白为什么你的随机数循环 10 次。只会保留最后一个结果,也许您想使用q += randomGenerator.nextInt(100);. 此外,如果它是相同的变量,ClassA.q则应替换q