单击时使JPanel突出显示

时间:2013-05-25 06:22:22

标签: java swing jpanel highlight mouse-listeners

所以我的框架中有几个Jpanels,其想法是,当选择其中一个面板时,它会突出显示。这只是第一步,因为最后,我希望能够有按钮作用于面板,但只有突出显示的按钮。

我在想我需要在面板上使用某种鼠标滑块,但我不知道如何实现它以获得我想要的效果。

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.border.Border;

public class CustomComponent {
    Border emptyBorder  = BorderFactory.createEmptyBorder(1,1,1,1);
    Border selectBorder = BorderFactory.createLineBorder(Color.blue);
    JPanel[] panels;

    private JPanel getContent() {
        JLabel label = new JLabel();
        label.setText("fffffffff");
        label.setBounds(5, 5, 25, 25);

        JLabel label2 = new JLabel();
        label2.setText("HHHHHHHHHH");
        label2.setBounds(25, 25, 25, 25);

        JLabel label3 = new JLabel();
        label3.setText("YYYYYYYY");
        label3.setBounds(50, 50, 25, 25);

        JPanel mainPanel = new JPanel();
        mainPanel.setSize(new Dimension(300,300));
        JPanel panel1 = new JPanel();
        panel1.add(label);
        JPanel panel2 = new JPanel();
        panel2.add(label2);
        JPanel panel3 = new JPanel();
        panel3.add(label3);

        panel1.setBackground(Color.WHITE);
        panel2.setBackground(Color.MAGENTA);
        panel3.setBackground(Color.orange);

        panel1.setBorder(emptyBorder);
        panel2.setBorder(emptyBorder);
        panel3.setBorder(emptyBorder);

        panels = new JPanel[] { panel1, panel2, panel3 };

        mainPanel.add(panel1);
        mainPanel.add(panel2);
        mainPanel.add(panel3);
        return mainPanel;
    }

    public static void main(String[] args) {
        JFrame frame = new JFrame("Test");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.add(new CustomComponent().getContent());
        frame.pack();
        frame.setVisible(true);
    }
}

1 个答案:

答案 0 :(得分:6)

以下示例代码显示当鼠标位于JPanel之一时如何实现突出显示。如果您想要点击而不是悬停,请实施mousePressedmouseReleased方法,而不是mouseEnteredmouseExited

enter image description here

import javax.swing.*;
import javax.swing.border.Border;
import java.awt.*;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;

public class MouseHoverDemo
{
    public static void main(String[] args)
    {
        new MouseHoverDemo();
    }

    MouseHoverDemo()
    {
        JFrame jFrame = new JFrame("Mouse Hover Demo");
        jFrame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        jFrame.setLayout(new GridLayout(5,5));
        for(int i=0;i<25;i++) jFrame.add(new CustomPanel());
        jFrame.pack();
        jFrame.setLocationRelativeTo(null);
        jFrame.setVisible(true);
    }

    class CustomPanel extends JPanel implements MouseListener
    {
        Border blackBorder = BorderFactory.createLineBorder(Color.BLACK);
        Border redBorder = BorderFactory.createLineBorder(Color.RED,5);
        CustomPanel()
        {
            addMouseListener(this);
            setBorder(blackBorder);
            setFocusable(true);
        }

        @Override
        public Dimension getPreferredSize()
        {
            return new Dimension(50, 50);
        }

        @Override public void mouseClicked(MouseEvent e){}
        @Override public void mousePressed(MouseEvent e){}
        @Override public void mouseReleased(MouseEvent e){}

        @Override
        public void mouseEntered(MouseEvent e)
        {
            setBorder(redBorder);
        }

        @Override
        public void mouseExited(MouseEvent e)
        {
            setBorder(blackBorder);
        }
    }
}

<强>更新

对于新增功能,请将customPanel的代码更改为此

class CustomPanel extends JPanel implements MouseListener
    {
        boolean isHighlighted;
        Border blackBorder = BorderFactory.createLineBorder(Color.BLACK);
        Border redBorder = BorderFactory.createLineBorder(Color.RED,5);
        CustomPanel()
        {
            addMouseListener(this);
            setBorder(blackBorder);
            setFocusable(true);
        }

        @Override
        public Dimension getPreferredSize()
        {
            return new Dimension(200, 100);
        }

        @Override public void mouseClicked(MouseEvent e)
        {
            if(isHighlighted) setBorder(blackBorder);
            else setBorder(redBorder);
            isHighlighted=!isHighlighted;
        }

        @Override public void mousePressed(MouseEvent e){}
        @Override public void mouseReleased(MouseEvent e){}
        @Override public void mouseEntered(MouseEvent e){}
        @Override public void mouseExited(MouseEvent e){}
    }