从事件监听器开始

时间:2010-05-31 03:16:56

标签: java user-interface image events

我有一个简单的应用程序,显示由平铺图像组成的图片(名为u1,u2,...,u16.jpg)。现在我想添加一些事件,这样我只有在点击适当的按钮时才能显示这些图像。我已经尝试过自己做了,但它没有用。我在哪里做错了什么?

原始代码:

import java.awt.GridLayout;
import javax.swing.*;
import javax.swing.border.BevelBorder;

public class Tiles_2 {

    public static void main(String[] args) {
        final JFrame f = new JFrame("Usmiech");
        JPanel panel = new JPanel(new GridLayout(4, 4, 3, 3));
        JLabel l = new JLabel();

        for (int i = 1; i < 17; i++) {
            String path = "u"+ i+".jpg";
            l = new JLabel(new ImageIcon(path));
            l.setBorder(BorderFactory.createBevelBorder(BevelBorder.RAISED));
            panel.add(l);
        }

        f.setContentPane(panel);
        f.setSize(300, 300);
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        f.setVisible(true);
    }
}

新代码:

import java.awt.GridLayout;
import javax.swing.*;
import javax.swing.border.BevelBorder;
import java.awt.event.*;

public class Zad_8_1 implements ActionListener {

    public void actionPerformed(ActionEvent e) {
        JButton b = (JButton)(e.getSource());
        String i = b.getText();
        b = new JButton(new ImageIcon("u"+i+".jpg"));
    }

    public static void main(String[] args) {
        final JFrame f = new JFrame("Smile");
        JPanel panel = new JPanel(new GridLayout(4, 4, 3, 3));
        JButton l = null;

        for (int i = 1; i < 17; i++) {
            String path = "u"+ i+".jpg";
            l = new JButton(""+i);
            l.setBorder(BorderFactory.createBevelBorder(BevelBorder.RAISED));
            l.setSize(53,53);
            panel.add(l);
        }

        f.setContentPane(panel);
        f.setSize(300, 300);
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        f.setVisible(true);
    }
}

这应该是这样的: this http://img535.imageshack.us/img535/3129/lab8a.jpg

4 个答案:

答案 0 :(得分:1)

尝试注册按钮以收听如下事件:

 for (int i = 1; i < 17; i++) {
        String path = "u"+ i+".jpg";
        l = new JButton(""+i);
        l.setBorder(BorderFactory.createBevelBorder(BevelBorder.RAISED));
        l.setSize(53,53);
        l.addActionListener(this);
        panel.add(l);
    }

答案 1 :(得分:0)

您应该只重置图标,而不是重新分配b JButton的新实例:

b.setIcon(new ImageIcon("u"+i+".jpg"));

(看看通过引用传递/通过Java中的值概念传递,well explained here

最重要的是,您需要遵循Vincent的建议并实际注册所有按钮ActionListener

l.addActionListener(this);

答案 2 :(得分:0)

b = new JButton(new ImageIcon("u"+i+".jpg")); 

创建一个新按钮并不会使按钮只是位于内存中,因为它尚未添加到GUI中。

简单的解决方案是重置图标:

b.setIcon( new ImageIcon(...) );

答案 3 :(得分:0)

您需要使用构造函数来添加事件侦听器。

import java.awt.GridLayout;
import javax.swing.*;
import javax.swing.border.BevelBorder;
import java.awt.event.*;
import java.awt.*;

public class Demo {

    public static void main(String args[]) {
        javax.swing.SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                render("Window");
            }
        });
    }

    public static void render(String s){
        JFrame f = new JFrame("Smile");
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        Pane pan = new Pane();
        f.add(pan);
        f.setSize(300,300);
        f.setVisible(true);
    }
}

    class Pane extends JPanel implements ActionListener {

        Pane(){
            setLayout(new GridLayout(4, 4, 3, 3));
            JButton l = null;
            for (int i = 1; i < 17; i++) {
                l = new JButton(""+i);
                l.setBorder(BorderFactory.createBevelBorder(BevelBorder.RAISED));
                l.addActionListener(this);
                l.setSize(53,53);
                add(l);
            }
        }

    public void actionPerformed(ActionEvent e) {
        JButton b = (JButton)(e.getSource());
        String i = b.getText();
        b.setIcon(new ImageIcon("u"+i+".jpg"));
    }
    }