添加滚动条时,jList消失

时间:2017-05-31 16:13:41

标签: java

当我取消注释下面的第6行和第11行时,jList消失。

JScrollPane scrollPane = new JScrollPane();
JList list = new JList();
list.setBackground(Color.CYAN);
list.setBounds(284, 22, 127, 102);
frame.getContentPane().add(list);

//scrollPane = new JScrollPane(list, JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
scrollPane.setVisible(true);
frame.getContentPane().add(scrollPane);
frame.getContentPane().add(textArea);
list.setVisible(true);
//scrollPane.setViewportView(list);

我看到了这个答案

  

将滚动窗格添加到容器

但我不知道该怎么做。

以下是所有课程:

public class Form{
    private String dbDriver= "com.mysql.jdbc.Driver";
    private String dbURL= "jdbc:mysql://localhost/datas";

    private JFrame frame;
    private JTextField txtIdPers;
    private JTextField txtName;
    private JTextField txtIdToDelete;

    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    Form window = new Form();
                    window.frame.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }
    public Form() {
        initialize();
    }

    private void initialize() {

        frame = new JFrame();
        frame.setBounds(100, 100, 450, 300);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.getContentPane().setLayout(null);

        DefaultListModel listModel= new DefaultListModel();

        JScrollPane scrollPane = new JScrollPane();

        JList list = new JList();
        list.setBackground(Color.CYAN);
        list.setBounds(284, 22, 127, 102);
        frame.getContentPane().add(list);

        Button btnDisplayAll = new Button("Display All");
        btnDisplayAll.setBounds(341, 205, 70, 22);
        frame.getContentPane().add(btnDisplayAll);
        displayPersons(btnDisplayAll, textArea, list, listModel);
    }


    private void displayPersons(Button btnDisplayAll, JTextArea textArea, JList list, DefaultListModel listModel){
        btnDisplayAll.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {

                ControllerDB controller = new ControllerDB();
                controller.driver= dbDriver;
                controller.url= dbURL;

                try {
                    controller.select("*", "person", "");
                } catch (SQLException e1) {
                    e1.printStackTrace();
                }
                for(int i=0; i<controller.Selected.size();i++){

                    for(int j=0; j<controller.Selected.get(i).length; j++)
                    {
                        listModel.addElement(controller.Selected.get(i)[j]);

                    }
                    //listModel.addElement("\n");
                }
                list.setModel(listModel);
            }
        });
    }
}

3 个答案:

答案 0 :(得分:0)

您未将list添加到scrollPane

scrollPane.add(list)

删除该行

frame.getContentPane().add(list);

答案 1 :(得分:0)

将滚动条添加到容器中,如下所示:

JScrollPane scrollPane = new JScrollPane( panelName );

试一试。

或试试这个:

JScrollPane scrollPane = new JScrollPane(listName);
JpanelName.add(scrollPane);
getContentPane().add(JpanelName);
pack();

请记住,当添加到滚动窗格的组件的首选大小大于滚动窗格的大小时,会出现滚动条。

也许尝试使用Box Layout代替?它可能有帮助吗?然后,您可以使用以下代码在组件之间添加垂直空间:

JpanelName.add(Box.createVerticalStrut(200));

答案 2 :(得分:0)

使用JList()的方法之一,针对您的问题:

import java.awt.Color;
import java.awt.Component;
import java.awt.Point;
import java.awt.PopupMenu;
import javax.swing.DefaultListModel;
import javax.swing.JFrame;
import javax.swing.JList;
import javax.swing.JPopupMenu;
import javax.swing.JScrollPane;

public class ExampJList extends JFrame {

    JScrollPane scrollPane = new JScrollPane();
    DefaultListModel model = new DefaultListModel();
    JList list = new JList(model);

    public ExampJList() {
        list.setBackground(Color.CYAN);
        list.setBounds(284, 22, 127, 102);
        getContentPane().add(list);

        // You can implement different action for popup
        JPopupMenu textArea = new JPopupMenu() {
            public void show(Component invoker, int x, int y) {
                int row = list.locationToIndex(new Point(x, y));
                if (row != -1) {
                    list.setSelectedIndex(row);
                }
                super.show(invoker, x, y);
            }
        };

        list.setComponentPopupMenu(textArea);

        scrollPane = new JScrollPane(list, JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
        scrollPane.setVisible(true);
        list.setVisible(true);
        scrollPane.setViewportView(list);
        getContentPane().add(scrollPane);
        setSize(600, 300);
        setVisible(true);
        updater.start();
    }

    final Thread updater = new Thread() {
        @Override
        public void run() {
            for (int i = 0; i < 10; i++) {
                model.addElement(i);
                try {
                    Thread.sleep(500);
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
            }
        }
    };

    public static void main(String[] args) {
        new ExampJList();
    }

}

菜单执行后,JFrame如下所示: enter image description here