如何使用函数更新 JList 并存储到数组中?
How do I update JList and store into an array using a function?
我的印象是我的大部分代码都可以工作,但是,我相信我在函数 updatedJList()
下只遗漏了一行 updatedJList()
请帮忙,谢谢。
import javax.swing.DefaultListModel;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JScrollPane;
import javax.swing.JTextField;
import javax.swing.ListSelectionModel;
import javax.swing.SwingConstants;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import javax.swing.JList;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class Ex1 extends JFrame {
private JTextField txtName;
private JList nameList;
private String[] nameArr;
private int arrCounter = 0;
private JLabel lblDisplayName;
public Ex1(){
this.setTitle("Exercise01");
this.setSize(300, 266);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
getContentPane().setLayout(null);
this.nameArr = new String[10];
JLabel lblName = new JLabel("Name:");
lblName.setBounds(10, 11, 264, 14);
getContentPane().add(lblName);
this.txtName = new JTextField();
this.txtName.setBounds(10, 25, 264, 20);
getContentPane().add(this.txtName);
this.txtName.setColumns(10);
JButton btnAddName = new JButton("Add Name to List");
btnAddName.setBounds(10, 49, 264, 23);
getContentPane().add(btnAddName);
btnAddName.addActionListener(new AddNameListener());
//Create ScrollPane
JScrollPane scrollPane = new JScrollPane();
scrollPane.setBounds(10, 75, 264, 126);
getContentPane().add(scrollPane);
//Create and Add JList
this.nameList = new JList();
nameList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
this.updateJList();
this.nameList.addListSelectionListener(new DisplayListener());
this.lblDisplayName = new JLabel("(Name will be shown here)");
this.lblDisplayName.setHorizontalAlignment(SwingConstants.CENTER);
this.lblDisplayName.setBounds(10, 203, 264, 14);
getContentPane().add(this.lblDisplayName);
this.setVisible(true);
}
private class AddNameListener implements ActionListener {
@Override
public void actionPerformed(ActionEvent e) {
updateJList();
}
}
private void updateJList(){
//I think I am missing something on this line.. HELP
String name = this.txtName.getText();
this.txtName.setText(name);
this.arrCounter++;
this.txtName.setText("");
DefaultListModel model = new DefaultListModel();
for(int i =0;i<this.nameArr.length;i++)
{
model.addElement(name);
}
this.nameList.setModel(model);
}
private class DisplayListener implements ListSelectionListener {
@Override
public void valueChanged(ListSelectionEvent arg0) {
displayName();
}
}
private void displayName()
{
int index = this.nameList.getSelectedIndex();
String name = this.nameArr[index];
this.lblDisplayName.setText(name);
}
public static void main(String[] args) {
Ex1 gui = new Ex1();
}
}
此时我非常非常迷茫,我似乎无法弄清楚出了什么问题,我已经学习 Java 大约 8 周了,我也一直在研究这个问题4小时。我真的认为我漏掉了一行,但如果您不这么认为,请随时发表评论。谢谢你们,很棒的人!
编辑:
经过一番讨论,OP 陈述的问题如下:
Whenever I run the code, it will work, however, when I key in the name in the text box to store it in the array and display it, it will not show up in the text box, therefore I cannot display the name.
不需要数组。
当您使用 Swing 组件时,数据存储在 "model" 中,在本例中为 DefaultListModel
。所以数据被添加到模型中,如果你想访问 JList 中的数据,你可以从模型中获取数据。
DefaultListModel model = new DefaultListModel();
该代码有误。您不希望每次单击按钮时都继续创建新模型。您想要将项目添加到现有模型以便显示所有项目。
阅读 How to Use Lists 上的 Swing 教程部分。 ListDemo
代码将向您展示如何更好地构造您的 class,以便您可以动态地添加和删除模型中的项目。所以下载代码并根据您的要求进行修改。
将以下代码与您问题中的代码进行比较。更改在代码后描述。
import javax.swing.DefaultListModel;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JScrollPane;
import javax.swing.JTextField;
import javax.swing.ListSelectionModel;
import javax.swing.SwingConstants;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import javax.swing.JList;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class Ex1 extends JFrame {
private JTextField txtName;
private JList<Object> nameList;
private String[] nameArr;
private int arrCounter = 0;
private JLabel lblDisplayName;
public Ex1() {
this.setTitle("Exercise01");
this.setSize(300, 266);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
getContentPane().setLayout(null);
this.nameArr = new String[10];
JLabel lblName = new JLabel("Name:");
lblName.setBounds(10, 11, 264, 14);
getContentPane().add(lblName);
this.txtName = new JTextField();
this.txtName.setBounds(10, 25, 264, 20);
getContentPane().add(this.txtName);
this.txtName.setColumns(10);
JButton btnAddName = new JButton("Add Name to List");
btnAddName.setBounds(10, 49, 264, 23);
getContentPane().add(btnAddName);
btnAddName.addActionListener(new AddNameListener());
// Create and Add JList
DefaultListModel<Object> model = new DefaultListModel<>();
this.nameList = new JList<>(model);
nameList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
// Create ScrollPane
JScrollPane scrollPane = new JScrollPane(nameList);
scrollPane.setBounds(10, 75, 264, 126);
getContentPane().add(scrollPane);
this.nameList.addListSelectionListener(new DisplayListener());
this.lblDisplayName = new JLabel("(Name will be shown here)");
this.lblDisplayName.setHorizontalAlignment(SwingConstants.CENTER);
this.lblDisplayName.setBounds(10, 203, 264, 14);
getContentPane().add(this.lblDisplayName);
this.setVisible(true);
}
private class AddNameListener implements ActionListener {
@Override
public void actionPerformed(ActionEvent e) {
updateJList();
}
}
private void updateJList() {
// I think I am missing something on this line.. HELP
String name = this.txtName.getText();
this.txtName.setText(name);
this.arrCounter++;
this.txtName.setText("");
DefaultListModel<Object> model = (DefaultListModel<Object>) nameList.getModel();
model.addElement(name);
}
private class DisplayListener implements ListSelectionListener {
@Override
public void valueChanged(ListSelectionEvent arg0) {
displayName();
}
}
private void displayName() {
int index = this.nameList.getSelectedIndex();
if (index >= 0) {
ListModel<Object> model = nameList.getModel();
Object obj = model.getElementAt(index);
String name = obj == null ? "" : obj.toString();
this.lblDisplayName.setText(name);
}
}
public static void main(String[] args) {
Ex1 gui = new Ex1();
}
}
首先为 JList
创建一个模型并将其传递给 JList
构造函数。
DefaultListModel<Object> model = new DefaultListModel<>();
this.nameList = new JList<>(model);
您正在添加一个空 JScrollPane
。您需要将 JList
传递给 JScrollPane
构造函数。
JScrollPane scrollPane = new JScrollPane(nameList);
在方法 updateJList()
中,您不需要新模型,您只需要通过向其中添加元素来更新现有模型。
DefaultListModel<Object> model = (DefaultListModel<Object>) nameList.getModel();
model.addElement(name);
编辑
对不起,我没有注意到你代码中显示从 nameList
中选择的值的那部分。
我将上述代码中的方法 displayName()
更改为以下内容:
private void displayName() {
int index = this.nameList.getSelectedIndex();
if (index >= 0) {
ListModel<Object> model = nameList.getModel();
Object obj = model.getElementAt(index);
String name = obj == null ? "" : obj.toString();
this.lblDisplayName.setText(name);
}
}
你不需要 nameArr
也不需要 arrCounter
。
我的印象是我的大部分代码都可以工作,但是,我相信我在函数 updatedJList()
下只遗漏了一行 updatedJList()
请帮忙,谢谢。
import javax.swing.DefaultListModel;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JScrollPane;
import javax.swing.JTextField;
import javax.swing.ListSelectionModel;
import javax.swing.SwingConstants;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import javax.swing.JList;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class Ex1 extends JFrame {
private JTextField txtName;
private JList nameList;
private String[] nameArr;
private int arrCounter = 0;
private JLabel lblDisplayName;
public Ex1(){
this.setTitle("Exercise01");
this.setSize(300, 266);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
getContentPane().setLayout(null);
this.nameArr = new String[10];
JLabel lblName = new JLabel("Name:");
lblName.setBounds(10, 11, 264, 14);
getContentPane().add(lblName);
this.txtName = new JTextField();
this.txtName.setBounds(10, 25, 264, 20);
getContentPane().add(this.txtName);
this.txtName.setColumns(10);
JButton btnAddName = new JButton("Add Name to List");
btnAddName.setBounds(10, 49, 264, 23);
getContentPane().add(btnAddName);
btnAddName.addActionListener(new AddNameListener());
//Create ScrollPane
JScrollPane scrollPane = new JScrollPane();
scrollPane.setBounds(10, 75, 264, 126);
getContentPane().add(scrollPane);
//Create and Add JList
this.nameList = new JList();
nameList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
this.updateJList();
this.nameList.addListSelectionListener(new DisplayListener());
this.lblDisplayName = new JLabel("(Name will be shown here)");
this.lblDisplayName.setHorizontalAlignment(SwingConstants.CENTER);
this.lblDisplayName.setBounds(10, 203, 264, 14);
getContentPane().add(this.lblDisplayName);
this.setVisible(true);
}
private class AddNameListener implements ActionListener {
@Override
public void actionPerformed(ActionEvent e) {
updateJList();
}
}
private void updateJList(){
//I think I am missing something on this line.. HELP
String name = this.txtName.getText();
this.txtName.setText(name);
this.arrCounter++;
this.txtName.setText("");
DefaultListModel model = new DefaultListModel();
for(int i =0;i<this.nameArr.length;i++)
{
model.addElement(name);
}
this.nameList.setModel(model);
}
private class DisplayListener implements ListSelectionListener {
@Override
public void valueChanged(ListSelectionEvent arg0) {
displayName();
}
}
private void displayName()
{
int index = this.nameList.getSelectedIndex();
String name = this.nameArr[index];
this.lblDisplayName.setText(name);
}
public static void main(String[] args) {
Ex1 gui = new Ex1();
}
}
此时我非常非常迷茫,我似乎无法弄清楚出了什么问题,我已经学习 Java 大约 8 周了,我也一直在研究这个问题4小时。我真的认为我漏掉了一行,但如果您不这么认为,请随时发表评论。谢谢你们,很棒的人!
编辑: 经过一番讨论,OP 陈述的问题如下:
Whenever I run the code, it will work, however, when I key in the name in the text box to store it in the array and display it, it will not show up in the text box, therefore I cannot display the name.
不需要数组。
当您使用 Swing 组件时,数据存储在 "model" 中,在本例中为 DefaultListModel
。所以数据被添加到模型中,如果你想访问 JList 中的数据,你可以从模型中获取数据。
DefaultListModel model = new DefaultListModel();
该代码有误。您不希望每次单击按钮时都继续创建新模型。您想要将项目添加到现有模型以便显示所有项目。
阅读 How to Use Lists 上的 Swing 教程部分。 ListDemo
代码将向您展示如何更好地构造您的 class,以便您可以动态地添加和删除模型中的项目。所以下载代码并根据您的要求进行修改。
将以下代码与您问题中的代码进行比较。更改在代码后描述。
import javax.swing.DefaultListModel;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JScrollPane;
import javax.swing.JTextField;
import javax.swing.ListSelectionModel;
import javax.swing.SwingConstants;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import javax.swing.JList;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class Ex1 extends JFrame {
private JTextField txtName;
private JList<Object> nameList;
private String[] nameArr;
private int arrCounter = 0;
private JLabel lblDisplayName;
public Ex1() {
this.setTitle("Exercise01");
this.setSize(300, 266);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
getContentPane().setLayout(null);
this.nameArr = new String[10];
JLabel lblName = new JLabel("Name:");
lblName.setBounds(10, 11, 264, 14);
getContentPane().add(lblName);
this.txtName = new JTextField();
this.txtName.setBounds(10, 25, 264, 20);
getContentPane().add(this.txtName);
this.txtName.setColumns(10);
JButton btnAddName = new JButton("Add Name to List");
btnAddName.setBounds(10, 49, 264, 23);
getContentPane().add(btnAddName);
btnAddName.addActionListener(new AddNameListener());
// Create and Add JList
DefaultListModel<Object> model = new DefaultListModel<>();
this.nameList = new JList<>(model);
nameList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
// Create ScrollPane
JScrollPane scrollPane = new JScrollPane(nameList);
scrollPane.setBounds(10, 75, 264, 126);
getContentPane().add(scrollPane);
this.nameList.addListSelectionListener(new DisplayListener());
this.lblDisplayName = new JLabel("(Name will be shown here)");
this.lblDisplayName.setHorizontalAlignment(SwingConstants.CENTER);
this.lblDisplayName.setBounds(10, 203, 264, 14);
getContentPane().add(this.lblDisplayName);
this.setVisible(true);
}
private class AddNameListener implements ActionListener {
@Override
public void actionPerformed(ActionEvent e) {
updateJList();
}
}
private void updateJList() {
// I think I am missing something on this line.. HELP
String name = this.txtName.getText();
this.txtName.setText(name);
this.arrCounter++;
this.txtName.setText("");
DefaultListModel<Object> model = (DefaultListModel<Object>) nameList.getModel();
model.addElement(name);
}
private class DisplayListener implements ListSelectionListener {
@Override
public void valueChanged(ListSelectionEvent arg0) {
displayName();
}
}
private void displayName() {
int index = this.nameList.getSelectedIndex();
if (index >= 0) {
ListModel<Object> model = nameList.getModel();
Object obj = model.getElementAt(index);
String name = obj == null ? "" : obj.toString();
this.lblDisplayName.setText(name);
}
}
public static void main(String[] args) {
Ex1 gui = new Ex1();
}
}
首先为 JList
创建一个模型并将其传递给 JList
构造函数。
DefaultListModel<Object> model = new DefaultListModel<>();
this.nameList = new JList<>(model);
您正在添加一个空 JScrollPane
。您需要将 JList
传递给 JScrollPane
构造函数。
JScrollPane scrollPane = new JScrollPane(nameList);
在方法 updateJList()
中,您不需要新模型,您只需要通过向其中添加元素来更新现有模型。
DefaultListModel<Object> model = (DefaultListModel<Object>) nameList.getModel();
model.addElement(name);
编辑
对不起,我没有注意到你代码中显示从 nameList
中选择的值的那部分。
我将上述代码中的方法 displayName()
更改为以下内容:
private void displayName() {
int index = this.nameList.getSelectedIndex();
if (index >= 0) {
ListModel<Object> model = nameList.getModel();
Object obj = model.getElementAt(index);
String name = obj == null ? "" : obj.toString();
this.lblDisplayName.setText(name);
}
}
你不需要 nameArr
也不需要 arrCounter
。