为什么我看不到 JList?

时间:2021-04-27 19:07:34

标签: java list swing jscrollpane jlist

主类

public class Main {

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

主窗口类

import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class CredentialManager extends JFrame implements ActionListener {

    public CredentialManager() {

        View.credentialManager.setSize(1200, 800);
        View.credentialManager.setResizable(false);
        View.credentialManager.setLayout(null);
        View.credentialManager.setLocationRelativeTo(null);
        View.credentialManager.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        View.credentialManager.setVisible(true);
        View.btnCredentialManagerManageUser.setBounds(550, 50, 120, 30);
        View.btnCredentialManagerSignOut.setBounds(50, 50, 95, 30);
        View.listCredentials.setBounds(100,100, 75,75);
        View.btnCredentialManagerManageUser.addActionListener(this);
        View.btnCredentialManagerSignOut.addActionListener(this);
        View.credentialManager.add(View.btnCredentialManagerManageUser);
        View.credentialManager.add(View.btnCredentialManagerSignOut);

        View.credentialManager.add(View.scrollPaneCredentials);
        View.scrollPaneCredentials.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);

        View.listModelCredentials.addElement("Credential1");
        View.listModelCredentials.addElement("Credential2");
        View.listModelCredentials.addElement("Credential3");
        View.listModelCredentials.addElement("Credential4");
        View.listModelCredentials.addElement("Credential5");
        View.listModelCredentials.addElement("Credential6");

    }

    @Override
    public void actionPerformed(ActionEvent actionEvent) {
        if (actionEvent.getSource().equals(View.btnCredentialManagerManageUser)) {
            System.out.println("Manager");
        } else if (actionEvent.getSource().equals(View.btnCredentialManagerSignOut)) {
            System.out.println("Sign Out");

        }
    }
}

使用 Swing 元素查看类

import javax.swing.*;

public class View {
    static JFrame credentialManager = new JFrame("Credential Manager");
    static JButton btnCredentialManagerManageUser = new JButton("Manage User");
    static JButton btnCredentialManagerSignOut = new JButton("Sign Out");
    static DefaultListModel<String> listModelCredentials = new DefaultListModel<>();
    static JList<String> listCredentials = new JList(listModelCredentials);
    static JScrollPane scrollPaneCredentials = new JScrollPane(listCredentials);
}

当我执行主类时,列表不会出现。我希望主框架包含两个按钮(出现)和带有滚动条的列表,但它没有出现。我尝试了很多东西,但其中任何一个都有效。

2 个答案:

答案 0 :(得分:0)

<块引用>

有滚动条但没有出现的列表

如果滚动窗格有大小,它将显示在 JList 里面,尽管在左上角。

View.scrollPaneCredentials.setSize(new Dimension(300, 300));

要让您了解发生这种情况的原因,请进行以下更改:

View.credentialManager.setLayout(new FlowLayout());
//                               ^--- changed from "null"

... 然后将这些添加到构造函数的底部:

View.credentialManager.pack();
View.credentialManager.setVisible(true);
//                     ^---- move this to bottom 

请注意,布局管理器将显示您的列表,尽管位于最右侧。

要获得恰到好处的布局管理器,请查看此文档:https://docs.oracle.com/javase/tutorial/uiswing/layout/using.html

答案 1 :(得分:-1)

一个必要的前提,我不明白在 Swing 中需要绝对定位元素而不是使用布局管理器。

也就是说,您尝试将 listCredentials 定位在滚动窗格内,而不是滚动窗格本身,因此替换:

View.listCredentials.setBounds(100,100, 75,75);

View.scrollPaneCredentials.setBounds(100,100, 75,75);
相关问题