简体   繁体   中英

How do you make a column in java swing package

Hey guys so i am making this game tic-tac-toe and wanted to know how to make another column to the right. You will understand better visually:

this is what i made now:

在此处输入图片说明

But if i add another button it just goes down vertically. How do i make it so it goes horizontally to the middle so i can make my tic-tac-toe game.

Code:

import java.awt.Dimension;
import java.awt.Insets;

import javax.swing.BorderFactory;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;

import sun.org.mozilla.javascript.internal.xml.XMLLib.Factory;



public class TicTacToe {
    JFrame frame;
    JPanel contentPane;
    JButton row1col1;
    JButton row1col2;
    JButton row1col3;
    JButton row2col1;
    JButton row2col2;
    JButton row2col3;
    JButton row3col1;
    JButton row3col2;
    JButton row3col3;   


    public TicTacToe() {
        // TODO Auto-generated constructor stub

        frame = new JFrame("Fds");
        contentPane = new JPanel();
        contentPane.setLayout(new BoxLayout(contentPane,BoxLayout.Y_AXIS));
        contentPane.setBorder(BorderFactory.createEmptyBorder
                 (10, 10, 10, 10));

        row1col1 = new JButton();
        row1col1.setMargin( new Insets(10, 10, 10, 10) );
        contentPane.add(row1col1);

        row1col2 = new JButton();
        row1col2.setMargin( new Insets(10, 10, 10, 10) );
        contentPane.add(row1col2);

        row1col3 = new JButton();
        row1col3.setMargin( new Insets(10, 10, 10, 10) );
        contentPane.add(row1col3);


        row2col1 = new JButton();
        row2col1.setMargin( new Insets(10, 10, 10, 10) );
        contentPane.add(row2col1);

        row2col2 = new JButton();
        row2col2.setMargin( new Insets(10, 10, 10, 10) );
        contentPane.add(row2col2);

        row2col3 = new JButton();
        row2col3.setMargin( new Insets(10, 10, 10, 10) );
        contentPane.add(row2col3);



        frame.setContentPane(contentPane);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.pack();
        frame.setVisible(true);


    }


    private static void runGUI() {
         JFrame.setDefaultLookAndFeelDecorated(true);
         TicTacToe greeting = new TicTacToe();
         }
         public static void main(String[] args) {
         /* Methods that create and show a GUI should be
         run from an event-dispatching thread */
         javax.swing.SwingUtilities.invokeLater(new Runnable() {
         public void run() {
         runGUI();
         }
         });


         }


}

It's all about your layout: you have specified the BoxLayout which put the controls vertically.

Just use another one. You can find more about Layout in the documentation

GridLayout seems to be perfect for your porpouse

You will need to use the GridLayout instead of the BoxLayout .

The grid layout manager will then take care of placing your buttons in a grid like fashion.

Something like so (untested):

JButton[] grid = new JButton[9];
this.setLayout(new GridLayout(3,3,1,1));
for(int i = 0; i < 9; i++) {
    JButton button = new JButton();
    ...//Attach event handlers, etc.
    this.add(button);
    grid.add(button);
}

The above code should create a 3 x 3 grid and populate it with buttons.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM