Java 杰帕内尔中心区

Java 杰帕内尔中心区,java,swing,jpanel,jtextarea,Java,Swing,Jpanel,Jtextarea,我不敢相信没有答案。。。我只想将JTextArea集中在JPanel中。我正在为此使用BoxLayout。当我运行程序时,JTextArea占据整个屏幕。为什么会这样 public class BLayout extends JFrame implements ActionListener { public BLayout() { super("GUI Testing"); setSize(500, 500); setDefaultClo

我不敢相信没有答案。。。我只想将
JTextArea
集中在
JPanel
中。我正在为此使用
BoxLayout
。当我运行程序时,
JTextArea
占据整个屏幕。为什么会这样

public class BLayout extends JFrame implements ActionListener {

    public BLayout() {
        super("GUI Testing");
        setSize(500, 500);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        JPanel choosePanel = new JPanel();
        choosePanel.setLayout(new BoxLayout(choosePanel, BoxLayout.X_AXIS));
        choosePanel.setBackground(Color.BLUE);

        JTextArea text = new JTextArea(1, 10);
        text.setLineWrap(true);
        text.setEditable(false);
        text.setText("Welcome to Library Search.\n\n"
                + "Choose a command from the \"Commands\""
                + " menu above for adding a reference, "
                + "searching references, or quitting the program.");
        choosePanel.add(text);
        add(choosePanel);
    }
如何使文本区域不占用整个屏幕而坐在面板的中间?

我使用的是BoxLayout。当我运行程序时,JTextArea占据了整个屏幕。为什么会这样

public class BLayout extends JFrame implements ActionListener {

    public BLayout() {
        super("GUI Testing");
        setSize(500, 500);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        JPanel choosePanel = new JPanel();
        choosePanel.setLayout(new BoxLayout(choosePanel, BoxLayout.X_AXIS));
        choosePanel.setBackground(Color.BLUE);

        JTextArea text = new JTextArea(1, 10);
        text.setLineWrap(true);
        text.setEditable(false);
        text.setText("Welcome to Library Search.\n\n"
                + "Choose a command from the \"Commands\""
                + " menu above for adding a reference, "
                + "searching references, or quitting the program.");
        choosePanel.add(text);
        add(choosePanel);
    }
因为这就是
BoxLayout
的工作原理。您可以使用
GridBagLayout
,例如,默认情况下,它将组件置于容器内的中心


使用
GridBagLayout
-只需执行
选择Panel.setLayout(新的GridBagLayout())它将居中。哇,就这么简单?谢谢:)用户希望使用BoxLayout而不是GridBagLayout@RuslanL奥佩斯卡罗没有明确表示。OP的主要愿望是“简单地将JTextArea集中在JPanel中”。OP刚才说“我正在使用BoxLayout进行此操作”,这对我来说只是一个额外的信息,而不是像“如何使用BoxLayout实现此目的”这样的问题。但我可能完全错了:P@RuslanLópezCarro没关系