[英]image does not open when button is pressed in actionListener in my GUI?
I am trying to get an image to open when the PlaySci button is pressed so I put the image in the PlaySci action listener, however it only opens when the exit button is pressed? 我试图在按下PlaySci按钮时打开图像,因此将图像放入PlaySci动作监听器中,但是仅在按下退出按钮时才打开图像吗?
I have looked at it for hours and still dont understand why, I have tried to get rid of the exit button alltogether but then the image does not show at all. 我已经看了好几个小时,但仍然不明白为什么,我试图一起摆脱退出按钮,但是图像根本不显示。
I made the image into a JLabel
at the top: 我将图像放在顶部的
JLabel
中:
ImageIcon scis1 = new ImageIcon("scis.jpg");
private JLabel picture = new JLabel(scis1);
Here is the code for my PlaySci button ActonListener: 这是我的PlaySci按钮ActonListener的代码:
class PlaySciHandler implements ActionListener {
public void actionPerformed(ActionEvent event) {
String computerRand = sps.computerChoice();
txtComputerRand.setText(computerRand);
String result = sps.play(Sps.SCISSORS);
txtResult.setText(result);
picture.setBounds(60, 200, 400, 400);// this is the image I want displayed when the PlaySci button is pressed
panel.add(picture);
}
}
This is the exit button ActionListener (That for some reason is the only way to display the image): 这是退出按钮ActionListener(出于某种原因,这是显示图像的唯一方法):
class exitHandler implements ActionListener{
public void actionPerformed(ActionEvent e) {
int n = JOptionPane.showConfirmDialog(frame, //when this button is pressed the image comes up?
"Are you sure you want to exit?",
"Exit?",
JOptionPane.YES_NO_OPTION);
if(n == JOptionPane.YES_OPTION){
System.exit(0);
}
}
}
This is the code creating the button and adding the ActionListener: 这是创建按钮并添加ActionListener的代码:
btnPlaySci = new JButton ("Scissors!");
btnPlaySci.setBounds(180, 40, 110, 20);
btnPlaySci.addActionListener(new PlaySciHandler());
panel.add (btnPlaySci);
btnPlaySci.setForeground(Color.MAGENTA);
Any help would be appreciated. 任何帮助,将不胜感激。
You should repaint your panel after you add picture to it. 向其添加图片后,应重新粉刷面板。 See the code for
PlaySciHandler
actionPerformed
method. 请参阅
PlaySciHandler
actionPerformed
方法的代码。
class PlaySciHandler implements ActionListener {
public void actionPerformed(ActionEvent event) {
String computerRand = sps.computerChoice();
txtComputerRand.setText(computerRand);
String result = sps.play(Sps.SCISSORS);
txtResult.setText(result);
picture.setBounds(60, 200, 400, 400);// this is the image I want displayed when the PlaySci button is pressed
panel.add(picture);
panel.repaint();//Must repaint the panel .
}
}
Note: As a side note I would suggest you to never use null Layout
for JPanel
.Use the inbuilt Layouts
provided by Swing
. 注意:作为一个侧面说明,我建议您不要对
JPanel
使用null Layout
。使用Swing
提供的内置Layouts
。 You can get more information about Layouts usage at this official site . 您可以在此官方站点上获得有关布局使用的更多信息。 Another one is that always stick with java naming conventions.
另一个是始终坚持使用Java命名约定。 Class
exitHandler
should be written as ExitHandler
instead.To know more have a look at this official site . 类
exitHandler
应该改写为ExitHandler
。要了解更多信息,请访问此官方网站 。
Don't add the JLabel
in the class PlaySciHandler implements ActionListener
block.Add it in your createForm()
method and make it invisible: picture.setVisible(false);
不要在
class PlaySciHandler implements ActionListener
的class PlaySciHandler implements ActionListener
添加JLabel
。将其添加到您的createForm()
方法中并使其不可见: picture.setVisible(false);
and when you want to display after a button click, make it visible : picture.setVisible(true);
当您想在单击按钮后显示时,使其可见:
picture.setVisible(true);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.