Heronimus Lex Heronimus Lex - 1 year ago 96
Java Question

Which layout should I be using?

I have created a form with 5 buttons using a GridBagLayout to get this form:
This is the form I have created

What I want is for The buttons to be bigger and more evenly spaced like this:Desired Form

Here is my code:

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);

}

}


This is the superclass of the class in question.

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{

}
}
}
}

Answer Source

This is a loaded question. I'll start by saying GridBagLayout is just fine for what you're trying to achieve. I think you should invest some time looking into: How to Use GridBagLayout.

You should also look into "Insets" for spacing options, and utilizing gridwidth, gridheight, and maybe even ipadx and ipady when dealing with GridBagConstraints.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download