简体   繁体   中英

My JButtons are bugged

I am having problems with drawing a 2D JButton array to a JFrame . All the JButtons draw properly except for the last one, the last JButton to be rendered is fitted to the JFrame . I have set the width and height of all the JButtons to 100x100 , but the last JButton to render does not have a width and height of 100x100 . I printed the properties of the console, and it said the button height and width was 494X496 .

Main class that runs everything:

package gameData;

import javax.swing.*;

public class GameRun {

    JFrame frame;
    ActionHandle AH = new ActionHandle();
    Screen screen = new Screen();

    public GameRun() {
        beginSession();
        screen.renderScreen(frame, AH);
    }

    public void beginSession() {
        JFrame frame = new JFrame();
        frame.setSize(500, 525);;
        frame.setResizable(false);
        frame.setVisible(true);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setTitle("Game");
        this.frame = frame;
    }

    public static void main(String[] args) {
        new GameRun();
    }

}

how the JButtons are drawn:

package gameData;

import java.awt.event.*;
import java.awt.*;
import javax.swing.*;

public class Screen {
    public void renderScreen(JFrame frame, ActionListener AL){
        JButton[][] button = new JButton[5][5];
        for(int i =0; i<button.length;i++){
            for(int j = 0; j<button[i].length; j++){
                button[i][j] = new JButton(i+" "+j);
                button[i][j].setBounds(i*100, j*100, 100, 100);
                button[i][j].addActionListener(AL);
                frame.add(button[i][j]);
            }
        }
    }
}
  1. JFrame uses a BorderLayout by default, so you are adding all the buttons to the same location on the frame, possibly overlapping them. Instead, you should consider using a GridBagLayout instead. See How to Use GridBagLayout for more details
  2. You should override the buttons getPreferredSize method if you want to affect they way in which the buttons are laid out.
  3. You should really call setResizable before you set the size of the frame
  4. You should use pack instead of setSize , frames have window decorations which get fitted inside the window, not added to it, this will affect the amount of viewable space you have for the content, pack will do all these calculations for you
  5. You should be creating/modifying your UI from within the context of the Event Dispatching Thread, see Initial Threads for more details

For example...

纽扣

import java.awt.EventQueue;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

public class Test {

    public static void main(String[] args) {
        new Test();
    }

    public Test() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                    ex.printStackTrace();
                }

                JFrame frame = new JFrame("Testing");
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.add(new TestPane());
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }

    public class TestPane extends JPanel {

        public TestPane() {
            setLayout(new GridBagLayout());
            GridBagConstraints gbc = new GridBagConstraints();
            gbc.gridx = 0;
            gbc.gridy = 0;
            JButton[][] button = new JButton[5][5];
            for (int i = 0; i < button.length; i++) {
                gbc.gridx = 0;
                for (int j = 0; j < button[i].length; j++) {
                    button[i][j] = new JButton(i + " " + j) {
                        @Override
                        public Dimension getPreferredSize() {
                            return new Dimension(100, 100);
                        }
                    };
                    add(button[i][j], gbc);
                    gbc.gridx++;
                }
                gbc.gridy++;
            }
        }

    }

}

Beware when playing with getPreferredSize , fonts are generally not rendered the same on all platforms and this will affect what you program looks look on different platforms. In you case, chaning the margins of the button might be safer

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM