我有一个问题,我的一个JPanels
没有根据窗口更新自己,而它应该也是。我将试着简要地解释一下发生了什么。将一个字符列表从另一个类输入到arraylist
(名为lookReply)中(这与我测试的一样有效),然后使用2 iterations
将每个字符分配给由JLabels
制成的方形表中的一个坐标。这些JLabels
位于lookReplyGUIPanel JPanel
中。按下按钮后,新字符将加载到arraylist
中,并重复执行。但是,该窗口不会显示此更新。我知道他们通过一些测试被输入到JLabels
中,但只是窗口的更新似乎不起作用。我正在使用invalidate, validate and repaint
,但它仍然不起作用。请参阅我下面的代码以获取所需的零件。
第一个方法-处理arraylist,然后调用另一个方法。
private void look()
{
//clear arrayList then add new lookReply to it
lookReply.clear();
while (HumanUser.lookReply==null)
{
}
for(int n = 0; n<HumanUser.lookReply.length(); n++)
{
lookReply.add(HumanUser.lookReply.charAt(n));
}
lookReply();
//UP TO HERE WORKS FINE
screen.invalidate();
screen.validate();
screen.repaint();
}
处理所讨论的CCD_ 10的方法。
/**
* Made up of several smaller JPanels each relate to 1 map position from the lookReply command.
*/
private JPanel lookReply()
{
JPanel lookReplyGUIPanel = new JPanel(new GridBagLayout());
GridBagConstraints c = new GridBagConstraints();
Border blackline = BorderFactory.createLineBorder(Color.black);
//Create a square table dependent on the radius of view.
//Then fill each cell with the block retrieved from the lookReply arrayList.
for(int y = lookReplyY; y>0; y--)
{
for(int x = lookReplyX; x>0; x--)
{wall...
//Then assign it to a variable which is used in the JLabel.
String item = null;
if (lookReply.size()!=0)
{
item = lookReply.get(x*y-1) + "";
}
//ADD TO CHECK WHAT EACH ITEM IS AND USE THE RELEVENT PICTURE
JLabel lookx = new JLabel(item);
int width = (2*screenHeight/(5*lookReplyX)-10);
lookx.setPreferredSize(new Dimension(width, width));
lookx.setBorder(blackline);
c.gridx = x;
c.gridy = y;
lookReplyGUIPanel.add(lookx, c);
}
}
return lookReplyGUIPanel;
}
如果这有帮助的话,请提供更多关于一切概述的详细信息。调用的第一个方法使用gridBagConstraints
将所有JPanels
添加到正确位置的JFrame
。JFrame
是在一个方法之外创建的,所以所有其他方法都可以看到它。非常感谢所有帮助,如果需要,很乐意提供更多细节!感谢
下面是一段简单的可编译代码,它演示了同样的问题。忽略必须调整窗口大小的事实-每次单击button
按钮都会将m增加1,并且应该更新以显示5X5 m表的lookReply
方法。但事实并非如此。
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
public class temp
{
JFrame screen = new JFrame("temp");
int m = 1;
public static void main(String[] args)
{
temp g = new temp();
g.create();
}
private void create()
{
screen.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
screen.setLayout(new GridBagLayout());
GridBagConstraints c = new GridBagConstraints();
c.gridy = 0;
screen.add(lookReply(), c);
c.gridy = 1;
screen.add(button(), c);
screen.setVisible(true);
}
private JPanel lookReply()
{
JPanel lookReplyGUIPanel = new JPanel(new GridBagLayout());
GridBagConstraints c = new GridBagConstraints();
for(int y = 5; y>0; y--)
{
for(int x = 5; x>0; x--)
{
JLabel lookx = new JLabel((m + ""));
c.gridx = x;
c.gridy = y;
lookReplyGUIPanel.add(lookx, c);
}
}
screen.invalidate();
screen.validate();
screen.repaint();
return lookReplyGUIPanel;
}
private JPanel button()
{
JPanel button = new JPanel(new GridBagLayout());
GridBagConstraints c = new GridBagConstraints();
JButton b = new JButton("button");
b.setBorder(null);
c.gridx = 2;
c.gridy = 2;
button.add(b, c);
b.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
m++;
lookReply();
}
});
return button;
}
}
因此,根据您的示例,lookReply
创建了一个新的JPanel
,但您没有对它做任何操作。因此,该组件从未添加到屏幕上,因此所有使其刷新的尝试都不会产生任何效果。。。
使用javax.swing.Timer
更新GUI元素,如下所示。