Domanda

I have a set of buttons. My JPanel has a layout of GridLayout. I would like any components to go outside the square to get centered. Here is an image: http://screencast.com/t/z86ldR9vh

I would like the Options button to be centered under the group.

Thanks in advance!

È stato utile?

Soluzione

Create another JPanel for the "Options" button and set it to flow layout. For example, say the top panel is called "panel1" and the bottom is "panel2":

// create top panel with first four buttons
JPanel panel1 = new JPanel(new GridLayout(2, 2));
panel1.add(new JButton("Play"));
panel1.add(new JButton("New Game"));
panel1.add(new JButton("Save Game"));
panel1.add(new JButton("Load Game"));

// create bottom panel with "Options" button
JPanel panel2 = new JPanel(new FlowLayout());
panel2.add(new JButton("Options"));

Or, if you would like the complete code for the class (with all the imports):

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JButton;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.GridLayout;

public class Buttons {
    public static void main(String[] args) {
        Buttons gui = new Buttons();
    }

    public Buttons() {
        // create frame
        JFrame frame = new JFrame();
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setSize(new Dimension(250, 150));
        frame.setLayout(new FlowLayout());
        frame.setVisible(true);

        // create top panel with first four buttons
        JPanel panel1 = new JPanel(new GridLayout(2, 2));
        panel1.add(new JButton("Play"));
        panel1.add(new JButton("New Game"));
        panel1.add(new JButton("Save Game"));
        panel1.add(new JButton("Load Game"));

        // create bottom panel with "Options" button
        JPanel panel2 = new JPanel(new FlowLayout());
        panel2.add(new JButton("Options"));

        // add panels to frame
        frame.add(panel1);
        frame.add(panel2);
    }   
}  
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top