如何添加JButton鼠标位置?

时间:2015-02-28 02:48:17

标签: java swing jbutton

更新

我用mouseEnter和mouseExit修复了它。我添加了鼠标适配器和鼠标监听器来修复它。我这样做是为了当鼠标进入时它是红色的,当它离开时它是黑色的。如果我使按钮不可见并制作我自己的自定义按钮,这仍然有效吗?我还能保留播放文字吗?

我的代码:

package Main;

import java.awt.Color;
import java.awt.Font;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;


public class Game extends JPanel {
public Game() {
    JFrame frame = new JFrame("Tennis Game");

    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setLayout(null);

    final JButton b = new JButton("Play");

    b.setBackground(new Color(220,220,220));
    b.setFocusPainted(false);
    b.setFont(new Font(Font.SANS_SERIF, Font.BOLD, 20));
    b.setBounds(110, 100, 80, 40);
    b.setForeground(Color.BLACK);

    b.addMouseListener(new java.awt.event.MouseAdapter(){

        public void mouseEntered(java.awt.event.MouseEvent evt) {
            b.setForeground(Color.RED);
        }

        public void mouseExited(java.awt.event.MouseEvent evt) {
            b.setForeground(Color.BLACK);
        }

    });

    frame.add(b);
    frame.setSize(300,400);
    frame.setVisible(true);

}



public static void main(String[] args) {
     SwingUtilities.invokeLater(new Runnable() {
         @Override
         public void run() {
             new Game();
         }
     });
 }

 }

谢谢!

1 个答案:

答案 0 :(得分:3)

  1. 您需要使用mouseEnter中的mouseExitMouseListener个事件;
  2. 你不应该,如果你的尝试,你将无可救药; Swing API是围绕使用布局管理器而设计的,试图在没有它们的情况下,将导致您重新发明轮子以解决您遇到的所有问题。
  3. 您将停用当前的边框画(setBorderPainted),然后应用自己的边框,例如LineBorder
  4. 首先查看How to use Mouse ListenersHow to us Borders了解更多详情......

      

    使用mouseEnter我试了但是当我这样做时它不起作用。

    首先,阅读教程How to use Mouse Listeners

    import java.awt.EventQueue;
    import java.awt.GridBagLayout;
    import java.awt.event.MouseAdapter;
    import java.awt.event.MouseEvent;
    import javax.swing.JButton;
    import javax.swing.JFrame;
    import javax.swing.JPanel;
    import javax.swing.UIManager;
    import javax.swing.UnsupportedLookAndFeelException;
    
    public class Test {
    
        public static void main(String[] args) {
            new Test();
        }
    
        public Test() {
            EventQueue.invokeLater(new Runnable() {
                @Override
                public void run() {
                    try {
                        UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                    } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                        ex.printStackTrace();
                    }
    
                    JFrame frame = new JFrame("Testing");
                    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                    frame.add(new TestPane());
                    frame.pack();
                    frame.setLocationRelativeTo(null);
                    frame.setVisible(true);
                }
            });
        }
    
        public class TestPane extends JPanel {
    
            public TestPane() {
                setLayout(new GridBagLayout());
                JButton btn = new JButton("play");
                add(btn);
    
                btn.addMouseListener(new MouseAdapter() {
    
                    @Override
                    public void mouseEntered(MouseEvent e) {
                        btn.setText(btn.getText().toUpperCase());
                    }
    
                    @Override
                    public void mouseExited(MouseEvent e) {
                        btn.setText(btn.getText().toLowerCase());
                    }
    
                });
            }
    
        }
    
    }
    

    但我同意AndrewThompson,你最好使用“普通”图标和“翻转”图标......

    enter image description here

    import java.awt.EventQueue;
    import java.awt.GridBagLayout;
    import java.io.IOException;
    import javax.imageio.ImageIO;
    import javax.swing.ImageIcon;
    import javax.swing.JButton;
    import javax.swing.JFrame;
    import javax.swing.JPanel;
    import javax.swing.UIManager;
    import javax.swing.UnsupportedLookAndFeelException;
    
    public class Test {
    
        public static void main(String[] args) {
            new Test();
        }
    
        public Test() {
            EventQueue.invokeLater(new Runnable() {
                @Override
                public void run() {
                    try {
                        UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                    } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                        ex.printStackTrace();
                    }
    
                    JFrame frame = new JFrame("Testing");
                    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                    frame.add(new TestPane());
                    frame.pack();
                    frame.setLocationRelativeTo(null);
                    frame.setVisible(true);
                }
            });
        }
    
        public class TestPane extends JPanel {
    
            public TestPane() {
                setLayout(new GridBagLayout());
                JButton btn = new JButton();
                add(btn);
    
                try {
                    btn.setIcon(new ImageIcon(
                                    ImageIO.read(getClass().getResource("/Play-Plain.png"))
                    ));
                    btn.setRolloverIcon(new ImageIcon(
                                    ImageIO.read(getClass().getResource("/Play-RollOver.png"))
                    ));
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
    
        }
    
    }