简体   繁体   English

使用null LayoutManager时,为什么我的JScrollPane与JTextArea不可见?

[英]Why isn't my JScrollPane with a JTextArea visible when using null LayoutManager?

I'm trying to display a JTextArea within a JScrollPane, but I just get an empty frame when I run my (simplified) program: 我正在尝试在JScrollPane中显示一个JTextArea,但是当我运行我的(简化)程序时,我只得到一个空框架:

import java.awt.Container;
import java.awt.Dimension;    
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;

public class ScrollPaneTest extends JFrame {
    private Container myCP; 
    private JTextArea resultsTA;
    private JScrollPane scrollPane;

    public ScrollPaneTest() {
        setSize(500, 500);
        setLocation(100, 100);
        myCP = this.getContentPane();
        myCP.setLayout(null);

        resultsTA = new JTextArea("Blah blah");
        scrollPane = new JScrollPane(resultsTA,
                JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,
                JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
        scrollPane.setPreferredSize(new Dimension(200, 100));
        scrollPane.setLocation(100, 300);
        myCP.add(scrollPane);

        setVisible(true);
        addWindowListener(new WindowAdapter() {
            public void windowClosing(WindowEvent e) {
                System.exit(0);
            }
        });
    }

    public static void main(String[] args) {
        new ScrollPaneTest();
    }
}

I use the null LayoutManager to be consistent with the textbook I'm teaching from. 我使用null LayoutManager与我教的教科书一致。

This will work: 这将有效:

public class ScrollPaneTest extends JFrame {
    private Container myCP; 
    private JTextArea resultsTA;
    private JScrollPane scrollPane;

    public ScrollPaneTest() {
        setSize(500, 500);
        setLocation(100, 100);
        myCP = this.getContentPane();
        myCP.setLayout(null);

        resultsTA = new JTextArea("Blah blah");
        resultsTA.setBounds(10, 10, 150, 30);

        scrollPane = new JScrollPane(resultsTA,JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
        scrollPane.setPreferredSize(new Dimension(200, 100));
        scrollPane.setBounds(0, 0, 500, 500);

        myCP.add(scrollPane);
        setVisible(true);
        addWindowListener(new WindowAdapter() {
            public void windowClosing(WindowEvent e) {
                System.exit(0);
            }
        });
    }

    public static void main(String[] args) {
        new ScrollPaneTest();
    }
}

If you are using null layout then you must specify bounds. 如果使用null布局,则必须指定边界。


Edit 编辑

setBounds() method coveres the task of setLocation() method. setBounds()方法包含了setLocation()方法的任务。

for example, setBounds(x,y,w,h); 例如, setBounds(x,y,w,h);

first 2 will set the x/y location of that component with respect to its container. 第一个将设置该组件相对于其容器的x / y位置。 second 2(w/h) will set the size of that component. 第二个2(w / h)将设置该组件的大小。

in other words:- 换一种说法:-

  1. setBounds(int x, int y, int witdh, int height) – Sets the component's size and location setBounds(int x,int y,int witdh,int height) - 设置组件的大小和位置
  2. setLocation(int x, int y) – Sets the component's location setLocation(int x,int y) - 设置组件的位置

I have to agree with kleopatra's comment on this one. 我必须同意kleopatra对此的评论。

Here is a variant of Harry Joy's code that uses layouts. 以下是使用布局的Harry Joy代码的变体。 It appears on-screen almost the same size as the original GUI but is resizable. 它在屏幕上看起来与原始GUI几乎相同,但可以调整大小。 It will also adapt easily to different PLAFs, default font sizes etc. (though it might end up a different size on-screen), whereas something with a null layout will not. 它还可以轻松适应不同的PLAF,默认字体大小等(虽然它可能在屏幕上以不同的大小结束),而具有null布局的东西则不会。

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class ScrollPaneTest extends JFrame {
    private Container myCP;
    private JTextArea resultsTA;
    private JScrollPane scrollPane;

    public ScrollPaneTest() {
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setLocation(100, 100);
        myCP = this.getContentPane();

        resultsTA = new JTextArea("Blah blah", 28, 43);

        scrollPane = new JScrollPane(resultsTA,JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);

        myCP.add(scrollPane);
        setVisible(true);
        pack();
    }

    public static void main(String[] args) {
        Runnable r = new Runnable() {
            public void run() {
                new ScrollPaneTest();
            }
        };
        SwingUtilities.invokeLater(r);
    }
}

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

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