JScrollPane用于多个标签

时间:2012-02-20 16:28:03

标签: java swing jscrollpane jlabel

我正在开发一个具有流程布局的程序,里面是一组标签,因为有很多它们都没有显示。是否有添加滚动窗格水平滚动所有这些标签?

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));

2 个答案:

答案 0 :(得分:3)

我建议在Column或{{Row中使用JListJTableJListJTable(取决于或指示)对象1}}默认为JLabel/JComponent

答案 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);
    }
}