如何在 Swing 中使用 GridLayout?
How do I use GridLayout in Swing?
我尝试用 Swing 制作 GUI。但是当我尝试使用 GridLayout 时,每个元素都会突然消失,我不知道为什么。谁能解释一下 and/or 提供解决方案?
我已经尝试查看它的教程,但没有太大帮助。我做了一些实验,但仍然没有用。 :(
JPanel pan1 = new JPanel();
JPanel pan2 = new JPanel();
JPanel pan3 = new JPanel();
JPanel pan4 = new JPanel();
JPanel pan5 = new JPanel();
add(pan1);
add(pan2);
add(pan3);
add(pan4);
add(pan5);
pan1.setLayout(new GridLayout(0, 1));
pan2.setLayout(new GridLayout(1, 1));
pan3.setLayout(new GridLayout(2, 1));
pan4.setLayout(new GridLayout(3, 1));
pan5.setLayout(new GridLayout(4, 1));
我希望每 0-4 列有 1 个元素,但它显示一个空白屏幕。
I expected having 1 Element in every 0-4 columns but it shows me a blank screen.
没有。您有 5 个面板,每个面板都有一个网格布局。
pan1.setLayout(new GridLayout(0, 1)); //pan1 gets a new GridLayout
pan2.setLayout(new GridLayout(1, 1)); //pan2 gets a new GridLayout
pan3.setLayout(new GridLayout(2, 1)); //...
pan4.setLayout(new GridLayout(3, 1));
pan5.setLayout(new GridLayout(4, 1));
你需要:
setLayout(new GirdLayout(1,5); //1 Row 5 Columns
add(pan1);
add(pan2);
add(pan3);
add(pan4);
add(pan5);
我尝试用 Swing 制作 GUI。但是当我尝试使用 GridLayout 时,每个元素都会突然消失,我不知道为什么。谁能解释一下 and/or 提供解决方案?
我已经尝试查看它的教程,但没有太大帮助。我做了一些实验,但仍然没有用。 :(
JPanel pan1 = new JPanel();
JPanel pan2 = new JPanel();
JPanel pan3 = new JPanel();
JPanel pan4 = new JPanel();
JPanel pan5 = new JPanel();
add(pan1);
add(pan2);
add(pan3);
add(pan4);
add(pan5);
pan1.setLayout(new GridLayout(0, 1));
pan2.setLayout(new GridLayout(1, 1));
pan3.setLayout(new GridLayout(2, 1));
pan4.setLayout(new GridLayout(3, 1));
pan5.setLayout(new GridLayout(4, 1));
我希望每 0-4 列有 1 个元素,但它显示一个空白屏幕。
I expected having 1 Element in every 0-4 columns but it shows me a blank screen.
没有。您有 5 个面板,每个面板都有一个网格布局。
pan1.setLayout(new GridLayout(0, 1)); //pan1 gets a new GridLayout
pan2.setLayout(new GridLayout(1, 1)); //pan2 gets a new GridLayout
pan3.setLayout(new GridLayout(2, 1)); //...
pan4.setLayout(new GridLayout(3, 1));
pan5.setLayout(new GridLayout(4, 1));
你需要:
setLayout(new GirdLayout(1,5); //1 Row 5 Columns
add(pan1);
add(pan2);
add(pan3);
add(pan4);
add(pan5);