Reputation: 11
I am struggling with JButton
s, specifically with keeping them all the same size, as it seems that adding anything to a button causes it to stretch to accommodate said addition.
As a result, a button labeled 'yes' won't be the same size as a button labelled as 'no', even if while blank both buttons should have been more than large enough to comfortably contain either string.
Essentially, I would like to know whether there is a way to label buttons without altering the dimensions of said buttons.
I am employing a GridBagLayout
, here is a relevant sample of my code for reference:
Edit
I managed to get the effect I was looking for by using Icons... You probably won't be able to see it in it's entirety, as I am calling the Icons directly from my hard drive... Regardless, I'll leave the completed code for reference...
Game.java:
package com.Sitrom.Words.Window;
import java.awt.Canvas;
import java.awt.Graphics;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import java.io.IOException;
public class Game extends Canvas implements Runnable{
private static final long serialVersionUID = 8433819262022712795L;
private boolean running = false;
private Thread thread;
public synchronized void start(){
if(running){
return;
}
running = true;
thread = new Thread(this);
thread.start();
}
public void run() {
}
public static void main(String[]args) throws IOException{
new Window(800,600,"Words",new Game());
}
}
Window.java:
package com.Sitrom.Words.Window;
import java.io.IOException;
import java.io.InputStream;
import java.io.PipedInputStream;
import java.io.PipedOutputStream;
import java.io.PrintStream;
import java.io.PrintWriter;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class Window extends PanePrimary{
public static class Nav{
public static boolean A1;
public static boolean A_B1;
public static boolean A_C1;
public static boolean A_D1;
public static boolean A_E1;
}
public Window(int w, int h, String title, Game game) throws IOException{
JFrame frame = new JFrame("Words");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLocationRelativeTo(null);
frame.setLocation(0, 0);
frame.setExtendedState(JFrame.MAXIMIZED_BOTH);
frame.setUndecorated(true);
frame.add(game);
PaneCore((JPanel) frame.getContentPane());
frame.pack();
game.start();
frame.setVisible(true);
}
}
PanePrimary.java:
package com.Sitrom.Words.Window;
import java.awt.AlphaComposite;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Font;
import java.awt.Graphics2D;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.TextField;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.PipedInputStream;
import java.io.PipedOutputStream;
import java.io.PrintStream;
import java.io.PrintWriter;
import java.util.List;
import java.util.Scanner;
import javax.annotation.processing.Messager;
import javax.imageio.ImageIO;
import javax.swing.BorderFactory;
import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
//import javax.swing.JTextField;
import com.Sitrom.Words.Window.Window.Nav;
public class PanePrimary {
public static void main(String[] args){
return;
}
public static void PaneCore(JPanel paneC){
if(Nav.A1 == true){
System.out.println(".");
paneC.removeAll();
Pane2(paneC);
}else{
System.out.println("..");
paneC.removeAll();
Pane1(paneC);
}
}
public static String repeat(int count, String with) {
return new String(new char[count]).replace("\0", with);
}
public static String repeat(int count) {
return repeat(count, " ");
}
public static void Pane1(JPanel pane1) {
JButton button;
final JTextArea textA = new JTextArea(11, 60);
JScrollPane scroll;
pane1.setBackground(Color.black);
pane1.setVisible(true);
pane1.setLayout(new GridBagLayout());
GridBagConstraints c = new GridBagConstraints();
scroll = new JScrollPane(textA);
c.fill = GridBagConstraints.BOTH;
c.anchor = GridBagConstraints.PAGE_START;
c.insets = new Insets(10,10,0,10);
c.ipady = 300;
c.ipadx = 300;
c.weightx = 1;
c.weighty = 0.8;
c.gridwidth = 6;
c.gridx = 0;
c.gridy = 0;
textA.setFont(new Font("8bitoperator JVE", Font.BOLD, 25));
textA.append(" Segment A0... ");
textA.append((new String(new char[2]).replace("\0", "\n")));
textA.append(" ~~~~~~~~~");
textA.append((new String(new char[3]).replace("\0", "\n")));
textA.append(" Begin?");
textA.append((new String(new char[3]).replace("\0", "\n")));
textA.append(" ~~~~~~~~~");
textA.append((new String(new char[2]).replace("\0", "\n")));
textA.append("* Yes (Y)");
textA.append((new String(new char[1]).replace("\0", "\n")));
textA.append("* No (N)");
textA.setBackground(Color.black);
textA.setForeground(Color.white);
scroll.setBorder(BorderFactory.createLineBorder(Color.white));
textA.setLineWrap(true);
textA.setWrapStyleWord(false);
textA.setEditable(false);
pane1.add(scroll, c);
button = new JButton();
c.fill = GridBagConstraints.BOTH;
c.anchor = GridBagConstraints.BELOW_BASELINE;
c.insets = new Insets(15,10,10,10);
c.ipady = 40;
c.ipadx = 40;
c.weightx = 0.25;
c.weighty = 0.2;
c.gridwidth = 1;
c.gridx = 0;
c.gridy = 1;
button.setIcon(new ImageIcon("C:\\Users\\Exmortis\\Pictures\\Projects\\Icon_Y.png"));
button.setPressedIcon(new ImageIcon("C:\\Users\\Exmortis\\Pictures\\Projects\\PressedIcon_Y.png"));
button.setBackground(Color.black);
button.setBorder(BorderFactory.createLineBorder(Color.white));
button.setFocusPainted(false);
button.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent ae) {
Nav.A1 = true;
pane1.setVisible(false);
PaneCore(pane1);
}});
pane1.add(button, c);
button = new JButton();
c.fill = GridBagConstraints.BOTH;
c.anchor = GridBagConstraints.BELOW_BASELINE;
c.insets = new Insets(15,10,10,10);
c.ipady = 40;
c.ipadx = 40;
c.weightx = 0.25;
c.weighty = 0.2;
c.gridwidth = 1;
c.gridx = 1;
c.gridy = 1;
button.setBackground(Color.black);
button.setIcon(new ImageIcon("C:\\Users\\Exmortis\\Pictures\\Projects\\Icon_Blank1.png"));
button.setPressedIcon(new ImageIcon("C:\\Users\\Exmortis\\Pictures\\Projects\\PressedIcon_Blank1.png"));
button.setBorder(BorderFactory.createLineBorder(Color.white));
button.setFocusPainted(false);
pane1.add(button, c);
button = new JButton();
c.fill = GridBagConstraints.BOTH;
c.anchor = GridBagConstraints.BELOW_BASELINE;
c.insets = new Insets(15,10,10,10);
c.ipady = 40;
c.ipadx = 40;
c.weightx = 0.25;
c.weighty = 0.2;
c.gridwidth = 1;
c.gridx = 2;
c.gridy = 1;
button.setBackground(Color.black);
button.setIcon(new ImageIcon("C:\\Users\\Exmortis\\Pictures\\Projects\\Icon_Blank1.png"));
button.setPressedIcon(new ImageIcon("C:\\Users\\Exmortis\\Pictures\\Projects\\PressedIcon_Blank1.png"));
button.setBorder(BorderFactory.createLineBorder(Color.white));
button.setFocusPainted(false);
pane1.add(button, c);
button = new JButton();
c.fill = GridBagConstraints.BOTH;
c.anchor = GridBagConstraints.BELOW_BASELINE;
c.insets = new Insets(15,10,10,10);
c.ipady = 40;
c.ipadx = 40;
c.weightx = 0.25;
c.weighty = 0.2;
c.gridwidth = 1;
c.gridx = 3;
c.gridy = 1;
//button.setFont(new Font("8bitoperator JVE", Font.BOLD, 25));
button.setBackground(Color.black);
//button.setForeground(Color.white);
button.setIcon(new ImageIcon("C:\\Users\\Exmortis\\Pictures\\Projects\\Icon_N.png"));
button.setPressedIcon(new ImageIcon("C:\\Users\\Exmortis\\Pictures\\Projects\\PressedIcon_N.png"));
//button.setMargin(null);
//button.setContentAreaFilled(false);
button.setBorder(BorderFactory.createLineBorder(Color.white));
button.setFocusPainted(false);
button.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent ae) {
System.exit(0);
}});
pane1.add(button, c);
}
public static void Pane2(JPanel pane2){
pane2.setBackground(Color.black);
pane2.setVisible(true);
JScrollPane scroll;
final JTextArea textA = new JTextArea(11, 30);
JButton button;
pane2.setLayout(new GridBagLayout());
GridBagConstraints c = new GridBagConstraints();
scroll = new JScrollPane(textA);
c.fill = GridBagConstraints.BOTH;
c.anchor = GridBagConstraints.PAGE_START;
c.insets = new Insets(10,10,0,10);
c.ipady = 300;
c.ipadx = 300;
c.weightx = 1;
c.weighty = 0.8;
c.gridwidth = 6;
c.gridx = 0;
c.gridy = 0;
textA.append("Segment A1..." + (new String(new char[5]).replace("\0", "\n") + "Introductory exposition..." + (new String(new char[5]).replace("\0", "\n") + "You may travel North, South, east, or West...")));
textA.setFont(new Font("8bitoperator JVE", Font.BOLD, 25));
textA.setBackground(Color.black);
textA.setForeground(Color.white);
scroll.setBorder(BorderFactory.createLineBorder(Color.white));
textA.setLineWrap(true);
textA.setWrapStyleWord(false);
textA.setEditable(false);
pane2.add(scroll, c);
button = new JButton("");
c.fill = GridBagConstraints.BOTH;
c.anchor = GridBagConstraints.BELOW_BASELINE;
c.insets = new Insets(15,10,10,10);
c.ipady = 40;
c.ipadx = 40;
c.weightx = 0.25;
c.weighty = 0.2;
c.gridwidth = 1;
c.gridx = 0;
c.gridy = 1;
button.setBackground(Color.black);
button.setIcon(new ImageIcon("C:\\Users\\Exmortis\\Pictures\\Projects\\Icon_N.png"));
button.setPressedIcon(new ImageIcon("C:\\Users\\Exmortis\\Pictures\\Projects\\PressedIcon_N.png"));
button.setFocusPainted(false);
button.setBorder(BorderFactory.createLineBorder(Color.white));
button.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent ae) {
Nav.A1 = false;
Nav.A_B1 = true;
pane2.setVisible(false);
PaneCore(pane2);
}});
pane2.add(button, c);
button = new JButton("");
c.fill = GridBagConstraints.BOTH;
c.anchor = GridBagConstraints.BELOW_BASELINE;
c.insets = new Insets(15,10,10,10);
c.ipady = 40;
c.ipadx = 40;
c.weightx = 0.25;
c.weighty = 0.2;
c.gridwidth = 1;
c.gridx = 1;
c.gridy = 1;
button.setBackground(Color.black);
button.setIcon(new ImageIcon("C:\\Users\\Exmortis\\Pictures\\Projects\\Icon_S.png"));
button.setPressedIcon(new ImageIcon("C:\\Users\\Exmortis\\Pictures\\Projects\\PressedIcon_S.png"));
button.setFocusPainted(false);
button.setBorder(BorderFactory.createLineBorder(Color.white));
button.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent ae) {
Nav.A1 = false;
Nav.A_C1 = true;
pane2.setVisible(false);
PaneCore(pane2);
}});
pane2.add(button, c);
button = new JButton("");
c.fill = GridBagConstraints.BOTH;
c.anchor = GridBagConstraints.BELOW_BASELINE;
c.insets = new Insets(15,10,10,10);
c.ipady = 40;
c.ipadx = 40;
c.weightx = 0.25;
c.weighty = 0.2;
c.gridwidth = 1;
c.gridx = 2;
c.gridy = 1;
button.setBackground(Color.black);
button.setIcon(new ImageIcon("C:\\Users\\Exmortis\\Pictures\\Projects\\Icon_E.png"));
button.setPressedIcon(new ImageIcon("C:\\Users\\Exmortis\\Pictures\\Projects\\PressedIcon_E.png"));
button.setFocusPainted(false);
button.setBorder(BorderFactory.createLineBorder(Color.white));
button.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent ae) {
Nav.A1 = false;
Nav.A_D1 = true;
pane2.setVisible(false);
PaneCore(pane2);
}});
pane2.add(button, c);
button = new JButton("");
c.fill = GridBagConstraints.BOTH;
c.anchor = GridBagConstraints.BELOW_BASELINE;
c.insets = new Insets(15,10,10,10);
c.ipady = 40;
c.ipadx = 40;
c.weightx = 0.25;
c.weighty = 0.2;
c.gridwidth = 1;
c.gridx = 3;
c.gridy = 1;
button.setBackground(Color.black);
button.setIcon(new ImageIcon("C:\\Users\\Exmortis\\Pictures\\Projects\\Icon_W.png"));
button.setPressedIcon(new ImageIcon("C:\\Users\\Exmortis\\Pictures\\Projects\\PressedIcon_W.png"));
button.setFocusPainted(false);
button.setBorder(BorderFactory.createLineBorder(Color.white));
button.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent ae) {
Nav.A1 = false;
Nav.A_E1 = true;
pane2.setVisible(false);
PaneCore(pane2);
}});
pane2.add(button, c);
}
}
Upvotes: 1
Views: 42
Reputation: 3622
Can you add more code and perhaps a screenshot to show what your application currently looks like (see stackoverflow.com/help/mcve for more details on what an ideal code example on Stack Overflow could look like)?
I often prefer to use sub panels with simple layout managers over using a single panel with a large number of components and a complex layout manager. You could consider using multiple panels and/or different layout managers (This is a nice overview of some of the layout managers that are available by default: A Visual Guide to Layout Managers.)
As a short example, this piece of code creates a panel with two buttons using a GridLayout
(with gaps and a border to add some spacing):
import java.awt.*;
import javax.swing.*;
import javax.swing.border.*;
public class ButtonWidths {
public static void main(final String[] arguments) {
SwingUtilities.invokeLater(() -> new ButtonWidths().createAndShowGui());
}
private void createAndShowGui() {
final JFrame frame = new JFrame("Stack Overflow");
frame.setBounds(100, 100, 240, 200);
frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
final JPanel panel = new JPanel(new GridLayout(2, 1, 28, 28));
panel.setBorder(new EmptyBorder(28, 28, 28, 28));
final JButton yesButton = new JButton("Yes");
final JButton noButton = new JButton("No");
panel.add(yesButton);
panel.add(noButton);
frame.getContentPane().add(panel);
frame.setVisible(true);
System.out.println("yesButton: " + yesButton.getSize());
System.out.println("noButton: " + noButton.getSize());
}
}
Setting the minimum size?
The GridBagLayout
should use the button's minimum (and/or preferred) sizes, so this might be a way to make the width of the buttons equal; from the GridBagLayout documentation:
In addition to its constraints object, the GridBagLayout also considers each component's minimum and preferred sizes in order to determine a component's size.
However, a search returned several Stack Overflow discussions where using the minimum size is advised against:
Upvotes: 1