【问题标题】:Gridlayout, different action to each cell on same mouseventGridlayout,对同一鼠标事件的每个单元格的不同操作
【发布时间】:2014-04-28 09:29:28
【问题描述】:

我有一个填充了从文件加载的图像的 Gridlayout,我想在 mouseEvent mouseClicked 上为每个图像添加一个不同的,例如弹出窗口。如何确定我点击的是哪张图片?我尝试添加 GetComponentAt(Point),但 Eclipse 一直将其显示为 mouseAdapter 的未识别方法,我如何确定 if 语句的字段?

这就是我所拥有的:

public class testclass implements ItemListener  {

JPanel template;
final static String title = "Title";

public void testclass (Container window){

    JPanel index = new JPanel();

    String index2[] = {title};
    JComboBox index3 = new JComboBox(index2);
    index3.setEditable(false);
    index3.addItemListener(this);
    index.add(index3);

     File folder = new File("images/");
        File[] listOfFiles = folder.listFiles();

        String nr;
        final JPanel panel = new JPanel(new GridLayout(4, 4, 4, 4));

        for (int i = 0; i < listOfFiles.length; i++) {
                nr = "images/" + listOfFiles[i].getName();
                final ImageIcon images = new ImageIcon(nr);
                final JLabel display[] = new JLabel[1];

            for (int n = 0; n < 1; n++){
                display[n] = new JLabel(images);
                panel.add(display[n]);  
         } }
            panel.addMouseListener(new MouseAdapter()

            { public void mouseClicked (MouseEvent e)
            {   //JPanel panel = (JPanel) getComponentAt(e.getPoint());
                JOptionPane.showMessageDialog(null, "Message");
            }}); 


    template = new JPanel(new CardLayout());
    template.add(panel, title);

    window.add(index, BorderLayout.PAGE_START);
    window.add(template, BorderLayout.CENTER);
}
public void itemStateChanged(ItemEvent event){

    CardLayout change = (CardLayout)(template.getLayout());
    change.show(template, (String)event.getItem());
}

private static void userinterface() {

    JFrame showwindow = new JFrame("Window");
    showwindow.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    testclass show = new testclass();
    show.testclass(showwindow.getContentPane());

    showwindow.pack();
    showwindow.setVisible(true);
}

public static void main(String[] args) {
    try {
        UIManager.setLookAndFeel("com.sun.java.swing.plaf.nimbus.NimbusLookAndFeel");
          } catch(Exception e){
          }
    javax.swing.SwingUtilities.invokeLater(new Runnable() {
        public void run() {
            userinterface();
        }
    });
}

}

【问题讨论】:

    标签: java mouseevent cells


    【解决方案1】:

    编辑:正确答案

    感谢您提供代码。我已经调整了一些东西,希望你能理解。

    • 首先,我添加了一个新对象 ImageButton。从我添加 actionListener 开始,你想要的功能就已经完成了(减去漂亮的外观,你将不得不玩弄它,或者问另一个问题)
    • 添加了“dir”字符串,这样您就不必继续复制和粘贴目录地址了。
    • 我不得不通过 userInterface() 方法调整窗口的大小,您应该考虑能够缩短调整参数的顺序,也许是另一个 GUI 对象来将所有这些信息保存在一起。李>

    有几点:

    您编写的代码很好,但需要您进行大量调整(获取窗口大小并反复检查不同大小,如果您调整了窗口大小,鼠标点击可以点击其他您没有点击的图像)不想要!)为了让 mouseListener 工作,我猜你会提供各种各样的图像。

    在你的代码中加入 cmets 可以帮助你和试图帮助你的人。

    无论如何,如果这有帮助,请投票/接受答案,我相信它确实有效。

    祝你好运!


    将这些文件放入你的eclipse并运行它们,如果你调整dir字符串以遵循你的原始目录路径,它应该可以工作。

    testclass.java

    import java.awt.BorderLayout;
    import java.awt.CardLayout;
    import java.awt.Container;
    import java.awt.GridLayout;
    import java.awt.event.ItemEvent;
    import java.awt.event.ItemListener;
    import java.io.File;
    
    import javax.swing.JComboBox;
    import javax.swing.JFrame;
    import javax.swing.JPanel;
    import javax.swing.UIManager;
    
    public class testclass implements ItemListener {
    
    JPanel template;
    final static String title = "Title";
    final String dir = "images/";
    
    public void testclass(Container window) {
    
        JPanel index = new JPanel();
    
        String[] index2 = { title };
        JComboBox index3 = new JComboBox(index2);
        index3.setEditable(false);
        index3.addItemListener(this);
        index.add(index3);
        index.setSize(500, 500);
    
        File folder = new File(dir);
        File[] listOfFiles = folder.listFiles();
    
        String nr;
        final JPanel panel = new JPanel(new GridLayout(4, 4, 4, 4));
    
        for (int i = 0; i < listOfFiles.length; i++) {
            nr = dir + listOfFiles[i].getName();
    
            panel.add(new ImageButton(nr));
    
        }
    
        template = new JPanel(new CardLayout());
        template.add(panel, title);
    
        window.add(template, BorderLayout.CENTER);
        window.add(index, BorderLayout.NORTH);
    
        window.setVisible(true);
    }
    
    public void itemStateChanged(ItemEvent event) {
    
        CardLayout change = (CardLayout) (template.getLayout());
        change.show(template, (String) event.getItem());
    }
    
    private static void userinterface() {
    
        JFrame showwindow = new JFrame("Window");
        showwindow.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    
        testclass show = new testclass();
        show.testclass(showwindow.getContentPane());
    
        showwindow.pack();
        showwindow.setVisible(true);
        showwindow.setSize(500, 500);
    }
    
    public static void main(String[] args) {
        try {
            UIManager
                    .setLookAndFeel("com.sun.java.swing.plaf.nimbus.NimbusLookAndFeel");
        } catch (Exception e) {
        }
        javax.swing.SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                userinterface();
            }
        });
    }
    }
    

    ImageButton.java

    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    
    import javax.swing.ImageIcon;
    import javax.swing.JButton;
    import javax.swing.JOptionPane;
    
    public class ImageButton extends JButton {
    
    private String fileName;
    private ImageIcon image;
    private JButton button;
    
    public ImageButton(String fileName) {
    
        setFileName(fileName);
        setImage(new ImageIcon(fileName));
        setButton(new JButton(getImage()));
    
        this.setIcon(getImage());
    
        this.addActionListener(new ActionListener() {
    
            @Override
            public void actionPerformed(ActionEvent ae) {
    
                JOptionPane.showMessageDialog(null, ae.getSource());
            }
        });
    
    }
    
    public String getFileName() {
        return fileName;
    }
    
    public void setFileName(String fileName) {
        this.fileName = fileName;
    }
    
    public ImageIcon getImage() {
        return image;
    }
    
    public void setImage(ImageIcon image) {
        this.image = image;
    }
    
    public JButton getButton() {
        return button;
    }
    
    public void setButton(JButton button) {
        this.button = button;
    }
    
    }
    

    【讨论】:

    • 我尝试添加 ActionListener,但它与 getComponentAt 相同,它只是无法正常工作,我不知道问题出在哪里。该程序的基础是有一个卡片布局,每张卡片都有一个网格布局,但是当我尝试将 ActionListener 添加到该特定卡片或使用 getComponentAt - Eclipse 似乎并没有将其识别为有效操作。我完全糊涂了。
    • 如果您可以添加更多代码来显示实际的 GUI,这可能会有很大帮助。我的另一个建议是您可以将 listOfFiles for-loop 放入它自己的对象中,然后从那里可以使用 ActionListener。如果无法查看您的其余代码,我很难想到其他任何事情。同样使用显示 for 循环,您实际上并不需要它 - display[0] = new JLabel(images); panel.add(显示[n]); -- 应该可以正常工作。
    • 我添加了更多代码。我不明白为什么使用 MouseEvent 或 ActionListener 都不能让它做我想做的事。我都试过了,但无论我把它放在 for 循环内部还是外部,它似乎都无法识别变量或方法。
    • 非常感谢!在那之前我很迷茫!
    猜你喜欢
    • 2019-10-08
    • 1970-01-01
    • 1970-01-01
    • 2021-08-12
    • 2013-11-15
    • 2013-03-06
    • 1970-01-01
    • 2017-05-04
    • 1970-01-01
    相关资源
    最近更新 更多