请问如何点击按钮就可以弹出图片?

import java.awt.*;


import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Arrays;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.*;
import java.awt.Image;

//让GUI继承Icon可以画图
public class GUI implements Icon{

    static JButton Button1 = new JButton("父亲");
    static JButton Button2 = new JButton("母亲");
    static JButton Button3 = new JButton("儿子");
    static JButton Button4 = new JButton("女儿");
    static JButton Button5 = new JButton("姐姐");
    static JButton Button6 = new JButton("妹妹");
    static JButton Button7 = new JButton("哥哥");
    static JButton Button8 = new JButton("弟弟");
    static JButton Button9 = new JButton("老婆");
    static JButton Button10 = new JButton("删除");
    static JButton Button11 = new JButton("清空");
    static JButton Button12 = new JButton("等于");
     static JTextField txt =new JTextField("",10);
     static String[] str = new String[15];
     static int i = 0;
     //写死一个标签,用来存放图片,写了再说管这么多
     static JLabel label;
     private ImageIcon father = new ImageIcon ("D:\\图片\\亲属计算器图片\\大耳朵图图\\爸爸.jpg");
//   JDialog  txt1 =new JDialog();



    public static void main(String[] args) {
     JFrame frame = new JFrame("亲戚计算器");


        /*
        final Box base = Box.createVerticalBox();
        final Box box1 = Box.createHorizontalBox();
        final Box box2 = Box.createHorizontalBox();
         //在这里先导入图片
         ImageIcon image_father = new ImageIcon("D:\\图片\\亲属计算器图片\\大耳朵图图\\爸爸.jpg");
         image_father.setImage(image_father.getImage().getScaledInstance(100, 200, 70));
         Button1.setIcon(image_father);*/
        // box1.add(Button1);
        // base.add(box1);
        // frame.add(base);
        /* 思路,先添加一块区域,可以用来显示图片,再在此基础上添加响应事件
         其实就是Label*/


     frame.setBounds(600,260,1000,1000);
     frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
     JLabel outLabel = new JLabel();

     //frame.add(father);
     JPanel panel = new JPanel();
      frame.add(panel);
      panel.setLayout(null);
      placeComponents(panel);
      new GUI(panel);
      System.out.println(str[0]);
      //这里其实只监听了文本,并且只能够监听文本
      MyActionListener1 myActionListener1 = new MyActionListener1(txt,outLabel);
      MyActionListener2 myActionListener2 = new MyActionListener2(txt);
      MyActionListener3 myActionListener3 = new MyActionListener3(txt);
      MyActionListener4 myActionListener4 = new MyActionListener4(txt);
      Button1.addActionListener(myActionListener1);
      Button2.addActionListener(myActionListener1);
      Button3.addActionListener(myActionListener1);
      Button4.addActionListener(myActionListener1);
      Button5.addActionListener(myActionListener1);
      Button6.addActionListener(myActionListener1);
      Button7.addActionListener(myActionListener1);
      Button8.addActionListener(myActionListener1);
      Button9.addActionListener(myActionListener1);
      Button10.addActionListener(myActionListener2);
      Button11.addActionListener(myActionListener3);
      Button12.addActionListener(myActionListener4);
      frame.setVisible(true);

}

    private static void placeComponents(JPanel panel) {
        // TODO Auto-generated method stub

    }

    public GUI(JPanel panel) {
        txt.setBounds(10,10, 200, 36);
        panel.add(txt);
        Button1.setBounds(30, 150, 80, 50);
        panel.add(Button1);

        Button2.setBounds(110, 150, 80, 50);
        panel.add(Button2);

        Button3.setBounds(190, 150, 80, 50);
        panel.add(Button3);

        Button4.setBounds(30, 200, 80, 50);
        panel.add(Button4);

        Button5.setBounds(110, 200, 80, 50);
        panel.add(Button5);

        Button6.setBounds(190, 200, 80, 50);
        panel.add(Button6);

        Button7.setBounds(30, 250, 80, 50);
        panel.add(Button7);

        Button8.setBounds(110, 250, 80, 50);
        panel.add(Button8);

        Button9.setBounds(190, 250, 80, 50);
        panel.add(Button9);
        Button10.setBounds(400, 150, 80, 50);
        panel.add(Button10);
        Button11.setBounds(400, 200, 80, 50);
        panel.add(Button11);
        Button12.setBounds(400, 250, 80, 50);
        panel.add(Button12);

        //与文本类似,这里放了一个标签,可以用来准备显示图片


//      Button1.setBounds(400, 150, 80, 50);
//      panel.add(Button10);




    }

}
/*class MyActionListener0 implements ActionListener{//按键后显示相应的图片
        private JImageField imageField;
}*/

class MyActionListener1 implements ActionListener{//按键后文本框显示,获取相应文本及图片
                                                                                          //即需要定义一个图片区域,并且需要判断这个文字对应的图片
    //这才是关键的监听代码的区域
    private JTextField textField;

    public MyActionListener1(JTextField txt, JLabel label) {
        super();
        this.textField = txt;

    }

        //每按一次按钮,此方法都会自动调用一次
    public void actionPerformed(ActionEvent e) {//名称按键
        // TODO Auto-generated method stub
        textField.setText(textField.getText()+e.getActionCommand()+"的");
        GUI.str[GUI.i] = e.getActionCommand().substring(0,2);
        GUI.i++;
        //System.out.println(Arrays.toString(GUI.str));
        //System.out.println(textField.getText()+e.getActionCommand()+"的");
    }
}   
class MyActionListener2 implements ActionListener{//删除方法

    private JTextField textField;

    public MyActionListener2(JTextField txt) {
        super();
        this.textField = txt;
    }

    public void actionPerformed(ActionEvent e) {

        textField.setText(textField.getText().substring(0, textField.getText().length()-3));
        GUI.str[GUI.i] = null;
        GUI.i--;
    }
}
class MyActionListener3 implements ActionListener{//清空按键

    private JTextField textField;

    public MyActionListener3(JTextField txt) {
        super();
        this.textField = txt;
    }


    public void actionPerformed(ActionEvent e) {

        textField.setText(textField.getText().substring(0, 0));
        for (int i = 0; i <= GUI.i; i++) {
            GUI.str[i] = null;
        }
        GUI.i = 0;
    }
}
class MyActionListener4 implements ActionListener{//计算按键

    private JTextField textField;

    public MyActionListener4(JTextField txt) {
        super();
        this.textField = txt;
    }


    public void actionPerformed(ActionEvent e) {
        Jisuanqi jsp = new Jisuanqi();
        textField.setText(jsp.jisaunmian(GUI.str, "y"));
        for (int i = 0; i <= GUI.i; i++) {
            GUI.str[i] = null;
        }
        GUI.i = 0;
    }
}

这是部分代码,运行不出来,因为少了其它的配合的类,主要是要在MyActionListener1中添加一些代码,使得点击Button1-9不仅可以弹出文字,还可以弹出图片。

1个回答

可以将图片放在一个模态框组件内,点击事件触发弹出模态框。

weixin_44327736
光阳阳光 好的,谢谢,我用了一个广泛的事件监听,然后使用标签,再往标签里加Icon类型的图片,然后达成了目的。
一年多之前 回复
Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问
相关内容推荐