[英]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.