2012-05-29 36 views
9

我有这个代码,我试图适合一个可滚动面板(JPanel),但我没有得到它。这里是我的代码:如何在Java中使JFrame可滚动?

public class Sniffer_GUI extends JFrame { 
Canvas c = new Canvas(); 
ConnectorPropertiesPanel props; 
public Sniffer_GUI() { 
    super("JConnector demo"); 
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 

    getContentPane().setLayout(new GridBagLayout()); 
    init(); 

    getContentPane().add(new JLabel("Connectors example. You can drag the connected component to see how the line will be changed"), 
         new GridBagConstraints(0, 0, 2, 1, 1, 0, GridBagConstraints.NORTHWEST, GridBagConstraints.HORIZONTAL, new Insets(5, 5, 0, 5), 0, 0)); 
    getContentPane().add(initConnectors(), 
         new GridBagConstraints(0, 1, 1, 1, 1, 1, GridBagConstraints.NORTHWEST, GridBagConstraints.BOTH, new Insets(5, 5, 5, 5), 0, 0)); 
    getContentPane().add(props, 
         new GridBagConstraints(1, 1, 1, 1, 0, 1, GridBagConstraints.NORTHWEST, GridBagConstraints.VERTICAL, new Insets(5, 0, 5, 5), 0, 0)); 
    setSize(800, 600); 
    setLocationRelativeTo(null); 

} 

在此先感谢。

我编辑补充说,部分似乎工作代码...

public Sniffer_GUI() { 
    super("JConnector demo"); 
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
    JPanel container = new JPanel(); 
    JScrollPane scrPane = new JScrollPane(container); 
    add(scrPane); 
    scrPane.setLayout(new ScrollPaneLayout()); 
    init(); 

    add(initConnectors()); 

    setSize(800, 600); 
    setLocationRelativeTo(null); 

} 

但它仍然不是滚动,至少它使得它的一个JScrollPane内的功能,是一个很好的一步。

+0

哪里是你的代码,试图使用JScrollPane的? – Puce

+0

@Puce也许他不知道JScrollPane。 – brimborium

+0

我不知道声明JScrollPane变量...... =( –

回答

0

试试这个:

JScrollPane sp = new JScrollPane(); 
this.add(sp). 
sp.add(*GUI elements for your applications.*) 

类似的东西应该为你工作。看看this还有:

+5

向滚动窗格添加元素通常不是您想要的。通常情况下,您将容器面板传递给JScrollPane的构造函数,或者使用'setViewportView' –

19

做一个JPanel滚动,并用它作为一个容器,是这样的:

JPanel container = new JPanel(); 
JScrollPane scrPane = new JScrollPane(container); 
add(scrPane); // similar to getContentPane().add(scrPane); 
// Now, you can add whatever you want to the container 
+0

当我尝试添加元素时,发现scrPane的布局问题,我该如何处理与原始代码类似的东西? –

+2

@JoeLewis而不是做你对内容窗格所做的事情,在Eng.Fouad的答案中描述的容器上执行它,并简单地将滚动窗格添加到内容窗格中(保留默认布局BorderLayout作为内容窗格的布局管理器) –

+0

@Guillaume Polet ok完美Eng.Fouad的代码部分工作,因为我在原始文章中编辑,它的工作原理,但它仍然不可滚动... –

6

延长@ Eng.Fouad答案:

public class Sniffer_GUI extends JFrame { 
    Canvas c = new Canvas(); 
    ConnectorPropertiesPanel props; 
    public Sniffer_GUI() { 
     super("JConnector demo"); 
     setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     JPanel container = new JPanel(); 
     JScrollPane scrPane = new JScrollPane(container); 
     getContentPane().add(scrPane); 
     container.setLayout(new GridBagLayout()); 
     init(); 

     container.add(new JLabel("Connectors example. You can drag the connected component to see how the line will be changed"), 
          new GridBagConstraints(0, 0, 2, 1, 1, 0, GridBagConstraints.NORTHWEST, GridBagConstraints.HORIZONTAL, new Insets(5, 5, 0, 5), 0, 0)); 
     container.add(initConnectors(), 
          new GridBagConstraints(0, 1, 1, 1, 1, 1, GridBagConstraints.NORTHWEST, GridBagConstraints.BOTH, new Insets(5, 5, 5, 5), 0, 0)); 
     container .add(props, 
          new GridBagConstraints(1, 1, 1, 1, 0, 1, GridBagConstraints.NORTHWEST, GridBagConstraints.VERTICAL, new Insets(5, 0, 5, 5), 0, 0)); 
     setSize(800, 600); 
     setLocationRelativeTo(null); 

    } 
} 
+0

我的英雄... xD现在我只需要使它可以垂直滚动,因为它只是水平的...:P –

+0

@JoeLewis你可以设置垂直和水平滚动条策略来调整这些东西。考虑阅读[ScrollPane教程](http://java.sun.com/docs/books/tutorial/uiswing/components/scrollpane.html)及其[JavaDoc](http://docs.oracle.com/javase /7/docs/api/javax/swing/JScrollPane.html) –

+0

我的问题是,iniConnectors()检索一个JPanel,并且我需要使该Panel可以滚动,因为它是最重要的,但是我甚至不能做到这一点将initConnector添加到另一个JScrollPane,然后执行container.add(“新的JScrollPane”等)。 –

1

要创建JFrame可滚动组件,将组件包装在JScrollPane中:

JScrollPane myJScrollPane = new JScrollPane(myJLabel, 
     JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED, 
     JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED); 

并取代menti与myJScrollPane一起使用myJLabel。为我工作。

+0

谢谢你在JScrollPane的构造函数中显示JScrollPane.VERTICAL_SCROLLBAR_​​AS_NEEDED和JScrollPane.HORIZONTAL_SCROLLBAR_​​AS_NEEDED。在我的[answer](http://stackoverflow.com/a/32872829/4744263)我使用setVerticalScrollBarPolicy和setHorizo​​ntalScrollBarPolicy方法。谢谢你的可能性。 –

1

只是在进行中,并随时纠正我,如果我离开基地,但使用这样的JScrollPane意味着需要更频繁的窗口调整大小的意外结果。

例如,我有一个程序,我在那里设置了一个JFrame范围的scrollpane。我在框架中的选项卡中也有一个JTextArea,它与内容窗格的大小相同。这个textArea也在它自己的scrollpane中(这比其他任何东西都更像是一个搞砸项目)。当我从文件加载内容到textArea中时,它会触发文本区域周围的滚动条。

结果是我的,我们称之为innerScrollPane,现在比JFrame更大,因为之前没有可见的滚动条。然后触发我现在要调用的outerScrollPane,显示它的滚动条,然后覆盖内部滚动条。

这很容易通过在我的文件打开方法的末尾添加一个额外的window.pack()参数来解决,但我只是想把它扔到那里。如果您不小心,滚动条可能会掩盖窗口中的内容。但是......好吧,有一百万种方法来防止这个问题,所以这不是一个大问题。只是要注意的事情。

6

也许这将帮助...

JFrame frame = new JFrame(); 
JPanel panel = new JPanel(); 

// add something to you panel... 
// panel.add(...); 

// add the panel to a JScrollPane 
JScrollPane jScrollPane = new JScrollPane(panel); 
// only a configuration to the jScrollPane... 
jScrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS); 
jScrollPane.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED); 

// Then, add the jScrollPane to your frame 
frame.getContentPane().add(jScrollPane);