Java JList的JTextField输入-列表未显示
我目前正在尝试开发一个桌面志愿者应用程序,允许用户通过文本框输入姓名,然后在Java JList的JTextField输入-列表未显示,java,swing,user-interface,textfield,jlist,Java,Swing,User Interface,Textfield,Jlist,我目前正在尝试开发一个桌面志愿者应用程序,允许用户通过文本框输入姓名,然后在JList的同一窗口中显示该姓名。但是,目前我的代码没有显示我添加的面板/列表或滚动条 我的代码如下。非常感谢您的帮助 import javax.swing.*; import java.awt.event.*; public class StudentViewer implements ActionListener { JFrame frame = new JFrame("View Student
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.*;
导入java.awt.event.*;
公共类StudentViewer实现ActionListener{
JFrame=新JFrame(“查看学生”);
JPanel面板=新的JPanel();
JButton button=新JButton();
JButton button2=新JButton();
JTextField=newjtextfield(“,25”);
DefaultListModel=新的DefaultListModel();
JList列表=新的JList(型号);
公共学生查看器(){
按钮=新的JButton(“单击以添加到列表”);
按钮。SetFocuspaint(假);
addActionListener(这个);
button2=新按钮(“新窗口”);
按钮2.设置焦点已上漆(假);
按钮2.addActionListener(此);
面板。添加(字段);
面板。添加(按钮);
面板。添加(按钮2);
列表=新JList(型号);
列表。设置固定单元格宽度(300);
列表。设置固定单元高度(50);
list.setLayoutOrientation(JList.VERTICAL);
//scroll=新的JScrollPane(列表,JScrollPane.VERTICAL\u SCROLLBAR\u AS\u NEEDED,JScrollPane.HORIZONTAL\u SCROLLBAR\u AS\u NEEDED);
//面板。添加(滚动);
框架。添加(面板);
框架。设置尺寸(400400);
frame.setVisible(true);
frame.setresizeable(false);
frame.setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE);
frame.setLocationRelativeTo(空);
}
已执行的公共无效操作(操作事件e){
如果(例如getSource()==按钮){
String text=field.getText();
模型.附录(文本);
showMessageDialog(框架,“添加到列表”、“完成”,
JOptionPane.INFORMATION(信息和消息);
frame.dispose();
}否则如果(例如getSource()==按钮2){
系统出口(0);
}
}
公共静态void main(字符串[]args){
可运行r=()->{
new StudentViewer();
};
SwingUtilities.invokeLater(r);
}
}
关于代码的很多内容要么是问题的一部分,要么就是毫无意义。请查看为生成此屏幕截图所做的更改
import javax.swing.*;
导入java.awt.event.*;
公共类StudentViewer实现ActionListener{
JFrame=新JFrame(“查看学生”);
JPanel面板=新的JPanel();
JButton button=新JButton();
JButton button2=新JButton();
JTextField=newjtextfield(“,5”);
DefaultListModel=新的DefaultListModel();
JList列表=新的JList(型号);
公共学生查看器(){
按钮=新的JButton(“单击以添加到列表”);
按钮。SetFocuspaint(假);
addActionListener(这个);
button2=新按钮(“新窗口”);
按钮2.设置焦点已上漆(假);
按钮2.addActionListener(此);
面板。添加(字段);
面板。添加(按钮);
面板。添加(按钮2);
列表=新JList(型号);
//列表。设置固定单元格宽度(300);
//列表。设置固定单元高度(50);
//list.setLayoutOrientation(JList.VERTICAL);
JScrollPane scroll=新的JScrollPane(列表,JScrollPane.VERTICAL\u SCROLLBAR\u需要时,JScrollPane.HORIZONTAL\u SCROLLBAR\u需要时);
面板。添加(滚动);
框架。添加(面板);
//frame.setSize(400400);冗余给定包()
frame.pack();
frame.setVisible(true);
//frame.setresizeable(false);
frame.setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE);
frame.setLocationRelativeTo(空);
}
已执行的公共无效操作(操作事件e){
如果(例如getSource()==按钮){
String text=field.getText();
模型.附录(文本);
showMessageDialog(框架,“添加到列表”、“完成”,
JOptionPane.INFORMATION(信息和消息);
//frame.dispose();
}否则如果(例如getSource()==按钮2){
系统出口(0);
}
}
公共静态void main(字符串[]args){
可运行r=()->{
new StudentViewer();
};
SwingUtilities.invokeLater(r);
}
}
更新您的问题并在此处添加代码,我们无法从图像复制!这有用吗?阅读上的Swing教程中的部分。ListDemo
示例代码正是您想要的。代码还将向您展示如何在事件调度线程(EDT)
上创建GUI时更好地构造类。
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);
}
}