如何将框架分为两部分

这是为俄罗斯方块。 留下玻璃(蓝色),控件(红色面板)位于右侧。 换句话说,现在我想将一个框架分成两部分:左(更宽)部分是蓝色,右边部分是红色。 而已。 但我似乎没有这样做。

所以,我的逻辑是:让框架具有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); } } 

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

你想使用的是SplitPane 。