【问题标题】:JLabels in a JScrollPaneJScrollPane 中的 JLabels
【发布时间】:2016-01-06 06:06:23
【问题描述】:

我有一个滚动窗格,我想在其中添加多个 jlabel。 这是代码..

JPanel panelEast = new JPanel();
    panelEast.setBorder(new TitledBorder(null, "Notifiche", TitledBorder.LEADING, TitledBorder.TOP, null, null));
    panelEast.setPreferredSize(new Dimension(250,120));
    panelEast.setLayout(null);

    JLabel lblNewLabel_3 = new JLabel("label1");
    lblNewLabel_3.setIcon(new ImageIcon(Home.class.getResource("/it/polimi/icon/contact.png")));
    lblNewLabel_3.setBounds(10, 81, 240, 52);
    panelEast.add(lblNewLabel_3);

    JLabel label_3 = new JLabel("label2");
    label_3.setIcon(new ImageIcon(Home.class.getResource("/it/polimi/icon/verified.png")));
    label_3.setBounds(new Rectangle(4, 0, 0, 0));
    label_3.setAlignmentY(Component.TOP_ALIGNMENT);
    label_3.setBounds(10, 30, 240, 52);
    panelEast.add(label_3);

    JLabel label_4 = new JLabel("label3");
    label_4.setIcon(new ImageIcon(Home.class.getResource("/it/polimi/icon/verified.png")));
    label_4.setBounds(new Rectangle(4, 0, 0, 0));
    label_4.setAlignmentY(Component.TOP_ALIGNMENT);
    label_4.setBounds(10, 131, 240, 52);
    panelEast.add(label_4);

    JScrollPane scrollPane = new JScrollPane(panelEast,JScrollPane.VERTICAL_SCROLLBAR_NEVER,JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
    pan2.add(scrollPane,BorderLayout.CENTER);

而且不起作用,有什么建议吗?

【问题讨论】:

    标签: java swing


    【解决方案1】:

    避免使用null 布局,像素完美的布局是现代用户界面设计中的一种错觉。影响组件单个尺寸的因素太多,您无法控制。 Swing 旨在与核心的布局管理器一起工作,丢弃这些将导致无穷无尽的问题和问题,您将花费越来越多的时间来尝试纠正

    JScrollPane 依赖于布局管理器 API 来计算它所显示的容器的首选大小,并确定该视图何时大于自身并显示滚动条

    【讨论】:

    • 由于您实际上没有描述问题,我只能猜测滚动条没有在您期望的时候显示,并且由于 Swing API 依赖于布局管理器 API 来做,好吧基本上,一切,null 布局是问题的第一个重要迹象。此外,脱离上下文的代码 sn-p 使得很难“猜测”问题的根源并就适当的解决方案提出建议
    • (1+) @Fr4ncx, but it does not solve the problem - 是的,使用 Layout Managers 将解决问题。摆脱空布局并摆脱setBounds(...)。如果您仍有问题,请发布使用布局管理器的更新代码。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-04-18
    • 2018-01-12
    • 2015-06-11
    • 1970-01-01
    • 2012-02-02
    • 1970-01-01
    • 2017-02-03
    相关资源
    最近更新 更多