[英]JTextField input to JList - list does not appear
我目前正在嘗試開發一個桌面志願者應用程序,該應用程序允許用戶通過文本框輸入名稱,然后在JList
中的同一 window 上顯示此名稱。 但是,目前我的代碼沒有顯示我的面板/列表或我添加的滾動條。
我的代碼如下。 任何幫助是極大的贊賞!
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);
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.