Java 如何将框架分成两部分

Java 如何将框架分成两部分,java,swing,layout,Java,Swing,Layout,这是俄罗斯方块。玻璃(蓝色)位于左侧,控制装置(红色面板)位于右侧。换句话说,现在我想把一个框架分成两部分:左(宽)部分是蓝色,右部分是红色。没别的了。但我似乎没有做到这一点 所以,我的逻辑是:让框架具有FlowLayout。然后我添加了两个面板,这意味着它们应该排成一行 我准备了这个: public class GlassView extends JFrame{ public GlassView(){ this.setSize(600, 750); th

这是俄罗斯方块。玻璃(蓝色)位于左侧,控制装置(红色面板)位于右侧。换句话说,现在我想把一个框架分成两部分:左(宽)部分是蓝色,右部分是红色。没别的了。但我似乎没有做到这一点

所以,我的逻辑是:让框架具有FlowLayout。然后我添加了两个面板,这意味着它们应该排成一行

我准备了这个:

public class GlassView extends JFrame{
    public GlassView(){
        this.setSize(600, 750);
        this.setVisible(true);
        this.setLayout(new FlowLayout());
        this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);


        JPanel glass = new JPanel();
        glass.setLayout(new BoxLayout(glass, BoxLayout.Y_AXIS));
        glass.setSize(450, 750);
        glass.setBackground(Color.BLUE);
        glass.setVisible(true);
        this.add(glass);

        JPanel controls = new JPanel();
        controls.setLayout(new BoxLayout(controls, BoxLayout.Y_AXIS));
        controls.setSize(150, 750);
        controls.setBackground(Color.RED);
        controls.setVisible(true);
        this.add(controls);
    }
}
但屏幕上只能看到一个灰色框架。你能帮我理解为什么吗

如何将框架分成两部分 ... 我想把一个框架分成两部分:左(宽)部分是蓝色,右部分是红色

您要使用的是一个

如何将框架分成两部分 ... 我想把一个框架分成两部分:左(宽)部分是蓝色,右部分是红色


正如Amir所说,您希望使用JSplitPane来实现此目的,您希望使用的是。

。我已经在你的代码中添加了这个。看看这个

/**
 * @param args the command line arguments
 */
public static void main(String[] args) {
    GlassView view = new GlassView();
}

private static class GlassView extends JFrame {

    private int width = 600;
    private int height = 750;

    public GlassView() {
        this.setSize(width, height);
        this.setVisible(true);
        this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);

        JPanel glass = new JPanel();
        glass.setSize(450, 750);
        glass.setBackground(Color.BLUE);
        glass.setVisible(true);

        JPanel controls = new JPanel();
        controls.setSize(150, 750);
        controls.setBackground(Color.RED);
        controls.setVisible(true);

        JSplitPane splitPane = new JSplitPane();
        splitPane.setSize(width, height);
        splitPane.setDividerSize(0);
        splitPane.setDividerLocation(150);
        splitPane.setOrientation(JSplitPane.HORIZONTAL_SPLIT);
        splitPane.setLeftComponent(controls);
        splitPane.setRightComponent(glass);

        this.add(splitPane);
    }
}

正如Amir所说,您希望为此使用JSplitPane。我已经在你的代码中添加了这个。看看这个

/**
 * @param args the command line arguments
 */
public static void main(String[] args) {
    GlassView view = new GlassView();
}

private static class GlassView extends JFrame {

    private int width = 600;
    private int height = 750;

    public GlassView() {
        this.setSize(width, height);
        this.setVisible(true);
        this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);

        JPanel glass = new JPanel();
        glass.setSize(450, 750);
        glass.setBackground(Color.BLUE);
        glass.setVisible(true);

        JPanel controls = new JPanel();
        controls.setSize(150, 750);
        controls.setBackground(Color.RED);
        controls.setVisible(true);

        JSplitPane splitPane = new JSplitPane();
        splitPane.setSize(width, height);
        splitPane.setDividerSize(0);
        splitPane.setDividerLocation(150);
        splitPane.setOrientation(JSplitPane.HORIZONTAL_SPLIT);
        splitPane.setLeftComponent(controls);
        splitPane.setRightComponent(glass);

        this.add(splitPane);
    }
}

关于您的种类信息,请始终仅在将所有组件添加到容器中之后,而不是在此之前,调用
frame.pack()
frame.setVisible()
。否则,在很多情况下,您可能无法在屏幕上看到内容。如果<代码> JSPLITANE 不是您所要查找的,那么请考虑一下,这样可以轻松地完成这项任务。为了更清楚,请看一下这一点:-)您正在使用的
setSize(…)
和许多布局管理器(大多数?)不尊重size属性,而通常是首选的size。有关您的信息,请始终调用
frame.pack()
frame.setVisible()
仅在将所有组件添加到容器之后,而不是之前。否则,在很多情况下,您可能无法在屏幕上看到内容。如果<代码> JSPLITANE 不是您所要查找的,那么请考虑一下,这样可以轻松地完成这项任务。为了更清楚,请查看以下内容:-)您正在使用的
setSize(…)
和许多布局管理器(大多数?)不尊重size属性,而通常是首选size。谢谢您的回答。但这似乎不适合我。此拆分窗格用于交互式调整大小,我的情况并非如此。第二,我想了解我对这些小组的错误做法。谢谢你的回答。但这似乎不适合我。此拆分窗格用于交互式调整大小,我的情况并非如此。第二,我想了解我对这些小组的错误做法。