私はこのフォームを取得するための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{
}
}
}
}
私はGBLの使用に同意します。 '大きなボタン'のヒントのいくつかは、使用するフォントサイズを大きくするか、['AbstractButton.setMargin(Instets)']を呼び出すことです(http://docs.oracle.com/javase/8/docs/api/javax/ swing/AbstractButton.html#setMargin-java.awt.Insets-)。 –