Java 如何制作可滚动的 JList 以添加从 JOptionPane 获得的详细信息
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3200846/
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
How to make a scrollable JList to add details got from a JOptionPane
提问by Haxed
I want to repeatedly take input from the user(probably using a button) via a JOptionPane(already done) and store the details in something(how about a dynamic object array) and display this information as a list in a scrollable JList.
我想通过 JOptionPane(已经完成)重复从用户(可能使用按钮)获取输入并将详细信息存储在某物(动态对象数组怎么样)中,并将此信息显示为可滚动 JList 中的列表。
MY CODE
我的代码
import java.awt.GridLayout;
import javax.swing.*;
class Flight {
public static void main(String[] args) {
//Panel
JPanel panel = new JPanel(new GridLayout(7, 2,20, 20));
//Add textfields here
JTextField txtflightno = new JTextField(8);
JTextField txtmechanicalstatus = new JTextField(8);
JTextField txtmedicalstatus = new JTextField(8);
JTextField txtfuellevel = new JTextField(8);
JTextField txtweathercondition = new JTextField(8);
JTextField txtfrequency = new JTextField(8);
JTextField txtflightpath = new JTextField(8);
//Add labels here
JLabel lblflightno = new JLabel("Flight No : ");
JLabel lblmechanicalstatus = new JLabel("Mechanical Status:");
JLabel lblmedicalstatus = new JLabel("Medical Status:");
JLabel lblfuellevel = new JLabel("Fuel Level:");
JLabel lblweathercondition = new JLabel("Weather Condition:");
JLabel lblfrequency = new JLabel("Frequency:");
JLabel lblflightpath = new JLabel("Flight Path:");
//Adding flightno to panel
panel.add(lblflightno);
panel.add(txtflightno);
//Adding mechanicalstatus to the panel
panel.add(lblmechanicalstatus);
panel.add(txtmechanicalstatus);
//Adding medicalstatus to the panel
panel.add(lblmedicalstatus);
panel.add(txtmedicalstatus);
//Adding fuellevel to the panel
panel.add(lblfuellevel);
panel.add(txtfuellevel);
//Adding weathercondition to the panel
panel.add(lblweathercondition);
panel.add(txtweathercondition);
//Adding frequency to the panel
panel.add(lblfrequency);
panel.add(txtfrequency);
//Adding flightpath to the panel
panel.add(lblflightpath);
panel.add(txtflightpath);
panel.setBounds(0, 0, 800, 600);
int result = JOptionPane.showConfirmDialog(null, panel, "Flight Details",
JOptionPane.OK_CANCEL_OPTION, JOptionPane.PLAIN_MESSAGE);
if (result == JOptionPane.OK_OPTION) {
}
}
}
How must I do the storing of the plane details ? How must I implement a scrollable JList ? Any suggestions.
我必须如何存储飞机详细信息?我必须如何实现可滚动的 JList ?有什么建议。
Many Thanks
非常感谢
回答by trashgod
As discussed in How to Use Lists, JList
uses a list model as the source of data it displays. Just add your data to a DefaultListModel
, and use it to construct your list.
如如何使用列表中所述,JList
使用列表模型作为它显示的数据源。只需将您的数据添加到 a DefaultListModel
,并使用它来构建您的列表。
DefaultListModel dlm = new DefaultListModel();
// add data
JList list = new JList(dlm);
panel.add(new JScrollPane(list));
回答by Carl Smotricz
To make the JList scrollable, simply embed it in a JScrollPane. Instead of
要使 JList 可滚动,只需将其嵌入JScrollPane 即可。代替
add(myList, constraints);
do
做
add(new JScrollPane(myList), constraints);
To extend the list, just get the JList's ListModel (using getListModel
) and use add
to add objects.
要扩展列表,只需获取 JList 的 ListModel(使用getListModel
)并用于add
添加对象。
More on using ListModels in Sun's tutorial.
更多关于在 Sun 的教程中使用 ListModels的信息。
More on ScrollPane in the Tutorial, too.
回答by Venkat
You've to use the ActionListener
of Button, that you've missed in the code snippet.
您必须使用ActionListener
代码片段中遗漏的 Button。
In the OK Option :
在确定选项中:
JList jlist = ...;
jlist.add(txtflightno.getText());
jlist.add(txtmechanicalstatus.getText());
jlist.add(txtmedicalstatus.getText());
....
....
&
add(new JScrollPanel(myList), constraints);
After this use validate method of Component to update the list with this new item. But one thing you should remember is that list displays each item row-wise. I suggest you to use JTablewith which you can display your items in a meaningful way...
在此之后,使用 Component 的 validate 方法用这个新项目更新列表。但是您应该记住的一件事是列表按行显示每个项目。我建议您使用JTable以有意义的方式显示您的项目......