简体   繁体   English

在GUI的actionListener中按下按钮时,图像无法打开?

[英]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 ActionListenerclass 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.

相关问题 按下按钮后,如何使GUI驱动程序类中的变量转移到ActionListener类中? - How do I make the the variables in my GUI Driver class to be transferred to the ActionListener class after a button is pressed? Java GUI ActionListener。 找出按下了哪个按钮 - Java GUI ActionListener. Finding out which button was pressed 按下按钮时,GUI无法打开(Java) - GUI won't open when button pressed (Java) 如果按下按钮,重新启动我的GUI - Restart my GUI if button is pressed 按下主 GUI 上的按钮时,即使步骤正确执行,编辑 GUI JDialog 也不会打开 - Edit GUI JDialog doesn't open when button on main GUI is pressed, even if steps followed properly 使用相同的actionListener为网格上的每个按钮打开单独的GUI - Using the same actionListener to open individual GUI's for each button on a grid 使用ActionListener的GUI中出现错误 - Errors in my GUI with ActionListener 用Java中的ActionListener按下或释放时,如何为按钮分配方法? - How to assign a method to a button when pressed or released with ActionListener in Java? JButton上的ActionListener在我的框架中创建按钮的“图像”吗? - ActionListener on JButton creating “image” of a button in my frame? 当按下JButton时,我的Java Actionlistener不会更改JTextArea - My Java Actionlistener won't change a JTextArea when a JButton is pressed
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM