Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java Button Width

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.

like image 616
Dianamu Avatar asked Oct 30 '12 10:10

Dianamu


People also ask

How do you make a button smaller in Java?

Simply delete the frame. setPreferredSize() and the buttons will be small (the frame size will be determined by the preferred sizes of inner components by calling frame.

How do you change the size of a button in Netbeans?

setLayout(new GridLayout(5, 13)); secondPanel. setMaximumSize(new Dimension(520, 200)); for (int i=1; i<=5; i++) { for (int j=1; j<=13; j++) { btn = new JButton(); btn. setPreferredSize(new Dimension(40, 40)); secondPanel. add(btn); } } mainPanel.

What is a JTextField in Java?

JTextField is a lightweight component that allows the editing of a single line of text. For information on and examples of using text fields, see How to Use Text Fields in The Java Tutorial. JTextField is intended to be source-compatible with java.


1 Answers

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

enter image description here

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

  • A Visual Guide to Layout Managers
  • Using Layout Managers

For more information

like image 125
MadProgrammer Avatar answered Nov 14 '22 01:11

MadProgrammer