我正在开发一个具有流程布局的程序,里面是一组标签,因为有很多它们都没有显示。是否有添加滚动窗格水平滚动所有这些标签?
JPanel mainpanel = new JPanel();
mainpanel.setLayout(new BoxLayout(mainpanel, BoxLayout.X_AXIS));
pane.add(mainpanel, BorderLayout.NORTH);
JPanel rightpanel = new JPanel();
rightpanel.setLayout(new FlowLayout());
for (int i = 0; i < 100; i++)
{
rightpanel.add(new JLabel("Label " + i));
}
mainpanel.add(new JLabel("Left label"));
mainpanel.add(new JScrollPane(rightpanel));
答案 0 :(得分:3)
答案 1 :(得分:2)
不确定您的问题是什么,因为您已经知道需要使用JScrollPane
。怎么样:
public class ScrollLabels
{
public static void main(String[] args)
{
JFrame frame = new JFrame("Labels");
JPanel mainpanel = new JPanel();
mainpanel.setLayout(new BoxLayout(mainpanel, BoxLayout.X_AXIS));
frame.add(mainpanel);
JPanel rightpanel = new JPanel();
rightpanel.setLayout(new FlowLayout());
for (int i = 0; i < 100; i++)
{
rightpanel.add(new JLabel("Label " + i));
}
mainpanel.add(new JLabel("Left label"));
mainpanel.add(new JScrollPane(rightpanel));
frame.setSize(500, 100);
frame.setVisible(true);
}
}