简体   繁体   English

为什么JTextArea没有出现在此代码中?

[英]Why JTextArea is not showing up in this code?

Why Jtextarea is not showing up in this code? 为什么Jtextarea没有出现在此代码中? I tried to add a Jtextarea in jpanel using gridbaglayout. 我试图使用gridbaglayout在jpanel中添加一个Jtextarea。 The frame opens properly but there is no Jtextarea on it. 框架正确打开,但是上面没有Jtextarea。 I couldn't identify the problem.Someone helps me please. 我无法确定问题所在。请有人帮助我。

    import javax.swing.*;
    import java.awt.*;
    public class ServerTest{
    //object declaration
    JFrame f;
    JPanel p;
    JTextArea ta;
    JTextField tf;
    JButton b1,b2;
    GridBagConstraints gbc;
    //constructor
    public ServerTest(){
        //instantiation
        f=new JFrame("Server");
        p=new JPanel();
        p.setBackground(Color.green);
        ta=new JTextArea("Hello");
        tf=new JTextField();
        b1=new JButton("EMO");
        b2=new JButton("VOICE");
        gbc=new GridBagConstraints();
        //end of instantiation

        //frame task
        f.setLayout(new FlowLayout());
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        p.setLayout(new GridBagLayout());

        gbc.gridx=0;
        gbc.gridy=0;
        gbc.gridwidth=3;
        gbc.gridheight=5;
        p.add(ta,gbc);
        f.add(p);
        f.pack();
        f.setVisible(true);
        //end of frame task

    }

    //Main method
    public static void main(String []args){
        ServerTest st = new ServerTest();
    }
}

The simple answer, there is... 简单的答案是...

你看见我看到的了吗

Here's proof... 这是证明...

证明

You might have better luck if you make use of the JTextArea(int, int) constructor and use a JScrollPane , for example... 例如,如果使用JTextArea(int, int)构造函数并使用JScrollPane ,则可能会更好。

也许更好的主意

public ServerTest() {
    //...
    ta = new JTextArea(10, 20);
    //...
    p.add(new JScrollPane(ta), gbc);
}

You should set some properties of the GridBagLayout to initialize the background grid of the layout: 您应该设置GridBagLayout一些属性以初始化布局的背景网格:

columnWeights 
columnWidths
rowWeights
rowHeights

Also it is not bad to change the layout of the whole frame from FlowLayout to BorderLayout : 同样也可以将整个框架的布局从FlowLayout更改为BorderLayout

f.setLayout(new BorderLayout());

you change your code like this sample and it will work: 您可以像以下示例一样更改代码,它将起作用:

//frame task
f.setLayout(new BorderLayout());
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
GridBagLayout bagLayout = new GridBagLayout();

bagLayout.columnWeights = new double[]{1.0,1.0,1.0,1.0,1.0};
bagLayout.columnWidths = new int[]{25,25,25,25,25};
bagLayout.rowWeights = new double[]{1.0,1.0,1.0,1.0,1.0};
bagLayout.rowHeights = new int[]{25,25,25,25,25};
p.setLayout(bagLayout);

gbc.gridx=0;
gbc.gridy=0;
gbc.gridwidth=3;
gbc.gridheight=5;
gbc.fill = GridBagConstraints.BOTH;

Also GridBagConstraints#fill property has a significant role on sizing the components when using GridBagLayout . 此外,使用GridBagLayout时, GridBagConstraints#fill属性在调整组件大小方面也起着重要作用。

If you want to see some detailed examples: SWING - GridBagLayout Class 如果要查看一些详细的示例,请执行以下操作: SWING-GridBagLayout类

Good Luck 祝好运

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM