java - 使用面板管理 pressButton

标签 java swing

我应该遵循的角色:

1)当点击面板时,它的颜色会改变。但是,如果我们第二次重新单击同一面板,颜色不会改变,因为它仍然被单击 boolean 值 isPressed = true 或 false。

2)当单击panel1(按住)并将鼠标移动到panel2(否则,panel2突出显示),然后释放鼠标单击。 Panel1 和 panel2 采用相同的颜色。 boolean 值 isHighlighted = true 或 false。

3)在第二个角色中,panel2 被突出显示,因此如果我们单击它或尝试将鼠标悬停在 panel3 上,颜色不会改变。

我有一个带有模型和 View 的大型代码,所以它们有很多东西要展示。我无法理解的是如何使用监听器来管理它。将监听器放在每个面板中并不方便。那么如何建议我将监听器放在 mainPanel 中并从中进行管理???。 enter image description here

编辑

这是我的代码:

package mouseeventdemo;

/*
* MouseEventDemo.java
*/

import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseListener;
import java.awt.event.MouseEvent;

import javax.swing.*;

public class MouseEventDemo extends JPanel implements MouseListener {
    BlankArea blankArea1;
    BlankArea blankArea2;
    JTextArea textArea;
    BlankArea  label; 
    static final String NEWLINE = System.getProperty("line.separator");

    public static void main(String[] args) {
        /* Use an appropriate Look and Feel */
        try {
            //UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
            //UIManager.setLookAndFeel("com.sun.java.swing.plaf.gtk.GTKLookAndFeel");
            UIManager.setLookAndFeel("javax.swing.plaf.metal.MetalLookAndFeel");
        } catch (UnsupportedLookAndFeelException ex) {
            ex.printStackTrace();
        } catch (IllegalAccessException ex) {
            ex.printStackTrace();
        } catch (InstantiationException ex) {
            ex.printStackTrace();
        } catch (ClassNotFoundException ex) {
            ex.printStackTrace();
        }
        /* Turn off metal's use of bold fonts */
        UIManager.put("swing.boldMetal", Boolean.FALSE);
        //Schedule a job for the event dispatch thread:
        //creating and showing this application's GUI.
        javax.swing.SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                createAndShowGUI();
            }
        });
    }

    /**
     * Create the GUI and show it.  For thread safety,
     * this method should be invoked from the
     * event dispatch thread.
     */
    private static void createAndShowGUI() {
        //Create and set up the window.
        JFrame frame = new JFrame("MouseEventDemo");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        //Create and set up the content pane.
        JComponent newContentPane = new MouseEventDemo();
        newContentPane.setOpaque(true); //content panes must be opaque
        frame.setContentPane(newContentPane);

        //Display the window.
        frame.pack();
        frame.setVisible(true);
    }

    public MouseEventDemo() {

        super(new GridLayout(0,1));
        blankArea1 = new BlankArea();
        blankArea1.setName("YELLOW");
        add(blankArea1);
        blankArea2 = new BlankArea();
        blankArea2.setName("RED");
        add(blankArea2);
        textArea = new JTextArea();
        textArea.setEditable(false);
        JScrollPane scrollPane = new JScrollPane(textArea);
        scrollPane.setVerticalScrollBarPolicy(
                JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
        scrollPane.setPreferredSize(new Dimension(200, 75));
        add(scrollPane);
        // add(test,BorderLayout.CENTER);
        //Register for mouse events on blankArea and the panel.
        blankArea1.addMouseListener(this);
        blankArea2.addMouseListener(this);
        //addMouseListener(this);

        /*addMouseMotionListener(new MouseAdapter() {
            public void mouseMoved(MouseEvent e) {
                eventOutput("Mouse moved main panel", e);
            }

            public void mouseDragged(MouseEvent e) {
                eventOutput("Mouse dragged main panel ", e);
            }
        });*/

        setPreferredSize(new Dimension(450, 450));
        setBorder(BorderFactory.createEmptyBorder(20,20,20,20));
    }

    void eventOutput(String eventDescription, MouseEvent e) {
        textArea.append(eventDescription + " detected on "
                + e.getComponent().getName()
                + "." + NEWLINE);
        textArea.setCaretPosition(textArea.getDocument().getLength());
    }

    public void mousePressed(MouseEvent e) {
       label = (BlankArea ) e.getSource();
       eventOutput("Mouse pressed (# of clicks: "+ e.getClickCount() + ")", e);
    }

    public void mouseReleased(MouseEvent e) {
        label = (BlankArea ) e.getSource();
        eventOutput("Mouse released (# of clicks: " + e.getClickCount() + ")", e);
    }

    public void mouseEntered(MouseEvent e) {
        label = (BlankArea ) e.getSource();
        eventOutput("Mouse entered", e);
    }

    public void mouseExited(MouseEvent e) {
        label = (BlankArea ) e.getSource();
        eventOutput("Mouse exited", e);
    }

    public void mouseClicked(MouseEvent e) {
        eventOutput("Mouse clicked (# of clicks: "+ e.getClickCount() + ")", e);
    }
}


    class BlankArea extends JLabel {
      Dimension minSize = new Dimension(100, 50);
      public boolean isPressed;
      public boolean isHilighted;

    public BlankArea(Color color) {
        setBackground(color);
        setOpaque(true);
        setBorder(BorderFactory.createLineBorder(Color.black));
    }

    public BlankArea() {
        isPressed = false;
        isHilighted = false;
        setOpaque(true);
        setBorder(BorderFactory.createLineBorder(Color.black));
    }

    public Dimension getMinimumSize() {
        return minSize;
    }

    public Dimension getPreferredSize() {
        return minSize;
    }   

}

最佳答案

对于面板中的每个按钮添加以下内容:buttonName.addActionListener(this);。然后在您的 actionPerformed 方法中,执行所有计算和限制。

关于java - 使用面板管理 pressButton,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33046476/

相关文章:

java - 如果 HashMap 被构造并且 "filled"作为 LinkedHashMap,它是否会在下一次读取时保留其元素的顺序?

java - 如何在JFrame中设置默认图标?

java - JLabel 文本未出现

java - 更改 GUI 上的颜色

java - 当 JButton 监听器工作时更改 JLabel 文本

java - 无法初始化 sessionFactory。 Spring hibernate 3

java - 更改嵌套对象的值 - MongoDB Java

java - 无法从数据库填充 Javafx TableView

java - 无法执行目标org.apache.hadoop:hadoop-maven-plugins:2.4.2-SNAPSHOT:protoc

java - 在 JDialog 中创建 JTabbedPane 并使框架可用