【问题标题】:how to properly set size of JButton with image?如何正确设置带有图像的 JButton 的大小?
【发布时间】:2014-03-01 00:43:44
【问题描述】:

这就是我现在拥有的:

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

我有两个 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]; }
}

【问题讨论】:

    标签: java swing user-interface jframe jbutton


    【解决方案1】:

    "如何正确设置带有图片的 JButton 的大小?"

    不要设置尺寸使用布局管理器。请参阅Laying out Components Within a Container 了解不同的布局管理器。最明显的一个是GridLayout,它会将您的组件布局在您定义的大小的网格中。如果你看看下面的方法,你会看到我使用了一个GridLayout,它有 8 行和 8 列。该方法采用动态网格大小。我给了它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;
    }
    

    完整代码

    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();
                }
            });
        }
    }
    

    【讨论】:

      【解决方案2】:

      最简单的方法是这样的:

      button.setSize(imageIcon.getIconWidth, imageIcon.getIconHeight);
      

      【讨论】:

        猜你喜欢
        • 2013-08-14
        • 2013-07-26
        • 1970-01-01
        • 2014-11-14
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-06-15
        • 1970-01-01
        相关资源
        最近更新 更多