search
HomeJavaJavaBaseHow to set the size of button with picture in Java

How to set the size of button with picture in Java

Nov 22, 2019 pm 04:42 PM
javapicturesizebuttonset up

How to set the size of button with picture in Java

In projects that require graphical interface programming in the Java part, image settings are often used to beautify buttons, but a very troublesome problem will arise during use. Follow the method:

JButton jb1 = new JButton();
jb1.setBounds(0, 0, 25, 20);
ImageIcon ii = new ImageIcon("images/xxx.png");
jb1.setIcon(ii);

At this point, you will find that the picture in the button does not fill the button as expected, but is placed in the button according to the size of the picture itself. So, what should be done to make the picture have the expected length and width of the button? What about the perfect filling in the button?

In fact, it is very simple. You only need to obtain the length and width of the set button in advance, use the getScaledInstance() method to reconstruct the image, and then construct the Icon object and pass it into the JButton.

The specific implementation process is as follows:

JButton jb1 = new JButton();
jb1.setBounds(0, 0, 25, 20);
ImageIcon ii = new ImageIcon("images/xxx.png");
//根据按钮大小改变图片大小
Image temp = ii.getImage().getScaledInstance(jb1.getWidth(), jb1.getHeight(), ii.getImage().SCALE_DEFAULT);
ii = new ImageIcon(temp);
jb1.setIcon(ii);

Recommended tutorial: java quick start

The above is the detailed content of How to set the size of button with picture in Java. For more information, please follow other related articles on the PHP Chinese website!

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

MantisBT

MantisBT

Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools