【问题标题】:Inserting image through event handling?通过事件处理插入图像?
【发布时间】:2014-11-22 10:02:34
【问题描述】:

我在 java 中遇到事件处理问题。

如果按下按钮 1,我想添加 image1,如果按下按钮 2,我想添加 image2,等等。

这是我到现在为止的代码;任何人都可以帮忙吗?此代码无法编译。

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
import  javax.swing.ImageIcon;

public class MyPanel extends JPanel {
    private JLabel jcomp1;
    private JButton jcomp2;
    private JButton jcomp3;
    private JButton jcomp4;
    private JButton jcomp5;
    private JButton jcomp6;
    private JButton jcomp7;
    private JButton jcomp8;
    private JButton jcomp9;
    private ImageIcon image1;
    private ImageIcon image2;
    private ImageIcon image3;
    private ImageIcon image4;
    private ImageIcon image5;
    private ImageIcon image6;
    private ImageIcon image7;
    private ImageIcon image8;

    public MyPanel() {
        //construct components
        image1 = new ImageIcon(getClass().getResource("hang1.jpg"));
        image2 = new ImageIcon(getClass().getResource("hang2.jpg"));
        image3 = new ImageIcon(getClass().getResource("hang3.jpg"));
        image4 = new ImageIcon(getClass().getResource("hang4.jpg"));
        image5 = new ImageIcon(getClass().getResource("hang5.jpg"));
        image6 = new ImageIcon(getClass().getResource("hang6.jpg"));
        image7 = new ImageIcon(getClass().getResource("hang7.jpg"));
        image8 = new ImageIcon(getClass().getResource("hang8.jpg"));


        jcomp1 = new JLabel (image1);
        jcomp2 = new JButton ("1");
        jcomp3 = new JButton ("2");
        jcomp4 = new JButton ("3");
        jcomp5 = new JButton ("4");
        jcomp6 = new JButton ("5");
        jcomp7 = new JButton ("6");
        jcomp8 = new JButton ("7");
        jcomp9 = new JButton ("8");

        //events
        jcomp2.setActionCommand("1");
        jcomp3.setActionCommand("2");
        jcomp4.setActionCommand("3");
        jcomp5.setActionCommand("4");
        jcomp6.setActionCommand("5");
        jcomp7.setActionCommand("6");
        jcomp8.setActionCommand("7");
        jcomp9.setActionCommand("8");


        jcomp2.addActionListener(new ButtonClickListener()); 
        jcomp3.addActionListener(new ButtonClickListener()); 
        jcomp4.addActionListener(new ButtonClickListener()); 
        jcomp5.addActionListener(new ButtonClickListener()); 
        jcomp6.addActionListener(new ButtonClickListener()); 
        jcomp7.addActionListener(new ButtonClickListener()); 
        jcomp8.addActionListener(new ButtonClickListener()); 
        jcomp9.addActionListener(new ButtonClickListener()); 


        //adjust size and set layout
        setPreferredSize(new Dimension(624, 537));
        setLayout(null);

        //add components

        add(jcomp2);
        add(jcomp3);
        add(jcomp4);
        add(jcomp5);
        add(jcomp6);
        add(jcomp7);
        add(jcomp8);
        add(jcomp9);

        // set component bounds (only needed by Absolute Positioning)
        jcomp1.setBounds(15, 10, 595, 350);
        jcomp2.setBounds(35, 375, 100, 25);
        jcomp3.setBounds(190, 375, 100, 25);
        jcomp4.setBounds(320, 375, 100, 25);
        jcomp5.setBounds(465, 375, 100, 25);
        jcomp6.setBounds(35, 450, 100, 25);
        jcomp7.setBounds(190, 450, 100, 25);
        jcomp8.setBounds(320, 450, 100, 25);
        jcomp9.setBounds(465, 450, 100, 25);
    }

    class ButtonClickListener implements ActionListener{
        public void actionPerformed(ActionEvent e) {
            String command = e.getActionCommand();  
            if (command.equals("1")) {
                jcomp1.set(image1);
            }

        }       
    }


    public static void main (String[] args) {
        JFrame frame = new JFrame("MyPanel");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.getContentPane().add(new MyPanel());
        frame.pack();
        frame.setVisible (true);
    }
}

【问题讨论】:

  • jcomp1.set(image) 我在 API 文档中找不到 JLabelset 方法
  • 听说过编程中的循环吗?
  • “此代码无法编译。” 为什么会这样?编译器告诉你什么?
  • 还有一个提示:不要使用空布局(绝对定位)。我不会比布局管理器更好看,而且更难维护。
  • 如果任何答案解决了您的问题,请考虑接受它(绿色复选标记)和\或赞成有用的答案。

标签: java swing events button awt


【解决方案1】:

我相信你正试图得到这样的东西:

public class MyPanel extends JPanel {

    private JLabel label;
    private JButton[] buttons = new JButton[8];
    private ImageIcon[] images = new ImageIcon[8];

    public MyPanel() {

        JPanel buttonPanel = new JPanel(new GridLayout(2, 4, 15, 10));

        for (int i = 0; i < images.length; i++) {
            images[i] = new ImageIcon(getClass().getResource(i+1 + ".png"));
            buttons[i] = new JButton(String.valueOf(i+1));
            buttons[i].setActionCommand(String.valueOf(i+1));
            buttons[i].addActionListener(new ButtonClickListener());
            buttonPanel.add(buttons[i]);
        }
        label = new JLabel(images[0]);

        setLayout(new BorderLayout());
        add(label);
        add(buttonPanel, BorderLayout.PAGE_END);
    }

    class ButtonClickListener implements ActionListener {

        public void actionPerformed(ActionEvent e) {

            label.setIcon(images[Integer.parseInt(e.getActionCommand()) - 1]);
        }
    }

    public static void main(String[] args) {

        JFrame frame = new JFrame("MyPanel");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.getContentPane().add(new MyPanel());
        frame.pack();
        frame.setVisible(true);
    }
}

注意事项:

  • 不要忘记更改图像文件名。
  • 您可以使用布局管理器来获得您想要的。
  • 我删除了setPreferredSize(new Dimension(624, 537));,因为您没有指定会使该行变得毫无意义的调整大小行为。 pack() 会为您处理尺寸。

【讨论】:

    【解决方案2】:

    设置按钮或标签的图标。为此,您需要创建一个 ImageIcon,它有多个构造函数可以从文件名或加载的图像创建。

    jcomp1.setIcon(new ImageIcon(...));

    【讨论】:

      猜你喜欢
      • 2016-04-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-12-14
      • 2015-03-15
      相关资源
      最近更新 更多