滚动条没有出现在 JList 上
scroll bar doesn't appear on JList
我正在使用 GridBagLayout
并且有一个 JList
我的自定义 class。 JList
本身如我所料显示,但没有出现滚动条。 (我的数据量也比我的 JList
大。)我检查了很多相关链接,他们都提到了这两行,但它对我不起作用。
JScrollPane scrollPane = new JScrollPane(list);
add(scrollPane);
完整代码:
import java.awt.Button;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Label;
import javax.swing.*;
public class UserPart extends JFrame {
Label messageL = new Label("Confirm an item for more options.");
Button confirmB = new Button("Confirm");
Button backB = new Button("Back");
DefaultListModel<User2> model;
public UserPart() {
JList<User2> list = new JList<User2>();
model = new DefaultListModel<User2>();
for (int i = 0; i < MainActivity2.arr.size(); i++) {
User2 u = new User2();
u.setForShow(i);
model.addElement(u);
}
// SETTING THE APPEARANCE OF THE JLIST
list.setModel(model);
list.setCellRenderer(new MyListCellRenderer());
list.setLayoutOrientation(JList.VERTICAL);
list.setPreferredSize(new Dimension(500, 750));
//////////////////////////////////////////////
GridBagLayout grid = new GridBagLayout();
GridBagConstraints gbc = new GridBagConstraints();
setLayout(grid);
setTitle("List of users");
GridBagLayout layout = new GridBagLayout();
this.setLayout(layout);
gbc.gridx = 0;
gbc.gridy = 0;
this.add(messageL, gbc);
gbc.gridx = 0;
gbc.gridy = 1;
this.add(list, gbc); //ADDED JLIST TO LAYOUT HERE
JScrollPane scrollPane = new JScrollPane(list); // DEFINED SCROLL BAR HERE
add(scrollPane); // ADDED SCROLL BAR TO LAYOUT HERE
setSize(1000, 1500);
setPreferredSize(getSize());
setVisible(true);
setDefaultCloseOperation(EXIT_ON_CLOSE);
}
}
将约束权重(weightx
、weighty
)设置为大于零的值 - 要使用所有布局 - 还将 fill
设置为 BOTH
。不要将列表直接添加到框架,只需添加滚动窗格 和 如上所述设置的约束。不要将 AWT 组件 (Label
) 与 Swing 组件混合使用,而应使用 JLabel
。总结如下:
JLabel label = new JLabel("Confirm an item for more options.");
JScrollPane scroll = new JScrollPane(list);
GridBagConstraints gbc = new GridBagConstraints();
JFrame frame = new JFrame();
frame.setLayout(new GridBagLayout());
gbc.gridx = 0;
gbc.gridy = 0;
frame.add(label, gbc);
gbc.gridx = 0;
gbc.gridy = 1;
gbc.weightx = 1;
gbc.weighty = 1;
gbc.fill = GridBagConstraints.BOTH;
frame.add(scroll, gbc);
我正在使用 GridBagLayout
并且有一个 JList
我的自定义 class。 JList
本身如我所料显示,但没有出现滚动条。 (我的数据量也比我的 JList
大。)我检查了很多相关链接,他们都提到了这两行,但它对我不起作用。
JScrollPane scrollPane = new JScrollPane(list);
add(scrollPane);
完整代码:
import java.awt.Button;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Label;
import javax.swing.*;
public class UserPart extends JFrame {
Label messageL = new Label("Confirm an item for more options.");
Button confirmB = new Button("Confirm");
Button backB = new Button("Back");
DefaultListModel<User2> model;
public UserPart() {
JList<User2> list = new JList<User2>();
model = new DefaultListModel<User2>();
for (int i = 0; i < MainActivity2.arr.size(); i++) {
User2 u = new User2();
u.setForShow(i);
model.addElement(u);
}
// SETTING THE APPEARANCE OF THE JLIST
list.setModel(model);
list.setCellRenderer(new MyListCellRenderer());
list.setLayoutOrientation(JList.VERTICAL);
list.setPreferredSize(new Dimension(500, 750));
//////////////////////////////////////////////
GridBagLayout grid = new GridBagLayout();
GridBagConstraints gbc = new GridBagConstraints();
setLayout(grid);
setTitle("List of users");
GridBagLayout layout = new GridBagLayout();
this.setLayout(layout);
gbc.gridx = 0;
gbc.gridy = 0;
this.add(messageL, gbc);
gbc.gridx = 0;
gbc.gridy = 1;
this.add(list, gbc); //ADDED JLIST TO LAYOUT HERE
JScrollPane scrollPane = new JScrollPane(list); // DEFINED SCROLL BAR HERE
add(scrollPane); // ADDED SCROLL BAR TO LAYOUT HERE
setSize(1000, 1500);
setPreferredSize(getSize());
setVisible(true);
setDefaultCloseOperation(EXIT_ON_CLOSE);
}
}
将约束权重(weightx
、weighty
)设置为大于零的值 - 要使用所有布局 - 还将 fill
设置为 BOTH
。不要将列表直接添加到框架,只需添加滚动窗格 和 如上所述设置的约束。不要将 AWT 组件 (Label
) 与 Swing 组件混合使用,而应使用 JLabel
。总结如下:
JLabel label = new JLabel("Confirm an item for more options.");
JScrollPane scroll = new JScrollPane(list);
GridBagConstraints gbc = new GridBagConstraints();
JFrame frame = new JFrame();
frame.setLayout(new GridBagLayout());
gbc.gridx = 0;
gbc.gridy = 0;
frame.add(label, gbc);
gbc.gridx = 0;
gbc.gridy = 1;
gbc.weightx = 1;
gbc.weighty = 1;
gbc.fill = GridBagConstraints.BOTH;
frame.add(scroll, gbc);