java-制作按钮

前端之家收集整理的这篇文章主要介绍了java-制作按钮 前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

我在这里为我的游戏制作菜单,可以按“开始”按钮在屏幕上导航.我正在尝试为该类实现一个单独的按钮,但是按照我布置CustomButtons类的方式,它只能以一个具有功能的按钮来工作,为了解决此问题,我决定制作一个单独的“按钮”方法,其中包含按钮的参数.我在绘画组件中调用了此控件,以确保将其显示在屏幕上,但是仅将文本“ START”显示在屏幕上.按钮的背景颜色,边框,字体等不会随调用而改变.

  1. public class CustomButton extends JButton implements MouseListener {
  2. Dimension size = new Dimension(100,50);
  3. boolean hover = false;
  4. boolean click = false;
  5. boolean isMethodCalled = false;
  6. String text = "";
  7. public CustomButton(String text,Button bb) {
  8. setVisible(true);
  9. setFocusable(true);
  10. setContentAreaFilled(false);
  11. setBorderPainted(false);
  12. this.text = text;
  13. addMouseListener(this);
  14. }
  15. public void Button(Graphics g) {
  16. g.setColor(new Color(255,255,hover ? 180 : 102 ));
  17. g.fillRect(0,250,7);
  18. g.fillRect(0,7,150);
  19. g.setColor(Color.ORANGE); // button background color
  20. g.fillRect(14,14,222,122);
  21. g.setColor(Color.WHITE); // text color
  22. g.setFont(Font.decode("arial-BOLD-24"));
  23. FontMetrics metrics = g.getFontMetrics();
  24. int width = metrics.stringWidth(text);
  25. g.drawString(text,17,40);
  26. }
  27. public void paintComponent(Graphics g) {
  28. super.paintComponent(g);
  29. Button menu = new Button();
  30. }
  31. public void setButtonText(String text) {
  32. this.text = text;
  33. }
  34. public String getButtonText(String text) {
  35. return text;
  36. }
  37. public void mouseEntered(MouseEvent e) {
  38. hover = true;
  39. }
  40. public void mouseExited(MouseEvent e) {
  41. hover = false;
  42. }
  43. public void mousePressed(MouseEvent e) {
  44. click = true;
  45. }
  46. public void mouseReleased(MouseEvent e) {
  47. click = false;
  48. }
  49. public void mouseClicked(MouseEvent e) {
  50. }
  51. }

任何人都知道我该怎么做,以便一旦从“ Buttons”方法调用该按钮后该按钮便能正常工作,以便在将所有图形设置都设置在paintComponent方法中的情况下,该按钮的显示效果应与原来一样.

这不是当前正在发生的事情.我不希望这种情况发生:

这是我想对按钮进行的​​操作:

最佳答案
要获得所需的自定义外观,最好是自定义按钮扩展JLabel.下面的代码演示了如何通过扩展JLabel来编写自定义按钮.

此按钮支持单击事件.我们可以添加ActionListeners来监听点击事件.当用户将鼠标悬停在其上方时,它将背景颜色更改为棕色.

  1. import javax.swing.*;
  2. import java.awt.*;
  3. import java.awt.event.*;
  4. import java.util.ArrayList;
  5. import java.util.List;
  6. public class CustomButton extends JLabel implements MouseListener {
  7. private List<ActionListener> actionListeners = new ArrayList<>();
  8. public CustomButton(String text) {
  9. super(text);
  10. setOpaque(true);
  11. setForeground(Color.white);
  12. setBackground(Color.orange);
  13. setFont(getFont().deriveFont(40.0f));
  14. addMouseListener(this);
  15. }
  16. public void addActionListener(ActionListener listener) {
  17. actionListeners.add(listener);
  18. }
  19. public void removeActionListener(ActionListener listener) {
  20. actionListeners.remove(listener);
  21. }
  22. @Override
  23. public void mouseClicked(MouseEvent e) {
  24. for (ActionListener listener : actionListeners) {
  25. listener.actionPerformed(new ActionEvent(this,"click"));
  26. }
  27. }
  28. @Override
  29. public void mousePressed(MouseEvent e) {
  30. }
  31. @Override
  32. public void mouseReleased(MouseEvent e) {
  33. }
  34. @Override
  35. public void mouseEntered(MouseEvent e) {
  36. setBackground(new Color(185,122,87));
  37. }
  38. @Override
  39. public void mouseExited(MouseEvent e) {
  40. setBackground(Color.orange);
  41. }
  42. public static void main(String[] args) {
  43. JFrame frame = new JFrame();
  44. frame.setDefaultCloSEOperation(JFrame.EXIT_ON_CLOSE);
  45. frame.getContentPane().setBackground(Color.darkGray);
  46. frame.getContentPane().setLayout(new FlowLayout());
  47. CustomButton customButton = new CustomButton("START");
  48. customButton.addActionListener(new ActionListener() {
  49. @Override
  50. public void actionPerformed(ActionEvent e) {
  51. JOptionPane.showMessageDialog(frame,"Button clicked");
  52. }
  53. });
  54. frame.getContentPane().add(customButton);
  55. frame.setBounds(300,200,400,300);
  56. frame.setVisible(true);
  57. }
  58. }

猜你在找的Java相关文章