Java 中的简单下拉菜单
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/22506331/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Simple Dropdown menu in Java
提问by NoobNe0
I am working on a very simple GUI in Java.
我正在用 Java 开发一个非常简单的 GUI。
In this GUI I want to display:
在这个 GUI 中,我想显示:
- A label with some text on the top of the page
- A JComboBox under the mentioned label
- A JButton under the mentioned JComboBox
- 在页面顶部带有一些文本的标签
- 提到的标签下的 JComboBox
- 提到的 JComboBox 下的 JButton
Here's my code:
这是我的代码:
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
public class Prova {
public static void main(String[] args) {
JFrame frame = new JFrame("A Simple GUI");
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(500, 500);
frame.setLocation(430, 100);
JPanel panel = new JPanel();
frame.add(panel);
JLabel lbl = new JLabel("Select one of the possible choices and click OK");
lbl.setVisible(true);
panel.add(lbl);
String[] choices = { "CHOICE 1","CHOICE 2", "CHOICE 3","CHOICE 4","CHOICE 5","CHOICE 6"};
final JComboBox<String> cb = new JComboBox<String>(choices);
cb.setVisible(true);
panel.add(cb);
JButton btn = new JButton("OK");
panel.add(btn);
}
}
Unfortunately, the result I get is
不幸的是,我得到的结果是
As you can see in the image, the label, the JComboBox and the JButton are on the same line!
正如您在图像中看到的,标签、JComboBox 和 JButton 位于同一行!
Instead, I want them "stacked" as described above:
相反,我希望它们如上所述“堆叠”:
JLabel
标签
JComboBox
组合框
JButton
按钮
I tried using the setLocation(int x, int y) method, but they always show in the same position.
我尝试使用 setLocation(int x, int y) 方法,但它们总是显示在相同的位置。
Many thanks!
非常感谢!
回答by Taerus
Study some tutorials on using layout managers, that's where you'll find the solution. They are pretty tough though.
学习一些关于使用布局管理器的教程,你会在那里找到解决方案。虽然他们很艰难。
回答by Atul O Holic
It is due to the Layout which is used to align the children. By default its FlowLayout which lays all the child components in a flow starting from left to right and hence you getting the above display.
这是由于用于对齐子项的布局。默认情况下,它的 FlowLayout 将所有子组件放置在从左到右的流中,因此您将获得上述显示。
You can use is a GridLayout with 3 rows and 1 column as per your requirement.
您可以根据您的要求使用 3 行 1 列的 GridLayout。
回答by Nerakai
use frame.setLayout(null);
this will allow you to place the Label, Button etc. where you like
使用frame.setLayout(null);
这将允许您将标签、按钮等放置在您喜欢的位置
回答by Ahmed Salem
You should use one of Java standard Layout (GridLayout, LinearLayout, BoxLayout)
您应该使用 Java 标准布局之一(GridLayout、LinearLayout、BoxLayout)
I recommend to use a grid layout with 1 column and 3 rows
我建议使用 1 列 3 行的网格布局
like below
像下面
setLayout(new GridLayout(1,3));
add(new Button("1"));
add(new Button("2"));
add(new Button("3"));
回答by bballdave025
If I've understood your question, the following code accomplishes what you are trying to do without being overly complicated:
如果我理解你的问题,下面的代码就可以完成你想要做的事情而不会过于复杂:
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.BoxLayout; // added code
import java.awt.Component; // added code
public class Prova {
public static void main(String[] args) {
JFrame frame = new JFrame("A Simple GUI");
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(500, 500);
frame.setLocation(430, 100);
JPanel panel = new JPanel();
panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS)); // added code
frame.add(panel);
JLabel lbl = new JLabel("Select one of the possible choices and click OK");
lbl.setAlignmentX(Component.CENTER_ALIGNMENT);
//lbl.setVisible(true); // Not needed
panel.add(lbl);
String[] choices = { "CHOICE 1", "CHOICE 2", "CHOICE 3", "CHOICE 4",
"CHOICE 5", "CHOICE 6" };
final JComboBox<String> cb = new JComboBox<String>(choices);
cb.setMaximumSize(cb.getPreferredSize()); // added code
cb.setAlignmentX(Component.CENTER_ALIGNMENT);// added code
//cb.setVisible(true); // Not needed
panel.add(cb);
JButton btn = new JButton("OK");
btn.setAlignmentX(Component.CENTER_ALIGNMENT); // added code
panel.add(btn);
frame.setVisible(true); // added code
}
}
The setLocation
method is often overly complicated unless you have very specific (artistic?) goals for the layout. For this problem, an easier solution is to use a BoxLayout
and specify that you want things added in the y-direction (vertically downwards.) Note that you will have to specify the dimensions of the JComboBox
(and some other GUI elements that you might want to add later) to avoid a giant drop-down menu. cf. another stackoverflow post, JComboBox width
setLocation
除非您对布局有非常具体(艺术?)的目标,否则该方法通常过于复杂。对于这个问题,一个更简单的解决方案是使用 aBoxLayout
并指定您希望在 y 方向(垂直向下)添加内容。请注意,您必须指定JComboBox
(以及您可能想要的其他一些 GUI 元素的尺寸)稍后添加)以避免出现巨大的下拉菜单。参见 另一个 stackoverflow 帖子,JComboBox 宽度