Java 图形用户界面JTextArea&;JScrollPane&;定位

Java 图形用户界面JTextArea&;JScrollPane&;定位,java,swing,layout-manager,Java,Swing,Layout Manager,我想添加到从x,y位置开始的带有滚动条的窗口JTextArea。 问题是,当我对代码进行可视化时,它确实会填充500x500窗口,并且我只希望在x,y位置填充200x200个完整窗口 我做错了什么?这是我的密码: public static void main(String args[]) { JFrame window = new JFrame(); window.setSize(500, 500); window.setResizable(false); J

我想添加到从x,y位置开始的带有滚动条的窗口
JTextArea
。 问题是,当我对代码进行可视化时,它确实会填充500x500窗口,并且我只希望在x,y位置填充200x200个完整窗口

我做错了什么?这是我的密码:

public static void main(String args[])
{
    JFrame window = new JFrame();
    window.setSize(500, 500);
    window.setResizable(false);

    JTextArea ta = new JTextArea("Default message");
    ta.setEditable(true);

    JScrollPane scroll = new JScrollPane(ta);
    scroll.setBounds(10,10,200,200);
    window.add(scroll);

    window.setVisible(true);
}
不要使用setBounds()设置组件的大小。Swing使用布局管理器调整组件的大小

默认情况下,JFrame使用BorderLayout,您要将组件添加到中心,以便组件占用所有可用空间

相反,你可以这样做:

JTextArea text Area = new JTextArea(5, 40);
JScrollPane scrollPane = new JScrollPane( textArea );
frame.add(scrollPane, BorderLayout.NORTH);
frame.pack();
frame.setVisible();
当您显示框架时,文本区域将占据整个空间,但如果将框架调整得更大,则文本区域将仅显示在北部,中间将有空白。

不要使用setBounds()设置组件的大小。Swing使用布局管理器调整组件的大小

import java.awt.*;
import javax.swing.*;
import javax.swing.border.EmptyBorder;

public class PaddedTextArea {

    public static void main(String[] args) {
        Runnable r = new Runnable() {
            @Override
            public void run() {
                // the GUI as seen by the user (without frame)
                JPanel gui = new JPanel(new BorderLayout());
                // adjust as needed
                gui.setBorder(new EmptyBorder(20,10,20,10));

                // better way to size a text area is using columns/rows 
                // in the constructor
                JTextArea ta = new JTextArea(3,40);
                JScrollPane sp = new JScrollPane(ta);
                gui.add(sp);

                JFrame f = new JFrame("Padded Text Area");
                f.add(gui);
                // Ensures JVM closes after frame(s) closed and
                // all non-daemon threads are finished
                f.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
                // See https://stackoverflow.com/a/7143398/418556 for demo.
                f.setLocationByPlatform(true);

                // ensures the frame is the minimum size it needs to be
                // in order display the components within it
                f.pack();
                // should be done last, to avoid flickering, moving,
                // resizing artifacts.
                f.setVisible(true);
            }
        };
        // Swing GUIs should be created and updated on the EDT
        // http://docs.oracle.com/javase/tutorial/uiswing/concurrency
        SwingUtilities.invokeLater(r);
    }
}
默认情况下,JFrame使用BorderLayout,您要将组件添加到中心,以便组件占用所有可用空间

相反,你可以这样做:

JTextArea text Area = new JTextArea(5, 40);
JScrollPane scrollPane = new JScrollPane( textArea );
frame.add(scrollPane, BorderLayout.NORTH);
frame.pack();
frame.setVisible();
当您显示框架时,文本区域将占据整个空间,但如果您将框架调整得更大,则文本区域将仅显示在北部,中间将有空白。

import java.awt.*;
import javax.swing.*;
import javax.swing.border.EmptyBorder;

public class PaddedTextArea {

    public static void main(String[] args) {
        Runnable r = new Runnable() {
            @Override
            public void run() {
                // the GUI as seen by the user (without frame)
                JPanel gui = new JPanel(new BorderLayout());
                // adjust as needed
                gui.setBorder(new EmptyBorder(20,10,20,10));

                // better way to size a text area is using columns/rows 
                // in the constructor
                JTextArea ta = new JTextArea(3,40);
                JScrollPane sp = new JScrollPane(ta);
                gui.add(sp);

                JFrame f = new JFrame("Padded Text Area");
                f.add(gui);
                // Ensures JVM closes after frame(s) closed and
                // all non-daemon threads are finished
                f.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
                // See https://stackoverflow.com/a/7143398/418556 for demo.
                f.setLocationByPlatform(true);

                // ensures the frame is the minimum size it needs to be
                // in order display the components within it
                f.pack();
                // should be done last, to avoid flickering, moving,
                // resizing artifacts.
                f.setVisible(true);
            }
        };
        // Swing GUIs should be created and updated on the EDT
        // http://docs.oracle.com/javase/tutorial/uiswing/concurrency
        SwingUtilities.invokeLater(r);
    }
}
更一般地说:javagui可能必须在多种平台上工作,使用不同的屏幕分辨率&使用不同的plaf。因此,它们不利于部件的精确放置。要为健壮的GUI组织组件,请使用布局管理器或1,以及布局填充和边框2

  • 更一般地说:javagui可能必须在多种平台上工作,使用不同的屏幕分辨率&使用不同的plaf。因此,它们不利于部件的精确放置。要为健壮的GUI组织组件,请使用布局管理器或1,以及布局填充和边框2


  • 同时考虑使用 EntEdTrime来影响组件的位置和大小,也考虑使用 EntPieldEng/<代码>来影响组件的位置和大小。components@AndrewThompson,对于一个健壮的GUI,最合理的做法是使用布局管理器?没有其他选择吗?@AndrewThompson,对于一个健壮的GUI,最合理的做法是使用布局管理器?没有其他选择吗?