我使用GridBagLayout创建了一个包含5个按钮的表单来获取此表单:
这是我的代码:
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);
}
}
这是该课程的超类。
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{
}
}
}
}
答案 0 :(得分:1)
这是一个加载的问题。我首先要说GridBagLayout
对于你想要实现的目标是正常的。我认为你应该投入一些时间来研究:How to Use GridBagLayout
。
您还应该查看&#34; Insets&#34;用于间距选项,并在处理gridwidth
时使用gridheight
,ipadx
甚至ipady
和GridBagConstraints
。
答案 1 :(得分:0)
您也可以使用BoxLayout。 (How to use BoxLayout
)。
您可以根据需要定位组件,如果要在组件之间添加空间,可以在组件中添加空边框或在组件之间放置不可见的组件。
您可以查看this discussion了解详情。