为什么我看不到 JList?
Why can't I see the JList?
主要Class
public class Main {
public static void main(String[] args) {
new CredentialManager();
}
}
主要WindowClass
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 元素查看 Class
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);
}
当我执行 main class 时,列表没有出现。我希望主框架包含两个按钮(出现)和带有滚动条的列表,但它没有出现。我已经尝试了很多东西,但其中任何一个都有效。
一个必要的前提,我不明白在Swing中需要绝对定位元素而不是使用布局管理器。
也就是说,您尝试定位 listCredentials,它位于滚动窗格内,而不是滚动窗格本身,因此替换:
View.listCredentials.setBounds(100,100, 75,75);
和
View.scrollPaneCredentials.setBounds(100,100, 75,75);
the list with the scrollbar but it does not appear
如果滚动窗格有大小,它将与 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
主要Class
public class Main {
public static void main(String[] args) {
new CredentialManager();
}
}
主要WindowClass
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 元素查看 Class
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);
}
当我执行 main class 时,列表没有出现。我希望主框架包含两个按钮(出现)和带有滚动条的列表,但它没有出现。我已经尝试了很多东西,但其中任何一个都有效。
一个必要的前提,我不明白在Swing中需要绝对定位元素而不是使用布局管理器。
也就是说,您尝试定位 listCredentials,它位于滚动窗格内,而不是滚动窗格本身,因此替换:
View.listCredentials.setBounds(100,100, 75,75);
和
View.scrollPaneCredentials.setBounds(100,100, 75,75);
the list with the scrollbar but it does not appear
如果滚动窗格有大小,它将与 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