[英]DefaultListModel in JList in JScrollPane, can't see the JList
[英]Why can't I see the JList?
主Class
public class Main {
public static void main(String[] args) {
new CredentialManager();
}
}
主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");
}
}
}
查看 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);
}
當我執行主 class 時,列表不會出現。 我希望主框架包含兩個按鈕(出現)和帶有滾動條的列表,但它沒有出現。 我嘗試了很多東西,但其中任何一個都有效。
帶有滾動條的列表,但沒有出現
如果滾動窗格有大小,它會在里面顯示 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.ZFC35FDC70D5FC69D2369883A8EZC7
一個必要的前提,我不明白 Swing 中需要絕對定位元素而不是使用布局管理器。
說,您嘗試 position listCredentials,即在滾動窗格內,而不是滾動窗格本身,所以替換:
View.listCredentials.setBounds(100,100, 75,75);
和
View.scrollPaneCredentials.setBounds(100,100, 75,75);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.