简体   繁体   English

添加JTextArea会使JFrame变得巨大吗?

[英]Adding JTextArea makes JFrame huge why?

Hello I was going along fine with this container by adding the panels one under another. 您好,我在此容器上做得很好,将一个面板添加到另一个面板下。 However when I added the JTextArea it made all of the other panels have much more padding between them. 但是,当我添加JTextArea时,使所有其他面板之间的填充更多。 How do I get rid of this extra space in between each panel? 如何摆脱每个面板之间的多余空间? I tried to set the horizontal and vertical gap to 0 in the GridLayout constructor however, changing those values has no effect unless I make them > 5 and then they get even bigger. 我尝试在GridLayout构造函数中将水平和垂直间隙设置为0,但是更改这些值无效,除非将其设置为> 5,然后它们变得更大。

    setLayout(new GridLayout(5, 1, 0, 0));

    pack();
    setVisible(true); //Make visible       

    textArea = new JTextArea(initialText, 6, 25);
  1. Don't use a GridLayout since this forces every cell to be the same size. 不要使用GridLayout,因为这会强制每个单元格都具有相同的大小。 So if one cell is very large, it will make all the cells very large. 因此,如果一个单元格很大,它将使所有单元格都非常大。
  2. Instead use other layouts or combinations of layouts to make a better more pleasing GUI. 取而代之的是使用其他布局或布局组合来制作更好,更令人愉悦的GUI。
  3. Don't forget to put your JTextArea into a JScrollPane. 不要忘记将JTextArea放入JScrollPane。

For example, a BoxLayout could work, but if you do this, you must recognize that you're actually setting the layout of the contentPane and not the JFrame: 例如,BoxLayout可以工作,但是如果您这样做,则必须认识到您实际上是在设置contentPane而不是JFrame的布局:

//!! setLayout(new GridLayout(5, 1, 0, 0)); // Set BorderLayout
setLayout(new BoxLayout(getContentPane(), BoxLayout.PAGE_AXIS)); // Note getContentPane()

Edit 编辑
You ask: 你问:

Setting it as BoxLayout really fixed the issue however, I am trying to undestand the logic used by getContentPane(), and then the PAGE_AXIS? 将其设置为BoxLayout确实解决了该问题,但是,我试图不理解getContentPane()和PAGE_AXIS使用的逻辑?

BoxLayout is unlike GridLayout, FlowLayout, and BorderLayout in that when you call the constructor, you have to pass in a reference to the component that you're adding the layout to. BoxLayout与GridLayout,FlowLayout和BorderLayout不同,在调用构造函数时,您必须传递对要向其中添加布局的组件的引用。 It looks like you're adding it to the JFrame since the class extends JFrame, and you're calling setLayout(...) on the JFrame itself, but if you pass in this , meaning the current JFrame instance, you'll get a runtime exception, because the setLayout(...) method of the JFrame in fact sets the layout of the JFrame's contentPane. 由于类扩展了JFrame,并且您正在对JFrame本身调用setLayout(...) ,因此您似乎将其添加到JFrame中,但是如果传入this ,则意味着当前的JFrame实例,运行时异常,因为JFrame的setLayout(...)方法实际上设置了JFrame的contentPane的布局。 So to fix this, you need to pass in the JFrame's contentPane. 因此,要解决此问题,您需要传入JFrame的contentPane。 For more on that, please read the tutorial on JFrames. 有关更多信息,请阅读JFrames教程。

For the second part of your question, on why the second parameter to the BoxLayout constructor, BoxLayout.PAGE_AXIS , it's for orienting your layout either horizontally or vertically. 对于你的问题的第二部分,为什么给BoxLayout构造方法的第二个参数, BoxLayout.PAGE_AXIS ,它是水平或垂直定向布局。 For more please read the BoxLayout tutorial and API. 有关更多信息,请阅读BoxLayout教程和API。

This reference will get you to the pertinent Swing tutorials: Swing Info 此参考将带您进入相关的Swing教程: Swing信息

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM