Java 将 JLabel 添加到 ScrollPane 中嵌入的面板
Java adding JLabels to Panel embedded in ScrollPane
JScrollPane scrollPane = new JScrollPane(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS, ScrollPaneConstants.HORIZONTAL_SCROLLBAR_ALWAYS);
scrollPane.setBounds(23, 11, 603, 123);
contentPane.add(scrollPane);
JPanel panel = new JPanel();
JLabel[] labels=new JLabel[callout.getRedCount()]; //callout.getRedCount() = 8
panel.setLayout(null);
int x = 50;
int y = 50;
for (int i=0;i<callout.getRedCount();i++){ //callout.getRedCount() = 8
labels[i]=new JLabel("Red" + i);
labels[i].setBounds(x, y, 32, 32);
panel.add(labels[i]);
x += 150;
}
scrollPane.setViewportView(panel);
我正在尝试将这些标签添加到嵌入 scrollPane
的面板中。标签显示正确,但最后几个标签超过面板大小时除外。 scrollPane
不滚动。我尝试了 .setLayout
各种不同的布局,但仍然没有结果。我是否缺少有关如何执行此操作的信息?
好的,我使用 GridBagLayout 完成了这项工作。
public static void main(String args[]){
JFrame contentPane = new JFrame();
JScrollPane scrollPane = new JScrollPane(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS, ScrollPaneConstants.HORIZONTAL_SCROLLBAR_ALWAYS);
scrollPane.setBounds(23, 11, 50, 50);
contentPane.add(scrollPane);
JPanel panel = new JPanel();
JLabel[] labels=new JLabel[8]; //callout.getRedCount() = 8
panel.setLayout(new GridBagLayout());
GridBagConstraints gbc = new GridBagConstraints();
gbc.insets = new Insets(0,10,0,0);
int x = 50;
int y = 50;
for (int i=0;i<8;i++){ //callout.getRedCount() = 8
labels[i]=new JLabel("Red" + i);
gbc.gridx = i;
panel.add(labels[i], gbc);
}
scrollPane.setViewportView(panel);
contentPane.setSize(50,50);
contentPane.setVisible(true);
}
您可以更改这些值,这非常粗糙。希望这对您有所帮助!
JScrollPane scrollPane = new JScrollPane(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS, ScrollPaneConstants.HORIZONTAL_SCROLLBAR_ALWAYS);
scrollPane.setBounds(23, 11, 603, 123);
contentPane.add(scrollPane);
JPanel panel = new JPanel();
JLabel[] labels=new JLabel[callout.getRedCount()]; //callout.getRedCount() = 8
panel.setLayout(null);
int x = 50;
int y = 50;
for (int i=0;i<callout.getRedCount();i++){ //callout.getRedCount() = 8
labels[i]=new JLabel("Red" + i);
labels[i].setBounds(x, y, 32, 32);
panel.add(labels[i]);
x += 150;
}
scrollPane.setViewportView(panel);
我正在尝试将这些标签添加到嵌入 scrollPane
的面板中。标签显示正确,但最后几个标签超过面板大小时除外。 scrollPane
不滚动。我尝试了 .setLayout
各种不同的布局,但仍然没有结果。我是否缺少有关如何执行此操作的信息?
好的,我使用 GridBagLayout 完成了这项工作。
public static void main(String args[]){
JFrame contentPane = new JFrame();
JScrollPane scrollPane = new JScrollPane(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS, ScrollPaneConstants.HORIZONTAL_SCROLLBAR_ALWAYS);
scrollPane.setBounds(23, 11, 50, 50);
contentPane.add(scrollPane);
JPanel panel = new JPanel();
JLabel[] labels=new JLabel[8]; //callout.getRedCount() = 8
panel.setLayout(new GridBagLayout());
GridBagConstraints gbc = new GridBagConstraints();
gbc.insets = new Insets(0,10,0,0);
int x = 50;
int y = 50;
for (int i=0;i<8;i++){ //callout.getRedCount() = 8
labels[i]=new JLabel("Red" + i);
gbc.gridx = i;
panel.add(labels[i], gbc);
}
scrollPane.setViewportView(panel);
contentPane.setSize(50,50);
contentPane.setVisible(true);
}
您可以更改这些值,这非常粗糙。希望这对您有所帮助!