【问题标题】:Java: Freezing components on a JFrameJava:冻结 JFrame 上的组件
【发布时间】:2013-02-03 23:29:18
【问题描述】:

潜伏已久,第二次发帖(就是这么绝望)

我有一个框架,需要将十二个组件彼此相邻显示(就好像每个组件都是它自己的列一样)。第一个组件是属性名称列表,第二个组件显示默认属性,接下来的十个都是从数据库中提取的。我希望我的滚动条能够有效地“冻结”前两个组件(即它始终显示前两个组件),并且滚动条允许您查看其余条目。这类似于我们冻结列的 Excel,例如 here

我研究过使用表格,但问题是我的组件不仅仅是文本。他们也有图片之类的。

这是我正在使用的一些代码

    JPanel info = new JPanel(); // this is the main component (holds the other 12)
    info.setLayout(new GridBagLayout()); // GridBag Layoout
    info.add(attNames); // add in the attribute names component
    info.add(currentCase); // add in the default values

    JPanel rets = new JPanel(); // add in the ten retrieved cases
    rets.setLayout(new GridLayout(1,10));
    for (int i=0;i<maxCases;i++)
    {
        rets.add(retCase[i]);
    }

    info.add(rets); // add the return cases to the info component
    JScrollPane scrollBar2=new JScrollPane(rets,JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED,JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);   // surround the return cases with a scroll bar
    info.add(scrollBar2); // add the scrollbar

    //add the info comp to the content pane along with other necessary components

    this.getContentPane().add(casesPanel, BorderLayout.NORTH);
    this.getContentPane().add(info, BorderLayout.CENTER);
    this.getContentPane().add(buttons, BorderLayout.SOUTH);

    //finally, add the overall scrollbars and set the size
    this.pack();
    JScrollPane scrollBar=new JScrollPane(info,JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED,JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);  
    this.setSize(this.getWidth(), 750);
    this.setResizable(true);
    this.add(scrollBar);

问题是返回案例的滚动条认为不需要它,我必须使用大滚动条,这意味着我可以远离前两个组件。

任何帮助将不胜感激!

问题来了

干杯, 克

【问题讨论】:

  • FYI Tables 支持图像(开箱即用),可用于生成几乎任何可以呈现为标准组件的内容
  • 将冻结部分放入滚动窗格的行标题中。查看how to use scroll panes 了解更多信息和示例
  • @MadProgrammer,非常感谢!这是一个非常简单而优雅的解决方案。我使用了行标题,效果非常好。
  • 我如何证明这个问题已经解决了?

标签: java swing jpanel jscrollpane


【解决方案1】:

将冻结部分放入滚动窗格的行标题中。查看how to use scroll panes 了解更多信息和示例

【讨论】:

    猜你喜欢
    • 2012-02-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-05-23
    • 1970-01-01
    相关资源
    最近更新 更多