我有一个 JFrame,在东和西有两个按钮,在北有标签,在南有标签和 TextField。和中心的一张图像。我想在中心 30 秒后调用另一个图像。但每次我调用第二张图像时,北、南、东、西的分量都会消失。
这是我的代码。 //JFrame 中的组件。
firstPicblur = new JPanel(new BorderLayout());
pictureblur01 = new ImageIcon("C:\\java pics\\papsi2.jpg");
pictureblurA = new JLabel(pictureblur01);
firstPicblur.add(pictureblurA,BorderLayout.CENTER);
//FirstPicture blurred B
firstPicblurB = new JPanel(new BorderLayout());
pictureblur02 = new ImageIcon("C:\\java pics\\papsi1.jpg");
pictureblurB = new JLabel(pictureblur02);
firstPicblurB.add(pictureblurB,BorderLayout.CENTER);
//Next0Prev buttons
Next0Prev = new JPanel(new BorderLayout());
Next = new JButton("NEXT");
Prev = new JButton("PREV");
//Next0Prev labels is constant at SOUTH
firstPicblurA = new JPanel(new BorderLayout());
clueNo1 = new JLabel("Picture Number 01");
TypeHere = new JTextField("Guess Who");
firstPicblurA.add(TypeHere, BorderLayout.CENTER);
firstPicblurA.add(clueNo1, BorderLayout.SOUTH);
Next0Prev.add(firstPicblur,BorderLayout.CENTER);
Next0Prev.add(Next,BorderLayout.EAST);
Next0Prev.add(Prev,BorderLayout.WEST);
Next0Prev.add(firstPicblurA,BorderLayout.SOUTH);
//其他组件
这是actionEvent
if(e.getSource() == continueButton){
add(Next0Prev);
Next0Prev.setVisible(true);
Next0Prev.repaint();
Next0Prev.revalidate();
loadingEffectBtn.setVisible(false);
Timer ta = new Timer(10000, new ActionListener() {
public void actionPerformed(ActionEvent e) {
add(firstPicblurB);
firstPicblurB.setVisible(true);
Next0Prev.setVisible(true);
}
});
ta.start();
ta.setRepeats(true);
}
但是当我这样做的时候。第二张图片出现在中心,但 N、E、W、S 分量正在消失。
最佳答案
这有点很难说,但似乎正在发生的事情是......
您正在创建 Next0Prev
并向其中添加所有组件...
Next0Prev = new JPanel(new BorderLayout());
//....
Next0Prev.add(firstPicblur,BorderLayout.CENTER);
Next0Prev.add(Next,BorderLayout.EAST);
Next0Prev.add(Prev,BorderLayout.WEST);
Next0Prev.add(firstPicblurA,BorderLayout.SOUTH);
我假设您要添加到框架中...
然后在 Timer
的 actionPerformed
中,将 firstPicblurB
添加到主容器...
public void actionPerformed(ActionEvent e) {
add(firstPicblurB);
firstPicblurB.setVisible(true);
Next0Prev.setVisible(true);
}
现在,假设主容器是 JFrame
或使用 BorderLayout
,这将有效地将 Next0Prev
隐藏为 BorderLayout
的 5 个位置中的任何一个位置只能有一个组件。
相反,您应该将 firstPicblurB
添加到 Next0Prev
...
一个更简单的解决方案是(正如已经指出的那样)使用单个JLabel
并简单地更改它的图标
我还强烈建议您花时间阅读并申请 Code Conventions for the Java Programming Language
关于java - 仅更改 JFrame 中的一个面板,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19374621/