Java 如何重新绘制JPanel
我有一个带有自定义图标和3个按钮的JLabel数组。第一次,单击面板重新绘制和 将显示以下标签:。当我单击第二个按钮(Urmatorul)时,我希望删除窗格的内容,然后替换标签,但这是它的外观:。最后一个按钮是退出按钮。 另外,我认为是关于GridBags的限制Java 如何重新绘制JPanel,java,swing,jpanel,Java,Swing,Jpanel,我有一个带有自定义图标和3个按钮的JLabel数组。第一次,单击面板重新绘制和 将显示以下标签:。当我单击第二个按钮(Urmatorul)时,我希望删除窗格的内容,然后替换标签,但这是它的外观:。最后一个按钮是退出按钮。 另外,我认为是关于GridBags的限制 static JLabel[] label = new JLabel[words[random].length()]; // in the main class //in the JPanel class for(int
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();
}
});
}
static JLabel[]label=new JLabel[words[random].length()];//在班上
//在JPanel类中
对于(int i=0;i
repaint();
和revalidate();
应该可以工作,但是试着在visibleJPanel
中添加和删除新的可见组件,并告诉我们发生了什么。添加和删除新的可见组件是什么意思?要添加不同的东西?如jbutton?1)为了更快地获得更好的帮助,发布(最小完整可验证示例)或(简短、自包含、正确的示例)。2)使用中所示的。