[英]Why this code's JTextArea occupies entire JFrame?
我希望框架的一部分包含JTextArea,但它会完全占据。 我无法在此处追踪错误。
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
。 当您仅将某些东西添加到诸如JFrame
类的BorderLayout
组件上时,它将添加到BorderLayout
正中央(如果您未指定添加组件的位置),它将占用整个JFrame
。
您应该使用正确的布局进行调整。
您可以尝试使用绝对布局。 在布局托盘上。
或启用它:
frame = new JFrame();
... //your code here
// to set absolute layout.
frame.getContentPane().setLayout(null);
这样,您可以将控件自由地放置在所需的任何位置。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.