如何解决JTextField 输入到 JList - 列表不出现
我目前正在尝试开发一个桌面志愿者应用程序,该应用程序允许用户通过文本框输入姓名,然后在 JList
的同一窗口中显示该姓名。但是,目前我的代码没有显示我的面板/列表或我添加的滚动条。
我的代码如下。非常感谢任何帮助!
import javax.swing.*;
import java.awt.event.*;
public class StudentViewer implements ActionListener {
JFrame frame = new JFrame("View Students");
JPanel panel = new JPanel();
JButton button = new JButton();
JButton button2 = new JButton();
JTextField field = new JTextField("",25);
DefaultListModel<String> model = new DefaultListModel<>();
JList<String> list = new JList<>(model);
public StudentViewer() {
button = new JButton("Click to add to the list");
button.setFocusPainted(false);
button.addActionListener(this);
button2 = new JButton("New Window");
button2.setFocusPainted(false);
button2.addActionListener(this);
panel.add(field);
panel.add(button);
panel.add(button2);
list = new JList(model);
list.setFixedCellWidth(300);
list.setFixedCellHeight(50);
list.setLayoutOrientation(JList.VERTICAL);
// scroll = new JScrollPane(list,JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED,JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
// panel.add(scroll);
frame.add(panel);
frame.setSize(400,400);
frame.setVisible(true);
frame.setResizable(false);
frame.setDefaultCloSEOperation(JFrame.HIDE_ON_CLOSE);
frame.setLocationRelativeto(null);
}
public void actionPerformed(ActionEvent e) {
if (e.getSource() == button) {
String text = field.getText();
model.addElement(text);
JOptionPane.showMessageDialog(frame,"Added to the list","Done",JOptionPane.informatION_MESSAGE);
frame.dispose();
} else if (e.getSource() == button2) {
System.exit(0);
}
}
public static void main(String[] args) {
Runnable r = () -> {
new StudentViewer();
};
SwingUtilities.invokelater(r);
}
}
解决方法
有很多代码要么是问题的积极组成部分,要么就是毫无意义。查看为生成此屏幕截图所做的更改。
import javax.swing.*;
import java.awt.event.*;
public class StudentViewer implements ActionListener {
JFrame frame = new JFrame("View Students");
JPanel panel = new JPanel();
JButton button = new JButton();
JButton button2 = new JButton();
JTextField field = new JTextField("",5);
DefaultListModel<String> model = new DefaultListModel<>();
JList<String> list = new JList<>(model);
public StudentViewer() {
button = new JButton("Click to add to the list");
button.setFocusPainted(false);
button.addActionListener(this);
button2 = new JButton("New Window");
button2.setFocusPainted(false);
button2.addActionListener(this);
panel.add(field);
panel.add(button);
panel.add(button2);
list = new JList(model);
//list.setFixedCellWidth(300);
//list.setFixedCellHeight(50);
//list.setLayoutOrientation(JList.VERTICAL);
JScrollPane scroll = new JScrollPane(list,JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED,JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
panel.add(scroll);
frame.add(panel);
// frame.setSize(400,400); redundant given pack()
frame.pack();
frame.setVisible(true);
//frame.setResizable(false);
frame.setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE);
frame.setLocationRelativeTo(null);
}
public void actionPerformed(ActionEvent e) {
if (e.getSource() == button) {
String text = field.getText();
model.addElement(text);
JOptionPane.showMessageDialog(frame,"Added to the list","Done",JOptionPane.INFORMATION_MESSAGE);
//frame.dispose();
} else if (e.getSource() == button2) {
System.exit(0);
}
}
public static void main(String[] args) {
Runnable r = () -> {
new StudentViewer();
};
SwingUtilities.invokeLater(r);
}
}
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。