按钮占据了我的整个屏幕

时间:2014-09-05 01:38:59

标签: java eclipse swing layout-manager

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;

public class box{
    public static String b1state = "A";
    public static String b2state = "S";
    public static String b3state = "D";
    public static String b4state = "F";
    public static String b5state = "G";
    public static String b6state = "H";
    public static String b7state = "J";
    public static String b8state = "K";
    public static String b9state = "Q";

    public static void main(String[] args){
        JFrame frame = new JFrame("Tic Tac Toe");
        frame.setSize(500,500);
        frame.setResizable(false);
        frame.setVisible(true);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        JPanel p = new JPanel();

        JButton b1 = new JButton(b1state);
        b1.setLocation(100,  50);
        JButton b2 = new JButton(b2state);
        b2.setLocation(150, 50);
        JButton b3 = new JButton(b3state);
        b3.setLocation(200, 50);
        JButton b4 = new JButton(b4state);
        b4.setLocation(100, 100);
        JButton b5 = new JButton(b5state);
        b5.setLocation(150, 100);
        JButton b6 = new JButton(b6state);
        b6.setLocation(200, 100);
        JButton b7 = new JButton(b7state);
        b7.setLocation(100, 150);
        JButton b8 = new JButton(b8state);
        b8.setLocation(150, 150);
        JButton b9 = new JButton(b9state);
        b9.setLocation(200, 150);

        b1.setSize(50, 50);
        b2.setSize(50, 50);
        b3.setSize(50, 50);
        b4.setSize(50, 50);
        b5.setSize(50, 50);
        b6.setSize(50, 50);
        b7.setSize(50, 50);
        b8.setSize(50, 50);
        b9.setSize(50, 50);

        p.add(b1);
        p.add(b2);
        p.add(b3);
        p.add(b4);
        p.add(b5);
        p.add(b6);
        p.add(b7);
        p.add(b8);
        p.add(b9);

        frame.add(p);
        frame.add(b1);
        frame.add(b2);
        frame.add(b3);
        frame.add(b4);
        frame.add(b5);
        frame.add(b6);
        frame.add(b7);
        frame.add(b8);
        frame.add(b9);
    }

当我运行它时,它有时会起作用,但有时它只是打开窗口,整个按钮占用屏幕!请帮助!

3 个答案:

答案 0 :(得分:3)

  1. 组件可能只驻留在单个父级中,将组件添加到另一个容器会将其从第一个容器中删除,所以基本上,任何内容都不再存在于JPanel中......
  2. 默认情况下,
  3. JFrame使用BorderLayout,这意味着只有一个组件可以占用它提供的五个可用空间中的任何一个。您的代码发生了什么b9,是添加到框架中的最后一个组件,是唯一显示的组件。 BorderLayoutb9提供了完整的可用空间。
  4. 首先看一下Laying Out Components Within a Container。您可能需要使用多个布局才能达到您想要的效果,但这样您就可以分离责任并隔离接口每个部分的各个要求

答案 1 :(得分:1)

替换它:

    JPanel p = new JPanel();

使用:

    JPanel p = new JPanel(new GridLayout(3, 3));

而且:

    frame.add(p);
    frame.add(b1);
    frame.add(b2);
    frame.add(b3);
    frame.add(b4);
    frame.add(b5);
    frame.add(b6);
    frame.add(b7);
    frame.add(b8);
    frame.add(b9);

用这个:

    frame.setContentPane(p);

这将使您的代码"工作"。 但你必须更多地使用容器来学习如何布局"组件。我建议你这样: http://docs.oracle.com/javase/tutorial/uiswing/layout/visual.html

答案 2 :(得分:0)

我有一个问题,如果它是填满你的窗口的最后一个按钮你可以"作弊"通过在代码末尾添加另一个按钮,然后禁用他并让他像这样隐身。 cheater.setVisible(false); cheater.setEnabled(false);

相关问题