简体   繁体   English

为什么我看不到 JList?

[英]Why can't I see the JList?

Main Class主Class

public class Main {

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

Main Window Class主Window Class

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

        }
    }
}

View Class with the Swing elements查看 Class 与 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);
}

When I execute the main class the list does not appear.当我执行主 class 时,列表不会出现。 I expect the main frame to contain the two button(which appears) and the list with the scrollbar but it does not appear.我希望主框架包含两个按钮(出现)和带有滚动条的列表,但它没有出现。 I have tried many things but any of them work.我尝试了很多东西,但其中任何一个都有效。

the list with the scrollbar but it does not appear带有滚动条的列表,但没有出现

If the scrollpane has a size, it will then display with the JList inside, albeit in the top left corner.如果滚动窗格有大小,它会在里面显示 JList,尽管在左上角。

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

To give you an idea of why this occurs, make the following changes:为了让您了解为什么会发生这种情况,请进行以下更改:

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

... and then add these to the bottom of your constructor: ...然后将这些添加到构造函数的底部:

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

Notice, the layout manager will display your list, albeit on the far right.请注意,布局管理器将显示您的列表,尽管在最右侧。

To get a layout manager that's just right, check out this documentation: https://docs.oracle.com/javase/tutorial/uiswing/layout/using.html要获得一个恰到好处的布局管理器,请查看此文档: https://docs.oracle.com/javase/tutorial/uiswing/layout/using.ZFC35FDC70D5FC69D2369883A8EZC7

A necessary premise, I don't understand the need of absolute positioning elements in Swing instead of using layout managers.一个必要的前提,我不明白 Swing 中需要绝对定位元素而不是使用布局管理器。

Said that, you try to position listCredentials, that is inside a scroll pane, instead of the scroll pane itself, so replace:说,您尝试 position listCredentials,即在滚动窗格内,而不是滚动窗格本身,所以替换:

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

with

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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