我有一组带有自定义图标和3个按钮的JLabel。第一次单击面板重绘时显示一个按钮(开始) 标签显示为:http://imgur.com/8dcpCe7。当我点击第二个(Urmatorul)时,我希望删除窗格的内容,然后替换标签,但这就是它的外观:http://imgur.com/nY87rCx。最后一个按钮是一个退出按钮。 另外,我认为是关于GridBagConstraints ..
static JLabel[] label = new JLabel[words[random].length()]; // in the main class
//in the JPanel class
for(int i = 0; i < label.length; i++)
label[i] = new JLabel(box);
add(sButton);
//This is the first button ( that clears the jpanel for the first time and adds the 2 buttons + jlabels
sButton.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
remove(sButton);
c.gridx = 3;
add(text,c);
c.gridx = 4;
add(field, c);
c.gridx = 0;
for(int i = 0; i < label.length; i++){
c.gridy = 1;
c.gridx++;
c.insets = new Insets(400,0,0,0);
add(label[i],c);
}
c.insets = new Insets(0,0,0,0);
c.gridy ++;
c.gridx = 0;
c.gridwidth = 4;
c.weighty = 0.09;
c.anchor = GridBagConstraints.SOUTH;
add(eButton, c);
c.gridx = 3;
add(nButton, c);
revalidate();
repaint();
}
});
eButton.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
System.exit(0);
}
});
// The second button, which i am having trouble with ( i just copied the code from the first button listener)
nButton.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
label = new JLabel[words[random].length()];
for(int i = 0; i < label.length; i++)
label[i] = new JLabel(box);
removeAll();
revalidate();
repaint();
c.gridx = 3;
add(text,c);
c.gridx = 4;
add(field, c);
c.gridx = 0;
for(int i = 0; i < label.length; i++){
c.gridy = 1;
c.gridx++;
c.insets = new Insets(400,0,0,0);
add(label[i],c);
}
c.insets = new Insets(0,0,0,0);
c.gridy ++;
c.gridx = 0;
c.gridwidth = 4;
c.weighty = 0.09;
c.anchor = GridBagConstraints.SOUTH;
add(eButton, c);
c.gridx = 3;
add(nButton, c);
revalidate();
repaint();
}
});
}
答案 0 :(得分:0)
repaint();
和revalidate();
应该有效,但请尝试添加新的可见组件并将其移至可见JPanel
并告诉我们会发生什么。