gpt4 book ai didi

java - 在 GUI 构建器中重新验证 JPanel

转载 作者:行者123 更新时间:2023-12-02 00:46:09 24 4
gpt4 key购买 nike

我在 Gui builder 中重新绘制 JPanel 时遇到了麻烦,任何人都可以帮助我吗?这是在其构造函数中生成随机数的主类

public class Main {
public static int q;
public Main(){

Random randomGenerator = new Random();
for (int idx = 1; idx <= 10; ++idx) {
q = randomGenerator.nextInt(100);
}
}

这是 A 类中的按钮单击事件,其中包含 JPanel 作为自定义代码

private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
new Main();
Jpl jpl=new Jpl();

}

这是一个作为自定义组件添加到 A 类的类

public class Jpl extends JPanel {

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


public int printMe(int q) {
removeAll();
for (int i = 0; i <q; i++) {
System.out.println("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");
}
});
}

revalidate();
repaint();
return 1;

}

最佳答案

一个问题:在 ActionListener 代码中,创建一个新的 JPanel(或 Jpl)对象:

private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
Jpl jpl=new Jpl();
int a= jpl.printMe(4);
}

但是这绝对不可能是 GUI 中显示的 Jpl 对象。是的,它是同一类的对象,但它是一个与正在显示的对象完全不同且截然不同的新对象,因此调用它的方法不会对显示的 Jpl 对象产生影响。解决方案是仅在显示的对象上调用方法。我无法告诉您如何获取对该对象的引用,因为我们不知道您显示该对象的其余代码。

另一个问题是您没有在这篇文章中提出问题,所以我不知道我的建议是否能帮助您解决主要问题(但我确实知道它会解决一个问题)。因此,我建议您提出一个正确的问题,以便我们更好地理解这些问题,并且您可能需要发布更多代码。

关于java - 在 GUI 构建器中重新验证 JPanel,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4951457/

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