java - 在 JList 上添加 JScrollPane 而不使用 JPanel

标签 java swing jpanel jscrollpane jlist

我的程序需要帮助。我需要在 JList 上放置 JScrollPane,而不将 JList 放在 JPanel 上。

import java.awt.*;
import java.awt.event.*;
import java.util.*;
import javax.swing.*;

public class refurbished extends JFrame implements ActionListener {
    ArrayList<String> names;
    JButton add;
    JTextField inputName;
    JScrollPane scrollName;
    JList nameList;

public refurbished() {
    setSize(700,500);
    setLayout(null);

    names = new ArrayList<String>();

    add = new JButton("Add");
    add.setBounds(25,200,90,30);
    add.setBackground(Color.WHITE);
    add.addActionListener(this);

    inputName = new JTextField();
    inputName.setBounds(150,350,150,30);

    nameList = new JList(names.toArray());

    scrollName = new JScrollPane(nameList);
    scrollName.setBounds(150,75,150,200);

    getContentPane().add(add);
    getContentPane().add(inputName);
    getContentPane().add(scrollName);

    setVisible(true);
}

public void actionPerformed (ActionEvent buttonclick) {
    if (buttonclick.getSource() == add) {
        names.add(inputName.getText().toLowerCase());
        nameList = new JList(names.toArray());
        scrollName = new JScrollPane(nameList);
        scrollName.setBounds(150,75,150,200);
}
}

public static void main (String[] args) {
    refurbished r = new refurbished();
}

}

你能帮我吗?我真的需要您的帮助,因为这是我的代码中唯一缺少的功能。

非常感谢您的帮助。

最佳答案

您尚未将列表添加到滚动 Pane 。您仅将列表添加到内容 Pane 。

scrollName = new JScrollPane();
scrollNumber = new JScrollPane();

getContentPane().add(nameList);      <-- Get rid of this
getContentPane().add(numberList);    <-- Get rid of this

你需要这个

scrollName = new JScrollPane(nameList);
scrollNumber = new JScrollPane(numberList);

getContentPane().add(scrollName);      
getContentPane().add(scrollNumber);

正如 @Alex2410 在下面的评论中指出的,“您还需要使用 LayoutManager,或者将 Bounds 设置为 JScrollPane 而不是 JList”

更新:对原始海报更新

在添加或删除组件后,您需要同时进行 revalidate() repaint() 操作。在 repaint() 之前添加 revalidate()。您只需要在方法中 revalidate() repaint() 一次

<小时/>

编辑:如果要更新列表,请使用 ListModel。您不需要用新列表替换整个列表

请参阅此代码。我所做的是使用 DefaultListModel 并将该模型设置为 Jlist。然后您可以动态地将元素添加到列表中。我修复了你的代码并且它有效。我对添加的内容和删除的内容发表了评论

import java.awt.*;
import java.awt.event.*;
import java.util.*;
import javax.swing.*;

public class refurbished extends JFrame implements ActionListener {

    ArrayList<String> names;
    JButton add;
    JTextField inputName;
    JScrollPane scrollName;
    JList nameList;
    DefaultListModel model;          <-- declare DefaultListModel

    public refurbished() {
        setSize(700, 500);
        setLayout(null);

        names = new ArrayList<String>();

        add = new JButton("Add");
        add.setBounds(25, 200, 90, 30);
        add.setBackground(Color.WHITE);
        add.addActionListener(this);

        inputName = new JTextField();
        inputName.setBounds(150, 350, 150, 30);

        model = new DefaultListModel();                <-- Initialize model
        nameList = new JList(model);                   <-- set model to list

        scrollName = new JScrollPane(nameList);
        scrollName.setBounds(150, 75, 150, 200);

        getContentPane().add(add);
        getContentPane().add(inputName);
        getContentPane().add(scrollName);

        setVisible(true);
    }

    public void actionPerformed(ActionEvent buttonclick) {
        if (buttonclick.getSource() == add) {
            //names.add(inputName.getText().toLowerCase());   
            //nameList = new JList(names.toArray());        <-- don't need all this
            //scrollName = new JScrollPane(nameList);
            //scrollName.setBounds(150, 75, 150, 200);
            String name = inputName.getText();           <-- get input
            names.add(name);                             
            model.addElement(name);                      <-- add name to model      
        }
    }

    public static void main(String[] args) {
        refurbished r = new refurbished();
    }
}

看看Using Models 。您应该花时间学习 MVC(模型、 View 、 Controller )范例。

关于java - 在 JList 上添加 JScrollPane 而不使用 JPanel,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20631401/

相关文章:

使用 Netbeans 的 Java 桌面应用程序

java - 修改参数的值

java - 使用 Rally Java Rest api 使用字段名称查找引用

扫描导入的文件时出现 Java 异常

java - 从 JLabel 中选择文本?

java - 更改/删除默认标记/关注元素

java - 如何在 JPanel 上绘制 vector 图形

java - 发布值并将图像上传到android中的php服务器

java - 使 JComponent 不透明

java - GridLayout 删除 JPanel 之间的填充