Java 在 GUI 上定位列表
我正在尝试将 JList 添加到 GUI,但想知道如何定位它?我希望它出现在 TextArea 的右侧,以显示将发送到 GUI 进行选择的数据。
谁能建议如何做到这一点?这是代码(注意:对于 Java 和 GUI 来说非常陌生)
protected static void createAndShowGUI() {
GUI predict = new GUI();
JFrame frame = new JFrame("Phone V1.0");
frame.setContentPane(predict.createContentPane());
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.pack();
frame.setMinimumSize(new Dimension(300, 400));
frame.setVisible(true); // Otherwise invisible window
}
private JPanel createContentPane() {
JPanel pane = new JPanel();
TextArea = new JTextArea(5, 10);
TextArea.setEditable(false);
TextArea.setLineWrap(true);
TextArea.setWrapStyleWord(true);
TextArea.setWrapStyleWord(true);
pane.setLayout(new BoxLayout(pane, BoxLayout.Y_AXIS));
//Adds the buttons from Top to Bottom
String[] items = {"dsfsdfd"};
list = new JList(items);
JScrollPane scrollingList = new JScrollPane(list);
int orient = list.getLayoutOrientation();
JPanel window = new JPanel();
pane.add(window);
JPanel buttonPanel = new JPanel();
buttonPanel.setLayout(new GridLayout(5, 3));
JButton[] buttons = new JButton[] {
new JButton("Yes"),
new JButton(""),
new JButton("Clr"),
new JButton("1"),
new JButton("2abc"),
new JButton("3def"),
new JButton("4ghi"),
new JButton("5jkl"),
new JButton("6mno"),
new JButton("7pqrs"),
new JButton("8tuv"),
new JButton("9wxyz"),
new JButton("*+"),
new JButton("0_"),
new JButton("^#")
}; // Array Initialiser
for (int i = 0; i < buttons.length; i++) {
buttonPanel.add(buttons[i]);
buttons[i].addActionListener(this);
}
pane.add(TextArea);
pane.add(list);
pane.add(buttonPanel);
return pane;
}
I'm trying to add a JList to a GUI, but am wondering how to position it? I want it to appear on the right hand side of the TextArea for data that will be sent to the GUI for selection.
Can anyone suggest how to do this? Here is the code (note: very new to Java and GUI's)
protected static void createAndShowGUI() {
GUI predict = new GUI();
JFrame frame = new JFrame("Phone V1.0");
frame.setContentPane(predict.createContentPane());
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.pack();
frame.setMinimumSize(new Dimension(300, 400));
frame.setVisible(true); // Otherwise invisible window
}
private JPanel createContentPane() {
JPanel pane = new JPanel();
TextArea = new JTextArea(5, 10);
TextArea.setEditable(false);
TextArea.setLineWrap(true);
TextArea.setWrapStyleWord(true);
TextArea.setWrapStyleWord(true);
pane.setLayout(new BoxLayout(pane, BoxLayout.Y_AXIS));
//Adds the buttons from Top to Bottom
String[] items = {"dsfsdfd"};
list = new JList(items);
JScrollPane scrollingList = new JScrollPane(list);
int orient = list.getLayoutOrientation();
JPanel window = new JPanel();
pane.add(window);
JPanel buttonPanel = new JPanel();
buttonPanel.setLayout(new GridLayout(5, 3));
JButton[] buttons = new JButton[] {
new JButton("Yes"),
new JButton(""),
new JButton("Clr"),
new JButton("1"),
new JButton("2abc"),
new JButton("3def"),
new JButton("4ghi"),
new JButton("5jkl"),
new JButton("6mno"),
new JButton("7pqrs"),
new JButton("8tuv"),
new JButton("9wxyz"),
new JButton("*+"),
new JButton("0_"),
new JButton("^#")
}; // Array Initialiser
for (int i = 0; i < buttons.length; i++) {
buttonPanel.add(buttons[i]);
buttons[i].addActionListener(this);
}
pane.add(TextArea);
pane.add(list);
pane.add(buttonPanel);
return pane;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
阅读 Swing 教程中关于使用布局管理器的部分。不需要只使用单个布局管理器。您可以嵌套布局管理器以获得所需的效果。
Read the section from the Swing tutorial on Using Layout Mananger. There is no need to only use a single layout manager. You can nest layout managers to get the desired effect.
使用 BorderLayout 管理器。基本上,BorderLayout 管理器允许您使用北、南、东、西和中心坐标来排列组件。中心的组件占用所有可用空间,因为父容器有更多可用空间。
最酷的事情是您可以将面板嵌套在其他面板中,为它们添加不同的布局管理器以获得您想要的布局。
在不相关的注释中,请尝试遵循 Java 命名约定。使用
JTextArea textArea
代替JTextArea TextArea
。它使您和阅读您的代码的人更容易理解它。Wrap your
TextArea
andlist
in a new panel with a BorderLayout manager. Basically the BorderLayout manager lets you arrange components using north, south, east, west and center coordinates. The components at the center takes all available space as the parent container has more space available to it.The cool thing is that you can nest panels inside other panels, adding them different layout managers to get your desired layout.
On an unrelated note, try to follow Java naming conventions. Instead of
JTextArea TextArea
useJTextArea textArea
. It makes it easier for you and people reading your code to understand it.您可以使用诸如 Mig Layout 之类的布局管理器来进行此类定位。
(来源:miglayout.com)
You could use a layout manager like Mig Layout for that kind of positionning.
(source: miglayout.com)
我可以推荐您
FormLayout
。在找到这个布局之前,我对 GridBagLayout 感到非常痛苦。FormLayout
功能更强大,学习和使用更方便,而且免费。给它一个机会。I could recommend you
FormLayout
. Before I found this layout I had a real pain withGridBagLayout
.FormLayout
is more powerful and much more convenient to learn and use and it is free. Give it a chance.正如其他人所建议的,熟悉布局管理器的概念。有几个附带标准 Swing API 和几个优秀的第 3 方 API。
此外,您还需要将
JList
添加到滚动窗格(JScrollPane
)。您可能需要考虑将其添加到拆分窗格 (JSplitPane
)。 考虑我的意思并不是“因为网上有人这么说而这样做”,我的意思是“如果这对您的最终用户有意义,就这样做”。As others suggested, familiarize yourself with the concept of layout managers. There are several that come with the standard Swing API and several good 3rd party ones out there.
In addition, you will want to add the
JList
to a scroll pane (JScrollPane
). You may want to consider adding it to a split pane (JSplitPane
). And by consider I don't mean "do it because some guy on the net said so" I mean "do it if it makes sense for your end users".