简体   繁体   English

JScrollPane未出现在JTextArea上

[英]JScrollPane not appearing on JTextArea

I'm trying to add the JScrollPane to my JTextArea, but somehow, it won't appear. 我正在尝试将JScrollPane添加到我的JTextArea中,但是不知何故,它不会出现。 I've tried resizing it according to the dimension of the JTextArea, but it doesn't seem to work. 我尝试根据JTextArea的尺寸调整其大小,但似乎不起作用。 Also, notice that I'm using the null layout because I want the full-on flexibility of displaying certain buttons and panels at a pinpoint location. 另外,请注意,我使用的是null布局,因为我想要在精确位置显示某些按钮和面板的全部灵活性。

import java.awt.Dimension;
import java.awt.BorderLayout;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JButton;
import javax.swing.JScrollPane;
import javax.swing.ScrollPaneConstants;
import javax.swing.JTextArea;

public class PaneTester{

  private static JFrame frame;
  private static JPanel panel;
  private static JScrollPane scrollPane;
  private static JTextArea notificationBox;

  public static void main (String [] args){
    stage1(); 
    stage2();
  }

  private static void stage1(){
    createFrame();
    createPanel();
    frame.getContentPane().add(panel);
    panel.setVisible(true);
    frame.setVisible(true);
  }

  private static void stage2(){
    generateNotificationBox();
  }

  private static void createFrame(){
    frame = new JFrame();
    frame.setSize(new Dimension(900,700)); 
    frame.setExtendedState(JFrame.MAXIMIZED_BOTH); 
    frame.setLocationRelativeTo(null);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setResizable(true);
  }

  private static void createPanel(){
    panel = new JPanel();
    panel.setLayout(null);
    generateGridButtons();
  }

  private static void generateGridButtons(){
    short y = 0; 
    for(short i=0;i<4;i++){
      y += 60;
      short x = 500;
      for(short j=0;j<5;j++){
        JButton gridButton = new JButton();
        gridButton.setBounds(x, y,120,60);
        panel.add(gridButton);
        x += 140;
      }
    }    
  }
  public static void generateNotificationBox(){
    notificationBox = new JTextArea(10,10);
    notificationBox.setBounds(25, 25, 200, 400);
    scrollPane = new JScrollPane(notificationBox, ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS,
                                 ScrollPaneConstants.HORIZONTAL_SCROLLBAR_ALWAYS ); 

    Dimension d = new Dimension(notificationBox.getPreferredSize());
    scrollPane.getViewport().setPreferredSize(d);
    scrollPane.getViewport().add(notificationBox);
    panel.add(notificationBox);
    panel.repaint();
  }

}

Stop mucking with setBounds and setPreferredSize , you're just making live more difficult for your self. 停止使用setBoundssetPreferredSize ,只会使自己的生活更加困难。

If you want to affect the size of JTextArea (and the viewable area of the JScrollPane ) have a look at the JTextArea constructor JTextArea(int rows, int columns ) , which will allow you to specify the number of rows/columns you want the JTextArea to default to, and which will allow the JTextArea to calculate it's preferredSize based on the current font's metrics in more stable cross platform way 如果要影响JTextArea的大小(以及JScrollPane的可视区域),请查看JTextArea构造函数JTextArea(int rows, int columns ,这将使您可以指定想要JTextArea的行数/列数默认为,这将允许JTextArea以更稳定的跨平台方式基于当前字体的度量来计算它的preferredSize

Your core problem, however, is right here... 但是,您的核心问题就在这里...

scrollPane.getViewport().add(notificationBox);
panel.add(notificationBox);

You add the notificationBox to the JScrollPane s JViewport , which is good, but then you add notificationBox to the panel , which will remove it from the JScrollPane 's JViewport , which is bad 您将notificationBox添加到JScrollPaneJViewport ,这很好,但是随后将notificationBox添加到panel ,这会将其从JScrollPaneJViewport删除,这很糟糕。

Instead, add the JScrollPane to the panel 相反,将JScrollPane添加到panel

scrollPane.getViewport().add(notificationBox);
panel.add(scrollPane);

You're also making overuse of static . 您还会过度使用static I'd highly recommend you take the time to reduce static down to it's absolute minimum required usage, this will probably mean that rather then constructing the UI in the main method, you have a "main" class which you can insatiate (from main ) which will perform the initial setup - IMHO 我强烈建议您花些时间将static降低到绝对最低的使用量,这可能意味着您不应该在main方法中构造UI,而是要拥有一个“ main”类(可以满足ins)(来自main )将执行初始设置-IMHO

I've tried that. 我已经尝试过了。 I think someone else suggested that from another post, but when I tried that, it just took away the JTextArea completely from the panel 我认为其他人从另一篇文章中建议了这一点,但是当我尝试这样做时,它只是将JTextArea从面板中完全删除了。

Get rid of panel.setLayout(null); 摆脱panel.setLayout(null); and start making use of appropriate layout managers and compound layouts. 并开始使用适当的布局管理器和复合布局。 Start by having look at Laying Out Components Within a Container for more details 首先查看放置容器中的组件以了解更多详细信息

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

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