【问题标题】:Adding both a JPanel and JScrollingPane to a JFrame将 JPanel 和 JScrollingPane 同时添加到 JFrame
【发布时间】:2016-08-04 15:32:40
【问题描述】:

我目前正在为数据库系统的 GUI 制作一个窗口。我试图在同一个 JFrame 中同时拥有 JScrollPane 和 JPanel。我基本上想要一个用户可以滚动浏览数据的部分,然后是底部不滚动的部分,其中有按钮可以更改排序。目前,当我尝试打开窗口时,只显示 JPanel。我知道 JScrollPane 本身可以工作,因为如果我注释掉添加 JPanel 的部分,它就可以正常工作。

public class ViewWindow
{
   DataContainer data;
   JFrame viewWin;
   DaysUntil days;
   JPanel contentPane;
   JScrollPane scroll;
   EmptyBorder border;
   DateFormat dateformat;
   Integer[] map;
   SortTest sor;
   JButton dayUntil, index, name;

   public ViewWindow(DataContainer da)
   {
       data= da;
       days= new DaysUntil();
       sor= new SortTest(data,days);
       viewWin= createWindow();
       JScrollPane main= createMainPanel();
       JPanel sortPane= createSortPanel();
       viewWin.getContentPane().add(main);
       viewWin.getContentPane().add(sortPane);
       viewWin.pack();
       viewWin.setVisible(false);

  }

这里是createMainPanel()的部分代码

public JScrollPane createMainPanel()
   {
      DateFormat dateformat =new SimpleDateFormat("MM/dd/yy");
      border = new EmptyBorder(10,20,10,20);
      JPanel mainPane= new JPanel();
      mainPane.setLayout(new BoxLayout(mainPane, BoxLayout.Y_AXIS));

      JPanel titlePane= new JPanel();
      titlePane.setLayout(new BoxLayout(titlePane, BoxLayout.Y_AXIS));

      contentPane= new JPanel();
      contentPane.setLayout(new BoxLayout(contentPane, BoxLayout.X_AXIS));

      JPanel indexPane= new JPanel();
         indexPane.setLayout(new BoxLayout(indexPane, BoxLayout.Y_AXIS));
         JLabel indexTitle= new JLabel("index");
         indexTitle.setBorder(border);
         indexPane.add(indexTitle);

       .....
       mainPane.add(titlePane);
  mainPane.add(contentPane);
  JScrollPane mainn= new JScrollPane(mainPane, JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
  return mainn;
 }

JPanel 如下:

public JPanel createSortPanel()
   {
      JPanel sortPanel = new JPanel();
      sortPanel.setLayout(new BoxLayout(sortPanel, BoxLayout.X_AXIS));

      dayUntil= new JButton("Sort by Due Date");
      index= new JButton("Sort by index");
      name= new JButton("Sort by name");
      sortPanel.add(dayUntil);
      sortPanel.add(index);
      sortPanel.add(name);
      return sortPanel;
   }

如果有人能提供帮助,将不胜感激。

【问题讨论】:

    标签: java swing jframe jpanel jscrollpane


    【解决方案1】:

    JFame 的默认布局是支持此要求的 BorderLayout:

    基本逻辑是:

    frame.add(scrollPane, BorderLayout.CENTER)
    frame.add(anotherPanel, BorderLayout.PAGE_END);
    

    阅读 Layout Manager 上的 Swing 教程部分,了解工作示例,以更好地帮助您了解 BorderLayout 的工作原理。

    【讨论】:

    • 如果我将默认布局切换为类似于盒子布局的布局,它会起作用吗?
    • @J.Shupperd,您可以做任何您想做的事情,但对于您描述的要求,我强烈建议无需从默认的 BorderLayout 更改框架布局的内容窗格的布局。 BorderLayout 将比 BoxLayout 更好地处理框架的大小调整。您可以在子面板上使用不同的布局管理器。阅读教程,下载示例并与他们一起玩,以便了解每种布局的工作原理。
    • @J.Shupperd 稍微扩展一下 camickr 的答案:在 BorderLayout 中,中心“窗格”随着 JFrame 的双向拉伸而拉伸。 'page_end' 'pane' 仅水平延伸。这通常是想要的,并且符合您的描述。使用不同的布局管理器是个好主意。
    猜你喜欢
    • 2013-08-08
    • 1970-01-01
    • 1970-01-01
    • 2012-08-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-01-27
    相关资源
    最近更新 更多