Java的按钮宽度 [英] Java Button Width

查看:168
本文介绍了Java的按钮宽度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试过使用此并不起作用。

  singleplayerButton.setBounds(20,20,200,100);

我不知道为什么,虽然,任何人可以帮助我这个?

我的整版code是这里

 包gmine;进口java.awt中的*。
java.awt.event中导入*。
进口的javax.swing *。公共类gmine实现的ActionListener {
        JFrame的interfaceFrame;
        JButton的singleplayerButton,multiplayerButton,optionsButton,quitButton;        公共gmine(){
                JFrame.setDefaultLookAndFeelDecorated(假);
                interfaceFrame =新的JFrame(G-矿B0.4);
                interfaceFrame.setSize(800600);
                interfaceFrame.setLayout(新网格布局(9,1,20,15));                singleplayerButton =的新的JButton(单人);
                singleplayerButton.addActionListener(本);
                interfaceFrame.add(singleplayerButton);
                singleplayerButton.setBounds(20,20,200,100);                multiplayerButton =的新的JButton(多人);
                multiplayerButton.addActionListener(本);
                interfaceFrame.add(multiplayerButton);                optionsButton =的新的JButton(选项);
                optionsButton.addActionListener(本);
                interfaceFrame.add(optionsButton);                quitButton =的新的JButton(退出);
                quitButton.addActionListener(本);
                interfaceFrame.add(quitButton);                interfaceFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                interfaceFrame.setVisible(真);
        }        公共无效的actionPerformed(ActionEvent的一个){        }        公共静态无效的主要(字串[] args){
                新gmine();
        }
}

我尝试完成使按键更小,所以不触及页面的一面。


解决方案

我个人使用,会给你更多的灵活性,决定按钮的布局方式,使一个巨大的努力满足您的组件$ P $的布局管理器pferred大小,但给你自由作出调整,因为你需要...

对于我来说,这是的GridBagLayout

 公共类ButtonsLayout {    公共静态无效的主要(字串[] args){
        新ButtonsLayout();
    }    公共ButtonsLayout(){
        EventQueue.invokeLater(新的Runnable(){
            @覆盖
            公共无效的run(){
                尝试{
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                }赶上(ClassNotFoundException的前){
                }赶上(InstantiationException前){
                }赶上(IllegalAccessException前){
                }赶上(UnsupportedLookAndFeelException前){
                }                JFrame的帧=新的JFrame(测试);
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.setLayout(新的BorderLayout());
                frame.add(新MenuPane());
                frame.pack();
                frame.setLocationRelativeTo(NULL);
                frame.setVisible(真);
            }
        });
    }    公共类MenuPane继承JPanel {        公共MenuPane(){
            的setLayout(新的GridBagLayout());            JButton的singleplayerButton =的新的JButton(单人);
            JButton的multiplayerButton =的新的JButton(多人);
            JButton的optionsButton =的新的JButton(选项);
            JButton的quitButton =的新的JButton(退出);            GridBagConstraints的GBC =新的GridBagConstraints();
            gbc.gridx = 0;
            gbc.gridy = 0;
            gbc.fill = GridBagConstraints.HORIZONTAL;
            gbc.ipadx = 20;
            gbc.ipady = 20;            加(singleplayerButton,GBC);
            gbc.gridy ++;
            加(multiplayerButton,GBC);
            gbc.gridy ++;
            加(optionsButton,GBC);
            gbc.gridy ++;
            加(quitButton,GBC);
        }
    }
}

在这里,我使用 ipadx ipady 的GridBagConstraints 通过布局管理器,以增加组件的宽度和高度,以及使用水平填以使所有的组件相同的宽度。

看一看

有关更多信息

I've tried to use this and it doesn't work

singleplayerButton.setBounds(20, 20, 200, 100);

I dont know why though, can anybody help me out with this?

My full page code is here

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

im trying to accomplish making the buttons smaller, so not touching the side of the page.

解决方案

I personally would use a layout manager that will give you more flexibility in deciding how the buttons are laid out and makes a great effort to honor your components preferred size, but gives you the freedom to make adjustments to as you need...

For me, that's 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);
        }
    }
}

Here, I've used ipadx and ipady of the GridBagConstraints to increase the width and height of the components through the layout manager, as well as using the HORIZONTAL fill to make all the components the same width.

Have a look at

For more information

这篇关于Java的按钮宽度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆