在不使用JPanel的情况下在JList上添加JScrollPane

时间:2013-12-17 10:17:13

标签: 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();
}

}

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

非常感谢您的帮助。

1 个答案:

答案 0 :(得分:3)

Yout尚未将列表添加到滚动窗格。您只是将列表添加到contentpane。

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()。在revalidate()之前添加repaint()。您只需要在方法

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(模型,视图,控制器)范例。