在 GUI 构建器中重新验证 JPanel
我在 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;
}
I am getting truble to repaint the JPanel in Gui builder can any body help me please.
here is main class that generate random Numbers in its Constructor
public class Main {
public static int q;
public Main(){
Random randomGenerator = new Random();
for (int idx = 1; idx <= 10; ++idx) {
q = randomGenerator.nextInt(100);
}
}
here is button click event in class A that include JPanel as custom code
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
new Main();
Jpl jpl=new Jpl();
}
Here is a class that add as custome component to class 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;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
一个问题:在您的 ActionListener 代码中,您创建了一个新的 JPanel(或 Jpl)对象:
但绝对不可能这是在 GUI 中显示的 Jpl 对象。是的,它是同一类的对象,但它是一个与正在显示的对象完全不同且截然不同的新对象,因此调用它的方法不会对显示的 Jpl 对象产生影响。解决方案是仅在显示的对象上调用方法。我无法告诉您如何获取对该对象的引用,因为我们不知道您显示该对象的其余代码。
另一个问题是您没有在这篇文章中提出问题,所以我不知道我的建议是否能帮助您解决主要问题(但我确实知道它会解决一个问题)。因此,我建议您提出一个正确的问题,以便我们可以更好地理解这些问题,并且您可能需要发布更多代码。
One problem: In your ActionListener code, you create a new JPanel (or Jpl) object:
but there is absolutely no way that this is the Jpl object that is displayed in your GUI. Yes it is an object of the same class, but it is a completely new different and distinct object from the one being displayed, and so calling a method on it will have no effect on the displayed Jpl object. The solution is to call your methods only on the displayed object. I cannot tell you how to get a reference to that object since we are not privy to the rest of your code where you display it.
The other problem is that you haven't asked a question in this post, so I have no idea if my suggestion will help your primary problem (but I do know that it will solve a problem). So I suggest that you ask a proper question so we can better understand the issues and that you'll likely need to post more code.
目前尚不清楚您在问什么,所以我只是针对您的代码中的问题发表评论。
printMe()
返回一个int
,但它始终是相同的值,因此没有任何意义。只需使该方法void
即可。String htmlLabel = "New Label+ i";
这始终会生成相同的字符串,并且不会为您提供i
的值。相反,您需要String htmlLabel = "New Label" + i;
您没有对循环中创建的
JLabel
执行任何操作,因此每个对象将不再当前迭代结束后被引用。接下来,您的MouseListener
将永远不会被触发,因为您添加它的标签永远不会出现在 GUI 中。It is not clear what you are asking, so I'm just going to post comments about problems in your code.
printMe()
returns anint
, but it is always the same value, so it has no meaning. Just make the methodvoid
.String htmlLabel = "New Label+ i";
This is always going to produce the same string and will not give you the value ofi
. Instead you wantString htmlLabel = "New Label" + i;
You are not doing any thing with the
JLabel
s you are making in the loop, so each object will no longer be referenced after the current iteration ends. In continuation of this, yourMouseListener
will never be triggered because the label you are adding it to will never appear in the GUI.除了其他人提到的问题之外:
您正在创建标签,但没有将它们添加到 JPanel 中。试试这个:
In addition to the issues others have mentioned:
You are creating the labels, but you are not adding them to the JPanel. Try this: