繁体   English   中英

java更新Jpanel组件

[英]java update Jpanel component

我在我的Gui Builder JFram 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();
}

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

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

}

你在一个新的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();
}

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

暂无
暂无

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

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