【问题标题】:Why isn't my JScrollPane with a JTextArea visible when using null LayoutManager?为什么在使用 null LayoutManager 时我的带有 JTextArea 的 JScrollPane 不可见?
【发布时间】:2011-07-31 09:18:35
【问题描述】:

我试图在 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();
    }
}

我使用 null LayoutManager 来与我所教的教科书保持一致。

【问题讨论】:

  • 如果您的教科书坚持使用 null LayoutManager 而不是教授手动定位和调整大小一次(并且根据您的经验显示它的工作量很大 :-) 将它扔进垃圾箱 - 它是没用 ;-) 所谓的空布局是不可以的
  • 教材是我系的一位资深成员和好朋友写的。 :-) 我只是为她教过一次课程。

标签: java swing jscrollpane jtextarea null-layout-manager


【解决方案1】:

这将起作用:

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

如果您使用的是空布局,那么您必须指定边界。


编辑

setBounds() 方法涵盖了setLocation() 方法的任务。

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

first 2 将设置该组件相对于其容器的 x/y 位置。 第二个 2(w/h) 将设置该组件的大小。

换句话说:-

  1. setBounds(int x, int y, int witdh, int height) - 设置组件的大小和位置
  2. setLocation(int x, int y) - 设置组件的位置

【讨论】:

  • 问题是你需要指定文本区域的大小和边界,而不仅仅是滚动窗格,如上所述。
  • 有人能解释一下 setBounds() 的含义吗? API 不清楚这与 setLocation() 有何不同。谢谢。
【解决方案2】:

我不得不同意 kleopatra 对此的评论。

这里是使用布局的 Harry Joy 代码的变体。它在屏幕上显示的大小与原始 GUI 几乎相同,但可调整大小。它还可以轻松适应不同的 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);
    }
}

【讨论】:

  • 我不知道 setDefaultCloseOperation() 或 pack(),这两个对我来说都非常有用。谢谢。
猜你喜欢
  • 2012-03-26
  • 1970-01-01
  • 2021-12-03
  • 2014-06-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多