我目前正在尝试制作一个放置在 JScrollPane 中的不透明 JList。我试图将所有内容都设置为setOpaque(false)
,但它仍然无法正常工作。
这是代码:
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import javax.swing.DefaultListCellRenderer;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
public class NavigationView extends JPanel{
private static final long serialVersionUID = 8033665282200374807L;
private JScrollPane pane;
private JList list;
public NavigationView() {
this.initialize();
this.build();
this.configure();
}
public void initialize() {
this.pane = new JScrollPane();
this.list = new JList();
}
public void build() {
this.setLayout(new GridBagLayout());
GridBagConstraints gbc = new GridBagConstraints();
gbc.fill = GridBagConstraints.BOTH;
gbc.gridx = 0;
gbc.gridy = 0;
gbc.weightx = 1;
gbc.weighty = 1;
gbc.gridwidth = 1;
gbc.insets = new Insets(5, 5, 5, 5);
this.add(this.pane, gbc);
}
public void configure() {
this.pane.setOpaque(false);
this.pane.setViewportView(this.list);
this.pane.getViewport().setOpaque(false);
this.list.setOpaque(false);
((DefaultListCellRenderer) this.list.getCellRenderer()).setOpaque(false);
}
}
如果我将此面板添加到具有彩色背景的 JFrame 中,我仍然会看到NavigationView
具有白色背景的 -Panel 的边界。我究竟做错了什么?