简体   繁体   English

JTextField 输入到 JList - 列表不出现

[英]JTextField input to JList - list does not appear

I am currently trying to develop a desktop volunteer application that allows a user to input a name via a textbox and then display this name on the same window in a JList .我目前正在尝试开发一个桌面志愿者应用程序,该应用程序允许用户通过文本框输入名称,然后在JList中的同一 window 上显示此名称。 However, currently my code is not displaying my panel/ list or the scrollbar that I added as well.但是,目前我的代码没有显示我的面板/列表或我添加的滚动条。

My code is below.我的代码如下。 Any help is greatly appreciated!任何帮助是极大的赞赏!

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);
    }
}

There is much about the code that either was actively part of the problem or just made no sense.有很多代码要么是问题的一部分,要么就是没有意义。 Have a look over the changes made to produce this screenshot.查看为生成此屏幕截图所做的更改。

在此处输入图像描述

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM