How to make round jbuttons in Java

Firstly, I am a web developer and novice Java programmer. My boss asks me to make this button in the application:

enter image description here

My custom button class needs to extend JButton or BasicButtonUI so that it can be reused.

I did some research on stack overflows, but I did not understand the answers, especially with the time limits from my boss.

+5
source share
4 answers

To do this, you must create your own component.

Set the paintComponent method to JPanel, and inside the paintComponent method draw (i.e. fill) the rounded rectangle 2D in gray:

 RoundRectangle2D roundedRectangle = new RoundRectangle2D.Float(x, y, w, h, 10, 10); g.fill(roundedRectangle); 

(The last two values ​​determine the curvature. Play around until you get what you want)

Now move x, y and reduce the width and height so that when drawing the next rectangle it is inside the gray rectangle, Set the graphic color to blue, then do the following:

 RoundRectangle2D roundedRectangle2 = new RoundRectangle2D.Float(x + 5, y + 5, w - 10, h - 10, 10, 10); g.fill(roundedRectangle2); 

You will also need to add text. To add text, x and y are required. The exact x and y positions can be difficult to compute, so you may need FontMetrics to get some more information about the rectangular shape of the string. Fontmetrics has methods like stringWidth () and getHeight () to help you determine what your x and y should be.

 g.drawString("Click Me", x, y); 

Finally, you need to have a mouse movement listener on the panel. The listener needs to find when the mouse is over the button, and then redraw the component.

Your rectangle can be transferred to the shape object, and a calculation can be made whether the mouse is in shape, For example:

 shape.contains(x,y) 

If it contains, change the color and then call repaint () or updateUI () on the panel.

Note. Your color object must be stored as a class level field in the class so that it can be changed with the mouse.

Hope this helps!

+13
source

If you do not want to draw images yourself using the graphics API or you cannot force the images to come from a graphic designer, you can use them as ImageIcon and use setRolloverIcon () and SetIcon () .

In this case, I will do it like this

 class ButtonRollover { private String normalImagePath; private String rolloverImagePath; public ButtonRollover(String normalImagePath, String rolloverImagePath) { this.normalImagePath = normalImagePath; this.rolloverImagePath = rolloverImagePath; } public void apply(AbstractButton abstractButton) { abstractButton.setBorderPainted(false); abstractButton.setBackground(new Color(0, 0, 0, 0)); abstractButton.setRolloverIcon(createImageIcon(rolloverImagePath)); abstractButton.setIcon(createImageIcon(normalImagePath)); } private ImageIcon createImageIcon(String path) { java.net.URL imgURL = getClass().getResource(path); if (imgURL != null) { return new ImageIcon(imgURL); } else { System.err.println("Couldn't find file: " + path); return null; } } } 

and how to use it. For instance.

 public class Main extends JFrame { public static void main(String[] args) { Main main = new Main(); main.setBackground(Color.WHITE); main.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); main.setSize(640, 480); Container contentPane = main.getContentPane(); ButtonRollover buttonRollover = new ButtonRollover("/bt_normal.png", "/bt_hover.png"); JButton btn = new JButton(); buttonRollover.apply(btn); contentPane.add(btn); main.setVisible(true); } } 

Just put the image files in the classpath.

+4
source

There are ways to do this.

1) JButton has a built-in setIcon API. Here you can install ImageIcon.

2) You can add a mouse listener (mouse entered, mouse out) to change the icons to the desired ones.

3) Make the button round - Apply for the creation of magnificent buttons.

+2
source
 public class Main extends JFrame { public static void main(String[] args) { Main main = new Main(); main.setBackground(Color.WHITE); main.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); main.setSize(640, 480); Container contentPane = main.getContentPane(); ButtonRollover buttonRollover = new ButtonRollover("/bt_normal.png", "/bt_hover.png"); JButton btn = new JButton(); buttonRollover.apply(btn); contentPane.add(btn); main.setVisible(true); } } 
0
source

Source: https://habr.com/ru/post/1203375/


All Articles