Creating a custom button in Java with JButton

后端 未结 4 1875
陌清茗
陌清茗 2020-11-21 11:38

I am trying to create a button that has a custom shape (hexagon), but otherwise acts like a normal JButton would (that is, works with an ActionListener).

I have crea

4条回答
  •  滥情空心
    2020-11-21 12:13

    I know this question has been answered, but you might want to look at using the built-in methods, and using images to draw your button in different states.

    Here is a bit of code I used to generate a custom button.

    BufferedImage startButton = ImageIO.read(getClass().getResource("/icons/standard/buttons/start_backup.png"));
    BufferedImage startButtonHover = ImageIO.read(getClass().getResource("/icons/standard/buttons/start_backup_hover.png"));
    BufferedImage startButtonActive = ImageIO.read(getClass().getResource("/icons/standard/buttons/start_backup_active.png"));
    
    JButton startBackupButton = new JButton(new ImageIcon(startButton));
    startBackupButton.setRolloverIcon(new ImageIcon(startButtonHover));
    startBackupButton.setPressedIcon(new ImageIcon(startButtonActive));
    startBackupButton.setBorder(BorderFactory.createEmptyBorder());
    startBackupButton.setContentAreaFilled(false);
    startBackupButton.setFocusable(false);
    

    You can then add an action listener to it as normal.

提交回复
热议问题