更改JLabel Background on MouseRelease事件 [英] Change JLabel Background on MouseRelease Event

查看:222
本文介绍了更改JLabel Background on MouseRelease事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试编写一个Java程序,当您点击JFrame中的某个元素时,JLabel的背景将被更改。事件处理方法调用以更改背景的功能在从事件调用时不起作用,但是只要您在独立于任何事件的地方调用该方法,它就会起作用。有没有人知道如何改变MouseRelease事件的背景,因为使用像这样的事件处理方法不起作用?

I am trying to write a Java program that when you click on a certain element in the JFrame, the background of a JLabel is changed. The function that the event handling method calls to change the background does not work when called from the event, however it does work whenever you call the method somewhere independent of any event. Does anyone know how you would go about changing the background on MouseRelease event, since using an event handling method like so doesn't work?

private void MouseReleaseEvent(java.awt.event.MouseEvent evt) {                                             
    // Do some random stuff, like a println.
    // This println works as a normal println statement should.
    System.out.println("Hello World!");
    // Now let's try the method that changes the background.
    // This method will not work within this event handler, but trying it outside
    // of it does work.
    changeBackground();
}   




  • 注意:以上代码仅供显示我的问题。它实际上不是我的代码的一部分。我不会发布代码,因为我正在和朋友一起工作,他不想释放代码,即使它只是一小部分,没有什么有价值的可以做。

  • 编辑:根据所有尝试帮助我的请求,我已将NetBeans项目工作区上传到FileDropper一个zip文件,您可以下载 here 。您会注意到,当我从主类调用方法时,它会闪烁另一个图像,并返回到当前的图像,但是当您释放鼠标点击播放时,它不会调用它,即使这应该。可以随意尝试代码。

    As per the request of all of those trying to help me, I have uploaded the NetBeans project workspace to FileDropper in a zip file, which you can download here. You will notice that when I call the method from the main class, it does flash another image and return to the current one as I want, but when you release the mouse from clicking on "Play", it does not call it, even though it should. Feel free to experiment with the code as you wish.

    编辑2:由于还要求我发布代码不想下载工作区,这里是:

    Edit 2: Since it has also been requested that I post the code for those that do not want to download the workspace, here it is:

    Simon.java

    Simon.java

    package simon;
    
    public class Simon {
        public static void main(String[] args) {
            // Load all of the other class files into this main class.
            SimonGUI SimonGUI = new SimonGUI();
            // For some reason, setting the background color doesn't work when we
            // try doing it within the GUI development environment, but it works
            // here, so that's what we are going to do.
            SimonGUI.getContentPane().setBackground(new java.awt.Color(0, 0, 0));
            // Make the form visible to the user. It is now ready for use.
            SimonGUI.setVisible(true);
            SimonGUI.playBackPattern();
        }
    }
    

    SimonGUI.java *

    SimonGUI.java*

    /*
     * To change this template, choose Tools | Templates
     * and open the template in the editor.
     */
    package simon;
    
    import java.awt.Component;
    import java.text.NumberFormat;
    import java.util.ArrayList;
    import java.util.Random;
    import java.util.logging.Level;
    import java.util.logging.Logger;
    import javax.swing.JOptionPane;
    
    /**
     *
     * @author Administrator
     */
    public class SimonGUI extends javax.swing.JFrame {
        // Declare class level variables.
        boolean aboutTextVisible = false;
        boolean mainMenuVisible = true;
        boolean userResponseAllowed = false;
        private Component frame;
    
        /**
         * Creates new form SimonGUI
         */
        public SimonGUI() {
            initComponents();
        }
    
        /**
         * This method is called from within the constructor to initialize the form.
         * WARNING: Do NOT modify this code. The content of this method is always
         * regenerated by the Form Editor.
         */
        @SuppressWarnings("unchecked")
        // <editor-fold defaultstate="collapsed" desc="Generated Code">                          
        private void initComponents() {
    
            lblPlayButton1 = new javax.swing.JLabel();
            lblPlayButton = new javax.swing.JLabel();
            lblSimonBoard = new javax.swing.JLabel();
    
            lblPlayButton1.setIcon(new javax.swing.ImageIcon(getClass().getResource("/simon/resources/MenuPlay.png"))); // NOI18N
            lblPlayButton1.setName("lblPlayButton"); // NOI18N
    
            setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
            setTitle("Simon");
            setBackground(new java.awt.Color(0, 0, 0));
            setMaximumSize(new java.awt.Dimension(600, 600));
            setMinimumSize(new java.awt.Dimension(600, 600));
            setName("frmSimon"); // NOI18N
            setPreferredSize(new java.awt.Dimension(600, 600));
            setResizable(false);
            getContentPane().setLayout(null);
    
            lblPlayButton.setIcon(new javax.swing.ImageIcon(getClass().getResource("/simon/resources/MenuPlay.png"))); // NOI18N
            lblPlayButton.setCursor(new java.awt.Cursor(java.awt.Cursor.HAND_CURSOR));
            lblPlayButton.setName("lblPlayButton"); // NOI18N
            lblPlayButton.addMouseListener(new java.awt.event.MouseAdapter() {
                public void mouseReleased(java.awt.event.MouseEvent evt) {
                    lblPlayButtonMouseReleased(evt);
                }
            });
            getContentPane().add(lblPlayButton);
            lblPlayButton.setBounds(150, 20, 317, 58);
            lblPlayButton.getAccessibleContext().setAccessibleName("lblPlayerButton");
    
            lblSimonBoard.setIcon(new javax.swing.ImageIcon(getClass().getResource("/simon/resources/Original.PNG"))); // NOI18N
            lblSimonBoard.setToolTipText("");
            lblSimonBoard.setFocusable(false);
            lblSimonBoard.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
            lblSimonBoard.setName("lblSimon"); // NOI18N
            getContentPane().add(lblSimonBoard);
            lblSimonBoard.setBounds(-1, 93, 600, 497);
            lblSimonBoard.getAccessibleContext().setAccessibleName("lblSimon");
    
            getAccessibleContext().setAccessibleName("frmSimon");
            getAccessibleContext().setAccessibleDescription("");
    
            pack();
        }// </editor-fold>                        
    
        private void lblPlayButtonMouseReleased(java.awt.event.MouseEvent evt) {                                            
            // This handles the start of Simon gameplay.
            toggleBoard();
            playBackPattern();
        }                                           
    
        public void toggleBoard() {
            // Handles toggling between the main menu and the simon board.
            if (mainMenuVisible == true)
            {
                // Board is visible, let's toggle it off.
                lblPlayButton.setBounds(700, 700, 317, 58);
                mainMenuVisible = false;
                lblSimonBoard.setIcon(new javax.swing.ImageIcon(getClass().getResource("/simon/resources/Original.PNG")));
            }
            else
            {
                // Board is not visible, let's toggle it on.
                lblPlayButton.setBounds(120, 140, 317, 58);
                mainMenuVisible = true;
                lblSimonBoard.setIcon(new javax.swing.ImageIcon(getClass().getResource("/simon/resources/SimonBackground.PNG")));
            }
        }
    
        /**
         * @param args the command line arguments
         */
        public static void main(String args[]) {
            /*
             * Set the Nimbus look and feel
             */
            //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
            /*
             * If Nimbus (introduced in Java SE 6) is not available, stay with the
             * default look and feel. For details see
             * http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
             */
            try {
                for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                    if ("Nimbus".equals(info.getName())) {
                        javax.swing.UIManager.setLookAndFeel(info.getClassName());
                        break;
                    }
                }
            } catch (ClassNotFoundException ex) {
                java.util.logging.Logger.getLogger(SimonGUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
            } catch (InstantiationException ex) {
                java.util.logging.Logger.getLogger(SimonGUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
            } catch (IllegalAccessException ex) {
                java.util.logging.Logger.getLogger(SimonGUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
            } catch (javax.swing.UnsupportedLookAndFeelException ex) {
                java.util.logging.Logger.getLogger(SimonGUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
            }
            //</editor-fold>
    
            /*
             * Create and display the form
             */
            java.awt.EventQueue.invokeLater(new Runnable() {
    
                public void run() {
                    new SimonGUI().setVisible(true);
                }
            });
        }
        // Variables declaration - do not modify                     
        private javax.swing.JLabel lblPlayButton;
        private javax.swing.JLabel lblPlayButton1;
        public javax.swing.JLabel lblSimonBoard;
        // End of variables declaration                   
    
        public void flashColor(int colorID) {
            // Flash one of the colors. The value of the color that we are going to
            // flash is passed here, and the color is flashed.
            // Pause for a fraction of a second before blinking the color. In case
            // something goes wrong during the pause, we have an error which we hope
            // to never use that will catch the program's weakness.
            try {
                Thread.sleep(250);
            } catch (InterruptedException ex) {
                JOptionPane.showMessageDialog(frame,"A fatal error has caused Simon to stop working.","Simon • Fatal Error",JOptionPane.ERROR_MESSAGE);
                System.exit(0);
            }
            // Flash the respective color.
            if(colorID == 1) {
                // Flash Red
                lblSimonBoard.setIcon(new javax.swing.ImageIcon(getClass().getResource("/simon/resources/Red.PNG")));
            }
            else if(colorID == 2){
                // Flash Green
                lblSimonBoard.setIcon(new javax.swing.ImageIcon(getClass().getResource("/simon/resources/Green.PNG")));
            }
            else if (colorID == 3) {
                // Flash Yellow
                lblSimonBoard.setIcon(new javax.swing.ImageIcon(getClass().getResource("/simon/resources/Yellow.PNG")));
            }
            else {
                // Flash Blue
                lblSimonBoard.setIcon(new javax.swing.ImageIcon(getClass().getResource("/simon/resources/Blue.PNG")));
            }
            // Wait for a fraction of a second before we return to the regular
            // board. In case something bad happens here while we pause, we have an
            // error message that we hope will never be used.
            try {
                Thread.sleep(250);
                lblSimonBoard.setIcon(new javax.swing.ImageIcon(getClass().getResource("/simon/resources/Original.PNG")));
            } catch (InterruptedException ex) {
                JOptionPane.showMessageDialog(frame,"A fatal error has caused Simon to stop working.","Simon • Fatal Error",JOptionPane.ERROR_MESSAGE);
                System.exit(0);
            }
        }
        public void userResponseEnabled(boolean responsePermitted) {
            // Toggles the class-level variable "userResponseAllowed", which
            // controls if the user can click on the buttons on the Simon board and
            // give a response.
    
            // Pass the value to the class-level variable.
            userResponseAllowed = responsePermitted;
        }
    
        // "Actual Game Code"
        int score = 0;
        ArrayList pattern = new ArrayList();
        int playerColor = 0;
        int y = 0;
        boolean correct = true;
        private SimonGUI SimonGUI;
    
        int certainColor = 1;
        int numberOfColors = 0;
    
        public void playBackPattern() {
            // Normally, the playBackPattern() method has more than just this line,
            // but for demo purposes, this line is all that is needed.
            flashColor(4);
        }
    }
    

    *这些代码中的一些是使用Swing自动生成的

    *Some of this code is automatically generated using the Swing GUI designer add-on for NetBeans.

    推荐答案

    根据示例代码,至少有两件事我可以这是令人担忧的

    Based on the example code, there are, at least, two things that I can that are worrisome


    1. 使用 null 布局。每个系统都是独一无二的,它具有自己的字体,屏幕分辨率和DPI属性。为了克服这个问题,为了更容易地编写可以在不同计算机上工作的复杂的用户界面,但是不同的操作系统,开发了布局管理API。强烈建议您查看在容器内部放置组件

    2. 在事件调度主题中使用 Thread.sleep

    1. The use of null layouts. Every system is unique, with it's own font, screen resolution and DPI properties. To overcome this issue, and to make it easier to write sophisticated user interfaces that could work across not only different computers, but different operating systems, the layout management API was developed. It strongly recommend that you take a look at Laying Out Components Within a Container
    2. The use of Thread.sleep within the Event Dispatching Thread.

    Swing是一个单线程框架。也就是说,需要对UI的所有交互和修改在事件调度线程的上下文中进行。阻止此线程的任何操作将阻止其处理,尤其是绘制请求。

    Swing is a single threaded framework. That is, it is required that all interactions and modifications to the UI take place within the context of the Event Dispatching Thread. Any actions which blocks this thread will prevent it from processing, amongst other things, paint requests.

    这意味着当您执行 Thread.sleep ,你是阻止EDT更新UI,直到你退出的方法之后...

    This mean that when you execute Thread.sleep, you are prevent the EDT from updating the UI until AFTER the method you are in exits...

    我可以想到的最简单的解决方案您将使用 javax.swing.Timer 。以下是一个演示这个想法的简单示例。

    The simplest solution I can think of for you is to use a javax.swing.Timer. The following is a simple example which demonstrates this idea.

    import java.awt.BorderLayout;
    import java.awt.Dimension;
    import java.awt.EventQueue;
    import java.awt.Image;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import javax.imageio.ImageIO;
    import javax.swing.ImageIcon;
    import javax.swing.JButton;
    import javax.swing.JFrame;
    import javax.swing.JLabel;
    import javax.swing.JPanel;
    import javax.swing.Timer;
    import javax.swing.UIManager;
    import javax.swing.UnsupportedLookAndFeelException;
    
    public class SimpleSimon {
    
        public static void main(String[] args) {
            new SimpleSimon();
        }
    
        public SimpleSimon() {
            EventQueue.invokeLater(new Runnable() {
                @Override
                public void run() {
                    try {
                        UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                    } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                    }
    
                    JFrame frame = new JFrame("Test");
                    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                    frame.add(new TestPane());
                    frame.pack();
                    frame.setLocationRelativeTo(null);
                    frame.setVisible(true);
                }
            });
        }
    
        public class TestPane extends JPanel {
    
            private JLabel board;
            private Timer timer;
            private int flashCount;
    
            private int flashColor;
            private Image[] colors;
    
            private Image original;
    
            public TestPane() {
    
                colors = new Image[4];
                try {
                    colors[0] = ImageIO.read(getClass().getResource("/resources/Blue.PNG"));
                    colors[1] = ImageIO.read(getClass().getResource("/resources/Green.PNG"));
                    colors[2] = ImageIO.read(getClass().getResource("/resources/Red.PNG"));
                    colors[3] = ImageIO.read(getClass().getResource("/resources/Yellow.PNG"));
                    original = ImageIO.read(getClass().getResource("/resources/Original.PNG"));
                } catch (Exception e) {
                    e.printStackTrace();
                }
    
                setLayout(new BorderLayout());
                JButton btn = new JButton("Play");
                board = new JLabel();
                board.setHorizontalAlignment(JLabel.CENTER);
                board.setVerticalAlignment(JLabel.CENTER);
                add(board);
    
                timer = new Timer(250, new ActionListener() {
                    @Override
                    public void actionPerformed(ActionEvent e) {
                        if (flashCount == 0) {
                            board.setIcon(new ImageIcon(colors[flashColor]));
                        } else {
                            board.setIcon(new ImageIcon(original));
                            timer.stop();
                        }
                        flashCount++;
                    }
                });
                timer.setRepeats(true);
    
                add(btn, BorderLayout.SOUTH);
                btn.addActionListener(new ActionListener() {
                    @Override
                    public void actionPerformed(ActionEvent e) {
                        flashCount = 0;
                        flashColor = (int)Math.round(Math.random() * 3);
                        timer.restart();
                    }
                });
            }
    
            @Override
            public Dimension getPreferredSize() {
                return new Dimension(650, 400);
            }
        }
    }
    

    基本上,我会更改你的 playBackPattern 方法来启动一个 javax.swing.Timer ,如果你想要的话,可以调用你的 flashColor 方法,例如...

    Basically, I would change your playBackPattern method to start a javax.swing.Timer which could, if you wanted it to, call your flashColor method, for example...

    请参阅 Swing中的并发更多的细节。

    更新了一个回叫功能

    这提供了一个提供回拨功能的非常基本的示例,以便在动画停止时,它会调用回指定的对象

    This provides a very basic example of providing a call back feature, so that when the animation stops, it will make a call back to the specified object.

    个人而言,我宁愿做的稍微有些不同,但是这个例子已经复杂化了。

    Personally, I would have preferred to do it slightly different, but it was over complicating the example...

    import java.awt.BorderLayout;
    import java.awt.Color;
    import java.awt.Dimension;
    import java.awt.EventQueue;
    import java.awt.Graphics2D;
    import java.awt.GridLayout;
    import java.awt.Image;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.awt.image.BufferedImage;
    import javax.swing.ImageIcon;
    import javax.swing.JButton;
    import javax.swing.JFrame;
    import javax.swing.JLabel;
    import javax.swing.JPanel;
    import javax.swing.Timer;
    import javax.swing.UIManager;
    import javax.swing.UnsupportedLookAndFeelException;
    
    public class SimpleSimon {
    
        public static void main(String[] args) {
            new SimpleSimon();
        }
    
        public SimpleSimon() {
            EventQueue.invokeLater(new Runnable() {
                @Override
                public void run() {
                    try {
                        UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                    } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                    }
    
                    JFrame frame = new JFrame("Test");
                    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                    frame.add(new TestPane());
                    frame.pack();
                    frame.setLocationRelativeTo(null);
                    frame.setVisible(true);
                }
            });
        }
    
        public class TestPane extends JPanel {
    
            private JLabel board;
            private Timer timer;
            private int flashCount;
            private int flashTimes;
    
            private int flashColor;
            private Image[] colors;
    
            private Image original;
    
            private AnimationCallBack callBack;
    
            public TestPane() {
    
                colors = new Image[4];
                try {
                    colors[0] = createImage(Color.BLUE);
                    colors[1] = createImage(Color.GREEN);
                    colors[2] = createImage(Color.RED);
                    colors[3] = createImage(Color.YELLOW);
                    original = createImage(Color.WHITE);
                } catch (Exception e) {
                    e.printStackTrace();
                }
    
                setLayout(new BorderLayout());
                board = new JLabel();
                board.setHorizontalAlignment(JLabel.CENTER);
                board.setVerticalAlignment(JLabel.CENTER);
                add(board);
    
                timer = new Timer(250, new ActionListener() {
                    @Override
                    public void actionPerformed(ActionEvent e) {
                        if (flashCount < flashTimes) {
                            if (flashCount % 2 == 0) {
                                board.setIcon(new ImageIcon(colors[flashColor]));
                            } else {
                                board.setIcon(new ImageIcon(original));
                            }
                        } else {
                            board.setIcon(new ImageIcon(original));
                            timer.stop();
                            // Animation has stopped, make call back...
                            if (callBack != null) {
                                callBack.animationDone();
                            }
                        }
                        flashCount++;
                    }
                });
                timer.setRepeats(true);
    
                JPanel buttons = new JPanel(new GridLayout(0, 2));
    
                final JButton btnOne = new JButton("#1");
                buttons.add(btnOne);
                btnOne.addActionListener(new ActionListener() {
                    @Override
                    public void actionPerformed(ActionEvent e) {
                        flashCount = 0;
                        flashTimes = 1;
                        flashColor = (int) Math.round(Math.random() * 3);
                        timer.restart();
    
                        btnOne.setEnabled(false);
                        // Set the call back so we know when the
                        // animation has stopped...
                        callBack = new AnimationCallBack() {
                            @Override
                            public void animationDone() {
                                btnOne.setEnabled(true);
                            }
                        };
                    }
                });
    
                final JButton btnTwo = new JButton("#2");
                buttons.add(btnTwo);
                btnTwo.addActionListener(new ActionListener() {
                    @Override
                    public void actionPerformed(ActionEvent e) {
                        flashCount = 0;
                        flashTimes = 3;
                        flashColor = (int) Math.round(Math.random() * 3);
                        timer.restart();
                        btnTwo.setEnabled(false);
                        // Set the call back so we know when the
                        // animation has stopped...
                        callBack = new AnimationCallBack() {
                            @Override
                            public void animationDone() {
                                btnTwo.setEnabled(true);
                            }
                        };
                    }
                });
                add(buttons, BorderLayout.SOUTH);
            }
    
            protected Image createImage(Color color) {
    
                BufferedImage img = new BufferedImage(400, 400, BufferedImage.TYPE_INT_ARGB);
                Graphics2D g2d = img.createGraphics();
                g2d.setColor(color);
                g2d.fillRect(0, 0, 400, 400);
    
                return img;
    
            }
    
            @Override
            public Dimension getPreferredSize() {
                return new Dimension(400, 400);
            }
        }
    
        public interface AnimationCallBack {
            public void animationDone();
        }
    }
    

    这篇关于更改JLabel Background on MouseRelease事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆