简体   繁体   中英

jPanel and jButton customization

I'm trying to learn how to code Conway's game of life in Java, and I'm getting stuck creating the GUI. I want to make a JPanel within the JFrame, with a larger border at South, and then two buttons in the south border, one for "Play" and one for "Restart." But the Design element won't let me resize or move anything around. I was able to resize the JPanel by going into the code and creating a larger border in the South, but I can't figure out how to resize the JButton. Any ideas?

(I'm using Eclipse Kepler...I hear NetBeans is better at this kind of stuff, should I just ditch Eclipse and try it with NetBeans?)

Here's my code so far:

import java.awt.BorderLayout;
import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import java.awt.Color;
import java.awt.FlowLayout;
import javax.swing.JButton;


public class GameOfLife extends JFrame {

    private JPanel contentPane;

    /**
     * Launch the application.
     */
    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    GameOfLife frame = new GameOfLife();
                    frame.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

    /**
     * Create the frame.
     */
    public GameOfLife() {
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setBounds(100, 100, 518, 508);
        contentPane = new JPanel();
        contentPane.setBorder(new EmptyBorder(10, 10, 50, 10));
        contentPane.setLayout(new BorderLayout(0, 0));
        setContentPane(contentPane);

        JPanel panel = new JPanel();
        panel.setBackground(Color.GRAY);
        contentPane.add(panel, BorderLayout.CENTER);

        JButton btnNewButton = new JButton("New button");
        contentPane.add(btnNewButton, BorderLayout.SOUTH);
    }

}

Basically, instead of doing BorderLayout.SOUTH , I want to manually place it where I want it within the frame. I'd also love to be able to do that with the JPanel--the whole North/West/South/East/Center thing in general seems very constricting. What's the way around it?

Maybe you should look at Swing Layouts in Java documentation:

A Visual Guide to Layout Managers

And the layout which is able to give you the most flexibility is the GridBagLayout but you will write much code to display the User Interface as your needs.

You will have a detailled way to go with the following official tutorial: How to Use GridBagLayout

In your code, you are using the simple BorderLayout which is very simple but not so much configurable.

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