为什么这段代码的JTextArea占用了整个JFrame? [英] Why this code's JTextArea occupies entire JFrame?

查看:153
本文介绍了为什么这段代码的JTextArea占用了整个JFrame?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望我的部分框架包含JTextArea但它完全占用。我无法在此处追踪错误。

I expect part of my frame contains the JTextArea but it occupies entirely. I cannot trace the error here.

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

public class EchoServer 
{
   public static void main(String args[])
   {
       CalcFrame c = new CalcFrame();
       CalcTextArea a = new CalcTextArea();
   } 
}

class CalcTextArea 
{
    JTextArea historyDisplayer  = new JTextArea("",50,20);
    CalcTextArea()
    {  
          //historyDisplayer.setVisible(true);
          historyDisplayer.insert("Hello World", 0);              
          Color bg = new Color(23,34,56);              
          historyDisplayer.setBackground(bg);               
          historyDisplayer.setBackground(bg);
    }       
}

class CalcFrame extends CalcTextArea
{
    JFrame frame = new JFrame(); 
    CalcFrame()
    {
        frame.setSize(DEFAULT_WIDTH,DEFAULT_HEIGHT);
        frame.setTitle("CALCULATOR");
        frame.setVisible(true);
        frame.add(historyDisplayer);

    }
    private static int  DEFAULT_WIDTH = 299,DEFAULT_HEIGHT = 190; 
}


推荐答案

默认情况下,JFrame 使用 BorderLayout 。当您只是将某些内容添加到 BorderLayout 组件(如 JFrame )时,它会添加到<$ c的正中心$ c> BorderLayout (如果你没有指定添加组件的位置),它占用整个 JFrame

JFrame by default uses BorderLayout. When you just add something onto a BorderLayout component like JFrame, it would add to the very center of the BorderLayout (if you did not specify where to add the component), and it takes up the entire JFrame.

您应该使用正确的布局调整它们。

You should use the correct layout to adjust them.

这篇关于为什么这段代码的JTextArea占用了整个JFrame?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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