Java Basic GUI(布局错误)

时间:2015-04-03 07:43:07

标签: java swing layout-manager grid-layout

所以我正在创建一个包含5x5矩阵按钮的游戏,其中一个是隐藏的奖品。我已完成游戏,但我遇到布局问题。

我添加了图片:  http://i.imgur.com/9zZ8jiV.png

当我运行程序时,左侧的那个出现,当我点击按钮时,右侧的图片出现。

第一个JFrame看起来非常奇怪我的意思是它占据了整个屏幕的长度,因此我需要打包它才能以合适的尺寸显示按钮。

第二个,我需要第一行才能显示标签, 然后每行开始包含5个按钮。

我很确定我使用了错误的布局,但我无法弄清楚如何修复它。 以下是代码:

MyJFrame班级代码:

class MyJFrame extends JFrame {
JPanel panel2;

public MyJFrame() {
setLayout(new GridLayout(7, 7));

panel2 = new PanelJ2();

add(panel2);
JButton b = new JButton("Start the freaking game");
b.addActionListener(new ActionListener(){
  public void actionPerformed(ActionEvent e){
    panel2.setVisible(true);
    b.setVisible(false);
    PotLuck.random = (int)(Math.random() * 25 + 1);
  }
});
add(b);
setTitle( "This is a freaking game");
pack();
setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE);
setVisible(true);
}
}

PanelJ2代码:

class PanelJ2 extends JPanel {
JButton[] buttons;
JLabel label, l;
public static int count;

public PanelJ2() {
setLayout( new GridLayout(5,5) );
setPreferredSize( new Dimension( 300, 200)); //I just wrote this line, I am not sure of the purpose quite really.
count = 0;
label = new JLabel("Number of guesses so far: " + count);
add(label);
l = new JLabel("");

buttons = new JButton[25];
for( int i = 0; i <= 24; i ++) {

  buttons[i] = new JButton(String.valueOf(i));
  add(buttons[i]);
  buttons[i].addActionListener( new ExampleActionListener1());
}
setBackground( Color.green);
setVisible(false); //this is set true when the button "Start the freaking game is pressed"
}
}

1 个答案:

答案 0 :(得分:1)

setLayout(new GridLayout(7, 7));将导致主要问题。一个更简单的问题是使用CardLayout

有关详细信息,请参阅How to Use CardLayout

import java.awt.CardLayout;
import java.awt.Color;
import java.awt.EventQueue;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

class MyJFrame extends JFrame {

    JPanel panel2;

    private CardLayout cl;

    public MyJFrame() {
        cl = new CardLayout();
        setLayout(cl);
        panel2 = new PanelJ2();

        add(panel2, "LotsOfButtons");
        JButton b = new JButton("Start the freaking game");
        b.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                cl.show(getContentPane(), "LotsOfButtons");
            }
        });
        add(b, "start");
        cl.show(getContentPane(), "start");
        setTitle("This is a freaking game");
        pack();
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setVisible(true);
    }

    static class PanelJ2 extends JPanel {

        JButton[] buttons;
        JLabel label, l;
        public static int count;

        public PanelJ2() {
            setLayout(new GridLayout(5, 5));
//          setPreferredSize(new Dimension(300, 200)); //I just wrote this line, I am not sure of the purpose quite really.
            count = 0;
            label = new JLabel("Number of guesses so far: " + count);
            add(label);
            l = new JLabel("");

            buttons = new JButton[25];
            for (int i = 0; i <= 24; i++) {

                buttons[i] = new JButton(String.valueOf(i));
                add(buttons[i]);
//              buttons[i].addActionListener(new ExampleActionListener1());
            }
            setBackground(Color.green);
            setVisible(false); //this is set true when the button "Start the freaking game is pressed"
        }

    }

    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                    ex.printStackTrace();
                }

                MyJFrame frame = new MyJFrame();
            }
        });
    }
}

您可能希望将按钮放入其他容器中,可能使用GridBagLayout