简体   繁体   中英

Why can't I see the JList?

Main Class

public class Main {

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

Main 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

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. 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.

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

A necessary premise, I don't understand the need of absolute positioning elements in Swing instead of using layout managers.

Said that, you try to position listCredentials, that is inside a scroll pane, instead of the scroll pane itself, so replace:

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

with

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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