简体   繁体   English

当用户单击GUI中的按钮时,我想打开一个图像

[英]I would like to open an image when a user clicks on a button in GUI

I am trying to get an image to open within my frame once a button has been clicked, so far I have put the image in a JLabel within the button I want it to open to once clicked. 单击按钮后,我正在尝试使图像在框架中打开,到目前为止,我已将图像放入希望单击以打开的按钮中的JLabel中。 But it shows up straight away. 但是它立即显示出来。 Any ideas on how to set it so that the image opens once the "scissors" button has been clicked? 关于如何设置它以便单击“剪刀”按钮后打开图像的任何想法?

    btnPlaySci = new JButton ("Scissors!");
    btnPlaySci.setBounds(180, 40, 110, 20);
    btnPlaySci.addActionListener(new PlaySciHandler());
    panel.add (btnPlaySci);
    btnPlaySci.setForeground(Color.MAGENTA);

    //below is my image, and above is the button I want it to open to

    ImageIcon rock1 = new ImageIcon("rock.jpg");
    JLabel picture = new JLabel(rock1);
    picture.setBounds(60, 200, 400, 400);
    panel.add(picture);

Edited code pasted in comment 编辑后的代码粘贴到注释中

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); 
            ImageIcon rock1 = new ImageIcon("rock.jpg"); 
            JLabel picture = new JLabel(rock1); 
            picture.setBounds(60, 200, 400, 400); panel.add(picture); 
        }
    }

I think you want to add the JLabel (containing image) to panel when button is clciked. 我认为您希望在单击按钮时将JLabel(包含图像)添加到面板中。

You will have to code the below class which will handle the event when user clicks button 'btnPlaySci'. 您将必须编写以下类的代码,该类将在用户单击按钮“ btnPlaySci”时处理事件。

btnPlaySci.addActionListener(new PlaySciHandler(panel)); //replace your addActionListener line with this code.

import java.awt.event.*;
class PlaySciHandler implements ActionListener 
{
    JPanel panel;
    PlaySciHandler(JPanel p)
    {
         panel = p;
    }
    public void actionPerformed(ActionEvent ae)   
    {
      ImageIcon rock1 = new ImageIcon("rock.jpg");
      JLabel picture = new JLabel(rock1);
      picture.setBounds(60, 200, 400, 400);
      panel.add(picture);   
    } 
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在swing GUI中,如何在用户单击按钮时将JPanel显示为最大化? - In swing GUI how to show a JPanel as maximized when the user clicks a button? 如何通过按此GUI的按钮来打开另一个GUI Jpanel类? - How would I open another gui jpanel class by pressing a button from this gui? 当用户注销后单击“后退”按钮时,会话如何失效,例如银行网站 - How to session expire when user clicks back button after log out i.e. like bank website 在GUI的actionListener中按下按钮时,图像无法打开? - image does not open when button is pressed in actionListener in my GUI? 如何检查用户何时单击了正确的按钮? - How do I check when a user clicks the correct button? GUI没有响应按钮单击 - GUI is not responding to button clicks 用户单击红叉时如何显示确认消息? Java GUI - How do i display a confirmation message when the user clicks on the red cross? Java GUI 用户单击主页按钮时终止活动 - Killing the activity when user clicks the home button 我希望声音在用户单击“主页”按钮时停止,但在调用新的内容视图时不停止 - I want the sound to stop when the user clicks the home button, but not when a new content view is called 我有一幅我希望每次单击按钮都可以增长的图像 - I have an image which i would like to have grow everytime i click a button
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM