java - 我应该使用哪种布局?

标签 java swing layout layout-manager gridbaglayout

我使用 GridBagLayout 创建了一个包含 5 个按钮的表单来获取此表单: This is the form I have created

我想要的是按钮更大并且间隔更均匀,如下所示:Desired Form

这是我的代码:

package com.GUI;
import java.awt.Color;
import javax.swing.*;
import com.seaglasslookandfeel.*;


public class JFramePlus extends JFrame{
    public JFramePlus(){
        super("OmegaBrain");
        setSize(1000,800);
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        setVisible(true);
        getContentPane().setBackground(Color.black);
        setResizable(false);

    }

}

这是相关类的父类(super class)。

package com.GUI;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
import java.util.Stack;



class GamePlay extends JFramePlus implements ActionListener{

    //Create Stack
    Stack sequence = new Stack();

    //Declare Variables
    String greekSequence;
    int stackCount;
    int timeLeft;
    static int optionNo;

    //Create Constraints
    GridBagConstraints c = new GridBagConstraints();

    //Defining new objects
    JLabel timeDisplay, sequenceViewer;
    JButton mainMenu, input1, input2, input3, input4, input5;
    JPanel timerPane, centerPane, exitPane;
    Timer t;


    GamePlay(){


        //Create Labels
        timeDisplay = new JLabel();
        sequenceViewer = new JLabel();

        //Create Panels
        timerPane = new JPanel();
        centerPane = new JPanel();     
        exitPane = new JPanel();

        //Change layout of centerPane
        centerPane.setLayout(new GridBagLayout());

        //Creates JButtons
        mainMenu = new JButton("Main Menu");

        input1 = new JButton("Ξ");
            c.gridx = 0;
            c.gridy = 1;
        centerPane.add(input1, c);    

        input2 = new JButton("Ω");
            c.gridx = 2;
            c.gridy = 1;
        centerPane.add(input2, c);

        input3 = new JButton("Ψ");
            c.gridx = 4;
            c.gridy = 1;
        centerPane.add(input3, c);

        input4 = new JButton("Φ");
            c.gridx = 1;
            c.gridy = 2;
        centerPane.add(input4, c);

        input5 = new JButton("Γ");
            c.gridx = 3;
            c.gridy = 2;
        centerPane.add(input5, c);

        //Create Timer
        t = new Timer(1000, this);


        //Changes the size of the font
        timeDisplay.setFont(timeDisplay.getFont().deriveFont(64.0f));

        //Generate Sequence
        sequenceGenerator();


        //Add components to panels
        timerPane.add(timeDisplay);

        centerPane.add(sequenceViewer, c);

        exitPane.add(mainMenu);



        //add panels to frame
        add(timerPane, BorderLayout.LINE_END);
        add(centerPane, BorderLayout.CENTER);
        add(exitPane, BorderLayout.SOUTH);


        //Change colors to fit theme
        timeDisplay.setForeground(Color.WHITE);
        sequenceViewer.setForeground(Color.WHITE);
        timerPane.setBackground(Color.BLACK);
        centerPane.setBackground(Color.BLACK);
        exitPane.setBackground(Color.BLACK);


        //Add ActionListeners to buttons
        mainMenu.addActionListener(this);
        input1.addActionListener(this);
        input2.addActionListener(this);
        input3.addActionListener(this);
        input4.addActionListener(this);
        input5.addActionListener(this);
    }

    public void sequenceGenerator(){
        sequence.push(1 + (int)(Math.random() * optionNo));
        stackCount++;

        greekSequence = "";
        for(int i = 0; i < stackCount; i++){
            if (sequence.get(i) == 1){
                greekSequence = greekSequence + 'Ξ';
            }
        }
        sequenceViewer.setText(greekSequence);
    }




    void startTimer() {
        t.start();

    }



    public void actionPerformed(ActionEvent evt) {
        Object source = evt.getSource();
        if(source == t){
            timeDisplay.setText(String.valueOf(timeLeft));
            timeLeft--;

            if(timeLeft == -1){
                t.stop();
            }
        }

        else if(source == mainMenu){
            int yesNo = JOptionPane.showConfirmDialog(  
                null,
                "Are you sure you want to exit? Your current score will be saved as it is." ,
                "Exit Game?",
                JOptionPane.YES_NO_OPTION);

            if(yesNo == JOptionPane.YES_OPTION){
                dispose();
                mainMenu menu = new mainMenu();
            }

            else{

            }
        }
    }
}

最佳答案

这是一个有内涵的问题。首先我会说 GridBagLayout 非常适合您想要实现的目标。我认为你应该花一些时间研究:How to Use GridBagLayout .

您还应该研究“Insets”的间距选项,并利用 gridwidthgridheight,甚至可能 ipadx处理 GridBagConstraints 时的 ipady

关于java - 我应该使用哪种布局?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40628433/

相关文章:

java - Java 格式的 JFileChooser 路径

java - 使用 Eclipse 和 Rserve 从 Java 调用 R 的简单程序

java Arrays.sort 二维数组

java - 如何在 Java 代码中为数据报套接字设置重用地址选项?

java - 通过数字行从 JTextArea 获取文本

java - 在浏览器上显示 java 代码的动态 Web 项目?

c - 关于C程序内存布局的问题

layout - Flutter 是否支持级联网格?

layout - 使用 Gridstack 或 gridster 进行小部件布局?

java - 如何检测将要删除的内容