如何将框架分为两部分 [英] How to divide a frame into two parts
问题描述
这是给俄罗斯方块的.左边的玻璃(蓝色),右边的控件(红色面板).换句话说,现在我只想将框架分为两部分:左(较宽)部分是蓝色,右部分是红色.而已.但是我似乎没有做到这一点.
This is for Tetris. The glass (blue) is left, and the controls (red panel) are situated in the right. In other words, now I would like just to have a frame divided into two parts: left (wider) part is blue, right part is red. Nothing more. But I seem to fail to do this.
所以,我的逻辑是:让框架具有FlowLayout.然后,我添加了两个面板,这意味着它们应该连续放置.
So, my logic is: let the frame have FlowLayout. Then I add two panels which means that they are expected to be put in a row.
我准备了这个:
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);
}
}
但是在屏幕上仅可见灰色框.你能帮我理解为什么吗?
But only a gray frame is visible on the screen. Could you help me understand why?
推荐答案
正如Amir所说,您想为此使用JSplitPane.我已经在您的代码中添加了此代码.看看这个.
As Amir said you want to use a JSplitPane for this. I have added this in your code. Have a look at this.
/**
* @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);
}
}
这篇关于如何将框架分为两部分的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!