JScrollPane未出现在JTextArea上 [英] JScrollPane not appearing on JTextArea

查看:95
本文介绍了JScrollPane未出现在JTextArea上的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将JScrollPane添加到我的JTextArea中,但是不知何故,它不会出现. 我尝试根据JTextArea的尺寸调整其大小,但似乎不起作用.另外,请注意,我使用的是null布局,因为我希望在确定的位置上完全灵活地显示某些按钮和面板.

I'm trying to add the JScrollPane to my JTextArea, but somehow, it won't appear. I've tried resizing it according to the dimension of the JTextArea, but it doesn't seem to work. 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.

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();
  }

}

推荐答案

停止使用setBoundssetPreferredSize进行破坏,您的生活更加艰难.

Stop mucking with setBounds and setPreferredSize, you're just making live more difficult for your self.

如果要影响JTextArea的大小(以及JScrollPane的可见区域),请查看JTextArea构造函数

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

但是,您的核心问题就在这里...

Your core problem, however, is right here...

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

您将notificationBox添加到JScrollPaneJViewport中,这很好,但是随后您将notificationBox添加到panel中,这会将其从JScrollPaneJViewport中删除. ,这很糟糕

You add the notificationBox to the JScrollPanes JViewport, which is good, but then you add notificationBox to the panel, which will remove it from the JScrollPane's JViewport, which is bad

相反,将JScrollPane添加到panel

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

您还过度使用了static.我强烈建议您花些时间将static降低到绝对的最低使用量,这可能意味着,而不是在main方法中构造UI,您将拥有一个主"类,可以使该类满足(来自main)将执行初始设置-IMHO

You're also making overuse of 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

我已经尝试过了.我认为其他人从另一篇文章中提出了建议,但是当我尝试这样做时,它只是完全从面板中删除了JTextArea

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

摆脱panel.setLayout(null);,开始使用适当的布局管理器和复合布局.首先查看在容器中布置组件更多详情

Get rid of 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

这篇关于JScrollPane未出现在JTextArea上的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆