Java 如何防止在 JScrollPane 中填充项目
Java How to prevent fill item in JScrollPane
我正在尝试动态地在 JScrollPane 中创建项目。
但是当我添加 JPanel(代表项目)时,面板会填满所有滚动区域。
当我添加 2 个 JPanel 项目时,它划分并填充了所有滚动区域。
enter image description here
'Green' Area 意思是Item Area 这张图片是我添加一个Item到Scroll Area的时候
enter image description here
'cyan' 颜色表示另一项。这张照片是我将第二个项目添加到滚动区域时的照片
我想修复每个项目的高度(和填充宽度)
当我添加添加到滚动窗格区域的项目时,左侧区域保持空白space。
我该怎么做?
下面是我的代码。
谢谢。
public class JPanelINScrollPanel extends JFrame{
public JPanelINScrollPanel(){
this.setSize(700,700);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel basePanel = new JPanel();
JScrollPane scrollPanel = new JScrollPane();
JPanel listPanel = new JPanel();
JPanel itemPanel1 = new JPanel();
JPanel itemPanel2 = new JPanel();
JPanel itemPanel3 = new JPanel();
basePanel.setBackground(Color.BLACK);
getContentPane().add(basePanel);
basePanel.setLayout(new BorderLayout(0, 0));
scrollPanel.setBackground(Color.BLUE);
basePanel.add(scrollPanel);
listPanel.setSize(500,500);
listPanel.setBackground(Color.red);
listPanel.setLayout(new BoxLayout(listPanel, BoxLayout.PAGE_AXIS));
scrollPanel.add(listPanel);
scrollPanel.setViewportView(listPanel);
//itemPanel1.setSize(50,50);
itemPanel1.setPreferredSize(new Dimension(50, 50));
itemPanel1.setBackground(Color.GREEN);
listPanel.add(itemPanel1);
}
public static void main(String[] args){
JPanelINScrollPanel test = new JPanelINScrollPanel();
test.setVisible(true);
}
}
很简单
只需将MaximumSize 设置为项目面板。
解决了。
我正在尝试动态地在 JScrollPane 中创建项目。
但是当我添加 JPanel(代表项目)时,面板会填满所有滚动区域。
当我添加 2 个 JPanel 项目时,它划分并填充了所有滚动区域。
enter image description here
'Green' Area 意思是Item Area 这张图片是我添加一个Item到Scroll Area的时候
enter image description here
'cyan' 颜色表示另一项。这张照片是我将第二个项目添加到滚动区域时的照片
我想修复每个项目的高度(和填充宽度)
当我添加添加到滚动窗格区域的项目时,左侧区域保持空白space。
我该怎么做?
下面是我的代码。
谢谢。
public class JPanelINScrollPanel extends JFrame{
public JPanelINScrollPanel(){
this.setSize(700,700);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel basePanel = new JPanel();
JScrollPane scrollPanel = new JScrollPane();
JPanel listPanel = new JPanel();
JPanel itemPanel1 = new JPanel();
JPanel itemPanel2 = new JPanel();
JPanel itemPanel3 = new JPanel();
basePanel.setBackground(Color.BLACK);
getContentPane().add(basePanel);
basePanel.setLayout(new BorderLayout(0, 0));
scrollPanel.setBackground(Color.BLUE);
basePanel.add(scrollPanel);
listPanel.setSize(500,500);
listPanel.setBackground(Color.red);
listPanel.setLayout(new BoxLayout(listPanel, BoxLayout.PAGE_AXIS));
scrollPanel.add(listPanel);
scrollPanel.setViewportView(listPanel);
//itemPanel1.setSize(50,50);
itemPanel1.setPreferredSize(new Dimension(50, 50));
itemPanel1.setBackground(Color.GREEN);
listPanel.add(itemPanel1);
}
public static void main(String[] args){
JPanelINScrollPanel test = new JPanelINScrollPanel();
test.setVisible(true);
}
}
很简单
只需将MaximumSize 设置为项目面板。
解决了。