Java按钮宽度

Dia*_*amu 4 java size swing button width

我试过使用它,它不起作用

singleplayerButton.setBounds(20, 20, 200, 100);
Run Code Online (Sandbox Code Playgroud)

我不知道为什么,有人可以帮我解决这个问题吗?

我的完整页面代码在这里

package gmine;

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class gmine implements ActionListener {
        JFrame interfaceFrame;
        JButton singleplayerButton, multiplayerButton, optionsButton, quitButton;

        public gmine() {
                JFrame.setDefaultLookAndFeelDecorated(false);
                interfaceFrame = new JFrame("G-Mine B0.4");
                interfaceFrame.setSize(800,600);
                interfaceFrame.setLayout(new GridLayout(9,1, 20, 15));

                singleplayerButton = new JButton("SinglePLayer");
                singleplayerButton.addActionListener(this);
                interfaceFrame.add(singleplayerButton);
                singleplayerButton.setBounds(20, 20, 200, 100);

                multiplayerButton = new JButton("MultiPlayer");
                multiplayerButton.addActionListener(this);
                interfaceFrame.add(multiplayerButton);

                optionsButton = new JButton("Options");
                optionsButton.addActionListener(this);
                interfaceFrame.add(optionsButton);

                quitButton = new JButton("Quit");
                quitButton.addActionListener(this);
                interfaceFrame.add(quitButton);

                interfaceFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                interfaceFrame.setVisible(true);
        }

        public void actionPerformed(ActionEvent a) {

        }

        public static void main(String[] args) {
                new gmine();
        }
}
Run Code Online (Sandbox Code Playgroud)

我试图完成使按钮更小,所以不要触摸页面的一面.

Mad*_*mer 9

我个人会使用一个布局管理器,它可以让您更灵活地决定按钮的布局方式,并努力尊重您的组件首选尺寸,但让您可以根据需要自由调整...

对我来说,就是这样 GridBagLayout

在此输入图像描述

public class ButtonsLayout {

    public static void main(String[] args) {
        new ButtonsLayout();
    }

    public ButtonsLayout() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (ClassNotFoundException ex) {
                } catch (InstantiationException ex) {
                } catch (IllegalAccessException ex) {
                } catch (UnsupportedLookAndFeelException ex) {
                }

                JFrame frame = new JFrame("Test");
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.setLayout(new BorderLayout());
                frame.add(new MenuPane());
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }

    public class MenuPane extends JPanel {

        public MenuPane() {
            setLayout(new GridBagLayout());

            JButton singleplayerButton = new JButton("SinglePLayer");
            JButton multiplayerButton = new JButton("MultiPlayer");
            JButton optionsButton = new JButton("Options");
            JButton quitButton = new JButton("Quit");

            GridBagConstraints gbc = new GridBagConstraints();
            gbc.gridx = 0;
            gbc.gridy = 0;
            gbc.fill = GridBagConstraints.HORIZONTAL;
            gbc.ipadx = 20;
            gbc.ipady = 20;

            add(singleplayerButton, gbc);
            gbc.gridy++;
            add(multiplayerButton, gbc);
            gbc.gridy++;
            add(optionsButton, gbc);
            gbc.gridy++;
            add(quitButton, gbc);
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

在这里,我用ipadxipadyGridBagConstraints通过布局管理器来增加组件的宽度和高度,以及使用的HORIZONTAL填充使所有的组件相同的宽度.

看一下

欲获得更多信息