java jbutton大小,如何正确设置图像的JButton大小?

This is what I have now:

HbDNA.png

This is what I want the image to look like:

KZVJ1.png

I have two java files, one extends JFrame and part with Jpanel looks basically

ShinyButtons panel = new ShinyButtons();

panel.setLocation(10, 10);

getContentPane().add(panel);

another one extends JPanel with JButtons

import javax.swing.*;

public class ShinyButtons extends JPanel{

public static byte RED = 0;

public static byte ORANGE = 1;

public static byte YELLOW = 2;

public static byte GREEN = 3;

public static byte BLUE = 4;

public static byte LIGHT_GRAY = 5;

public static byte DARK_GRAY = 6;

public static byte ROWS = 8;

private byte[][] buttonTable;

public ShinyButtons() {

buttonTable = new byte[ROWS][ROWS];

resetButtons();

setBorder(BorderFactory.createEmptyBorder());

setSize(552,552);

ImageIcon[] icons = {new ImageIcon("RedButton.png"), new ImageIcon("OrangeButton.png"), new ImageIcon("YellowButton.png"),

new ImageIcon("GreenButton.png"), new ImageIcon("BlueButton.png"), new ImageIcon("LightGrayButton.png"),

new ImageIcon("DarkGrayButton.png")};

for (int row=0; row<8; row++){

for (int col=0; col<8; col++){

JButton buttons = new JButton();

buttons.setSize(69,69);

buttons.setIcon(icons[(byte)(Math.random()*7)]);

add(buttons);

buttons.setLocation(row*69, col*69);

buttons.setBorder(BorderFactory.createEmptyBorder());

}

}

}

private void resetButtons() {

for (int r=0; r

for (int c=0; c

buttonTable[r][c] = (byte)(Math.random()*7);

}

public byte getButton(int r, int c) { return buttonTable[r][c]; }

}

解决方案

"how to properly set size of JButton with image?"

Don't set the sizes at all Use a Layout manager. See Laying out Components Within a Container to learn the different layout managers. The most obvious one is GridLayout that will layout your components in a grid of a size defined by you. If you look at the method below, you will see I use a GridLayout with 8 rows and 8 cols. The method takes in a dynamic grid size. I passed 8 to it. The all you have to do is all the images/labels to the panel. No need to specify location/size. The GridLayout will take care of that for you.

private JPanel createPanel(ImageIcon[] icons, int gridSize) {

Random random = new Random();

JPanel panel = new JPanel(new GridLayout(gridSize, gridSize));

for (int i = 0; i < gridSize * gridSize; i++) {

int index = random.nextInt(icons.length);

JLabel label = new JLabel(icons[index]);

label.setBorder(new LineBorder(Color.GRAY, 2));

panel.add(label);

}

return panel;

}

TQYAs.png

Full Code

import java.awt.BorderLayout;

import java.awt.Color;

import java.awt.FlowLayout;

import java.awt.GridLayout;

import java.util.Random;

import javax.swing.ImageIcon;

import javax.swing.JButton;

import javax.swing.JFrame;

import javax.swing.JLabel;

import javax.swing.JPanel;

import javax.swing.JTextField;

import javax.swing.SwingUtilities;

import javax.swing.border.LineBorder;

public class CircleImages {

public CircleImages() {

ImageIcon[] icons = createImageIcons();

JPanel iconPanel = createPanel(icons, 8);

JPanel bottomLeftPanel = new JPanel(new FlowLayout(FlowLayout.LEADING));

bottomLeftPanel.add(new JLabel("Score: "));

bottomLeftPanel.add(new JTextField(10));

JPanel bottomRightPanel = new JPanel(new FlowLayout(FlowLayout.TRAILING));

bottomRightPanel.add(new JButton("New Game"));

bottomRightPanel.add(new JButton("Quit"));

JPanel bottomPanel = new JPanel(new GridLayout(1, 2));

bottomPanel.add(bottomLeftPanel);

bottomPanel.add(bottomRightPanel);

JFrame frame = new JFrame();

frame.add(iconPanel);

frame.add(bottomPanel, BorderLayout.PAGE_END);

frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

frame.pack();

frame.setLocationRelativeTo(null);

frame.setVisible(true);

}

private JPanel createPanel(ImageIcon[] icons, int gridSize) {

Random random = new Random();

JPanel panel = new JPanel(new GridLayout(gridSize, gridSize));

for (int i = 0; i < gridSize * gridSize; i++) {

int index = random.nextInt(icons.length);

JLabel label = new JLabel(icons[index]);

label.setBorder(new LineBorder(Color.GRAY, 2));

panel.add(label);

}

return panel;

}

private ImageIcon[] createImageIcons() {

String[] files = {"blackcircle.png",

"bluecircle.png",

"greencircle.png",

"greycircle.png",

"orangecircle.png",

"redcircle.png",

"yellowcircle.png"

};

ImageIcon[] icons = new ImageIcon[files.length];

for (int i = 0; i < files.length; i++) {

icons[i] = new ImageIcon(getClass().getResource("/circles/" + files[i]));

}

return icons;

}

public static void main(String[] args) {

SwingUtilities.invokeLater(new Runnable() {

public void run() {

new CircleImages();

}

});

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值