标记按钮并维护尺寸

时间:2016-01-16 15:36:52

标签: swing jbutton gridbaglayout

我正在努力使用JButton s,特别是保持它们的大小相同,因为似乎向按钮添加任何内容会导致它伸展以适应所述添加。

因此,标有“是”的按钮与标记为“否”的按钮的大小不同,即使空白时两个按钮都应该足够大,以便能够轻松地包含任何一个字符串。

基本上,我想知道是否有办法在不改变所述按钮尺寸的情况下标记按钮。

我正在使用GridBagLayout,这是我的代码的相关示例供参考:

修改

我设法通过使用图标来获得我正在寻找的效果......你可能无法完全看到它,因为我直接从我的硬盘驱动器中调用了图标......无论如何,我留下完整的代码供参考......

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

            }

}

1 个答案:

答案 0 :(得分:1)

您是否可以添加更多代码以及屏幕截图来显示您的应用程序当前的样子(有关Stack Overflow上理想代码示例的详细信息,请参阅stackoverflow.com/help/mcve)?

我经常更喜欢使用具有简单布局管理器的子面板而不是使用具有大量组件和复杂布局管理器的单个面板。您可以考虑使用多个面板和/或不同的布局管理器(这是对默认情况下可用的一些布局管理器的一个很好的概述:A Visual Guide to Layout Managers。)

作为一个简短示例,这段代码使用GridLayout创建一个带有两个按钮的面板(带有间隙和边框以添加一些间距):

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

设置最小尺寸?

GridBagLayout应使用按钮的最小(和/或首选)尺寸,这样可以使按钮的宽度相等;来自GridBagLayout documentation

  

除了约束对象之外,GridBagLayout还会考虑每个组件的最小和首选大小,以确定组件的大小。

但是,搜索返回了几个Stack Overflow讨论,建议使用最小大小: