Java 如何使用图像正确设置JButton的大小?

Java 如何使用图像正确设置JButton的大小?,java,swing,user-interface,jframe,jbutton,Java,Swing,User Interface,Jframe,Jbutton,这就是我现在拥有的: 这就是我希望图像看起来的样子: 我有两个java文件,一个扩展了JFrame,另一个基本上是Jpanel ShinyButtons panel = new ShinyButtons(); panel.setLocation(10, 10); getContentPane().add(panel); 另一个是用JButtons扩展JPanel 导入javax.swing.* public class ShinyButtons extends JPanel{

这就是我现在拥有的:

这就是我希望图像看起来的样子:

我有两个java文件,一个扩展了JFrame,另一个基本上是Jpanel

 ShinyButtons panel = new ShinyButtons(); 

 panel.setLocation(10, 10);

 getContentPane().add(panel); 
另一个是用JButtons扩展JPanel 导入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<ROWS; r++) 
        for (int c=0; c<ROWS; c++) 
            buttonTable[r][c] = (byte)(Math.random()*7); 
} 

public byte getButton(int r, int c) { return buttonTable[r][c]; }
}
public类ShinyButtons扩展了JPanel{
公共静态字节RED=0;
公共静态字节=1;
公共静态字节黄色=2;
公共静态字节绿色=3;
公共静态字节蓝色=4;
公共静态字节浅灰色=5;
公共静态字节深灰色=6;
公共静态字节行=8;
专用字节[][]按钮表;
公共ShinyButtons(){
buttonTable=新字节[行][行];
重置按钮();
setboorder(BorderFactory.createEmptyByOrder());
设置大小(552552);
ImageIcon[]图标={newImageIcon(“RedButton.png”)、newImageIcon(“OrangeButton.png”)、newImageIcon(“YellowButton.png”),
新图像图标(“GreenButton.png”)、新图像图标(“BlueButton.png”)、新图像图标(“LightGrayButton.png”),
新的图像图标(“DarkGrayButton.png”)};

对于(int row=0;row而言,最简单的方法如下:

button.setSize(imageIcon.getIconWidth, imageIcon.getIconHeight);
“如何使用图像正确设置JButton的大小?”

根本不要设置大小使用布局管理器。请参阅以了解不同的布局管理器。最明显的是
GridLayout
,它将在您定义的大小的网格中布局组件。如果您查看下面的方法,您将看到我使用的是具有8行8列的
GridLayout
。该方法采用动态网格大小。我向其传递了8。您所要做的就是将所有图像/标签发送到面板。无需指定位置/大小。
GridLayout
将为您解决此问题

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 JPanel createPanel(ImageIcon[]图标,int gridSize){
随机=新随机();
JPanel panel=newjpanel(newgridlayout(gridSize,gridSize));
对于(int i=0;i

完整代码

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();
            }
        });
    }
}
导入java.awt.BorderLayout;
导入java.awt.Color;
导入java.awt.FlowLayout;
导入java.awt.GridLayout;
导入java.util.Random;
导入javax.swing.ImageIcon;
导入javax.swing.JButton;
导入javax.swing.JFrame;
导入javax.swing.JLabel;
导入javax.swing.JPanel;
导入javax.swing.JTextField;
导入javax.swing.SwingUtilities;
导入javax.swing.border.LineBorder;
公共类循环图像{
公共环路图像(){
ImageIcon[]icons=createImageIcons();
JPanel iconPanel=createPanel(图标,8);
JPanel bottomLeftPanel=newjpanel(newflowlayout(FlowLayout.LEADING));
添加(新的JLabel(“分数”);
添加(新的JTextField(10));
JPanel bottomRightPanel=newjpanel(newflowlayout(FlowLayout.training));
添加(新的JButton(“新游戏”));
添加(新建JButton(“退出”);
JPanel-bottomPanel=新的JPanel(新的网格布局(1,2));
添加(bottomLeftPanel);
bottomPanel.add(bottomRightPanel);
JFrame=新JFrame();
frame.add(iconPanel);
框架。添加(底部面板,边框布局。第_页结束);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.pack();
frame.setLocationRelativeTo(空);
frame.setVisible(true);
}
专用JPanel createPanel(ImageIcon[]图标,int gridSize){
随机=新随机();
JPanel panel=newjpanel(newgridlayout(gridSize,gridSize));
对于(int i=0;i