使用面板管理pressButton

时间:2015-10-09 20:18:32

标签: java swing

我应该遵循的角色:

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

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

3)在第二个角色中,panel2被突出显示,因此如果我们点击它或者我们尝试将其悬停在面板3上,颜色就不会改变。

我有一个带模型和视图的megacode,所以他们需要展示很多东西。我无法理解的是如何使用Listener来管理它。在每个面板中设置监听器并不方便。那么有些人如何建议我将我的监听器放在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;
    }   

}

1 个答案:

答案 0 :(得分:1)

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