JScrollPane 无法正常工作 [英] JScrollPane doesn't work properly

查看:27
本文介绍了JScrollPane 无法正常工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

先生充满鳗鱼的气垫船

我几乎按照你说的做了.我的道歉它仍然没有显示:(

I almost did what you said. MY APOLOGY it is still not showing :(

private void initialize() {
    frame = new JFrame();
    frame.setBounds(100, 100, 760, 666);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    //frame.getContentPane().setLayout(null);
    //frame.getContentPane().setLayout();

    textArea = new JTextArea(20,20);
    textArea.setPreferredSize(new Dimension(100, 100));
    textArea.setLineWrap(true);
    JScrollPane sp = new JScrollPane(textArea, JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
    frame.getContentPane().add(sp);
    frame.setVisible(true);

}

我正在关注 YouTube 上的教程,在 Eclipse 中使用 WindowBuilderPro.我想使用 JScollPane 创建(可滚动)JTextArea.尽管我遵循了原则,但它不起作用!这件小事占用了我的时间,所以我决定问问你们:(亲切,有人能告诉我为什么 JTextArea 没有显示在 JFrame 中.

I'm following a tutorial on YouTube using WindowBuilderPro in Eclipse. I want to create (scrollable) JTextArea using JScollPane. Even though I followed the principles, it didn't work! This little thing consumes my time so I decided to ask you guys :( KINDLY, can someone tell me why JTextArea isn't showing in JFrame .

public class ProjectGUI {

private JFrame frame;
private JTextField urls;
private JTextArea textArea;

/**
 * Launch the application.
 */
public static void main(String[] args) {
    EventQueue.invokeLater(new Runnable() {
        public void run() {
            try {
                ProjectGUI window = new ProjectGUI();
                window.frame.setVisible(true);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    });
}

/**
 * Create the application.
 */
public ProjectGUI() {
    initialize();
}

/**
 * Initialize the contents of the frame.
 */
private void initialize() {
    frame = new JFrame();
    frame.setBounds(100, 100, 760, 666);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.getContentPane().setLayout(null);

    JPanel panel = new JPanel();
    panel.setBorder(new TitledBorder(null, "X.509 Extractor", TitledBorder.LEADING, TitledBorder.TOP, null, null));
    panel.setBounds(18, 16, 722, 145);
    panel.setLayout(null);
    frame.getContentPane().add(panel);

    urls = new JTextField();
    urls.setText("For multiple URLs, separate them by comma ,");
    urls.setForeground(Color.BLUE);
    urls.setBounds(59, 57, 646, 28);
    panel.add(urls);

    JButton button1 = new JButton("EXTRACT");
    button1.setBounds(303, 97, 117, 40);
    frame.setLocationRelativeTo(null);
    panel.add(button1);

    textArea = new JTextArea();
    textArea.setBounds(18, 212, 722, 414);
    textArea.setLineWrap(true);
    JScrollPane sp= new JScrollPane(textArea);
    frame.getContentPane().add(sp);
    frame.setVisible(true);

}

}

推荐答案

您正在设置 JTextArea 的边界,这是您永远不应该做的事情,这会阻止它在 JScrollPane 中增长.

You're setting bounds of your JTextArea, something you should never do, and this is preventing it from growing within the JScrollPane.

您的 JTextArea 未显示,因为您使用的是空布局并添加了一个未指定其大小或位置的组件,即 JScrollPane.

Your JTextArea isn't showing because you're using a null layout and add a component without specifying its size or position, the JScrollPane.

建议:

  • 不要使用空布局和setBounds
  • 相反,使用布局管理器使用 JPanel 的适当组合
  • 切勿设置 JTextArea 的大小或边界.
  • 是的,设置它的行和列属性,没问题.

编辑
关于你的最新帖子,让我补充一下,只是为了清楚:

Edit
Regarding your latest post, let me add, just to be clear:

  • 不要设置 JTextArea 的首选大小
  • 不要设置 JTextAera 的任何大小.

当 JTextArea 内的文本大于 JScrollPane 视口的边界时,您将看到滚动条.例如:

You will see scroll bars when the text inside of the JTextArea is larger than the bounds of the JScrollPane's viewport. For example:

import java.util.Random;
import javax.swing.JOptionPane;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;

public class JTextAreaTest {
   public static void main(String[] args) {
      Random random = new Random();
      StringBuilder sb = new StringBuilder();
      for (int i = 0; i < 100; i++) {
         for (int j = 0; j < 20; j++) {
            for (int k = 0; k < random.nextInt(5) + 5; k++) {
               char c = (char) ('a' + random.nextInt(26));
               sb.append(c);               
            }
            sb.append(' ');
         }
         sb.append('\n');               
      }

      JTextArea textArea = new JTextArea(15, 40);
      textArea.setText(sb.toString());
      JScrollPane scrollpane = new JScrollPane(textArea);

      JOptionPane.showMessageDialog(null, scrollpane);
   }
}

这篇关于JScrollPane 无法正常工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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