Java JPannel不可见 [英] Java JPannel not Visible

查看:130
本文介绍了Java JPannel不可见的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试使用带有以下代码的JFrame创建简单的GUI.

I tried creating simple GUI using JFrame with below code.

package sorting_array_gui;

package sorting_array_gui;



import java.awt.BorderLayout; 
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;

import javax.swing.JScrollPane;

import javax.swing.JTable;

import javax.swing.JTextArea;

import javax.swing.JTextField;

import javax.swing.WindowConstants;

import javax.swing.table.DefaultTableModel;


public class userwindow extends JFrame {

private static final long serialVersionUID = 1L;



public userwindow() {
        super("A Programm to Sort Your Array");
        setSize(1000,600);
        setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        setVisible(true);

        JPanel p1= new JPanel();

        JButton b1= new JButton("Click Here");
        p1.add(b1);

        JTextField t1= new JTextField();


        p1.add(t1);
        JLabel l1= new JLabel("This is a Lable");
        p1.add(l1);



        add(p1,BorderLayout.CENTER);



}


}

当我添加JTextfield时,JPlane行为异常,甚至JButton和JLabel也停止显示. 为什么会这样.

When I added JTextfield , JPlane misbehaved and even JButton and JLabel stoped showing. Why is that happening.

推荐答案

当我添加JTextfield时,JPlane行为异常,甚至JButton和JLabel也停止显示."

"When I added JTextfield , JPlane misbehaved and even JButton and JLabel stoped showing."

我的代码没有这种行为.但是您应该注意以下几点.

I don't get this behavior with your code. But you should note the below.

  • setVisible(true);应该是添加所有组件之后最后一个您要做的事情.

  • setVisible(true); should be that last thing you do after adding all components.

public userwindow() {
    super("A Programm to Sort Your Array");
    JPanel p1= new JPanel();
    JButton b1= new JButton("Click Here");
    p1.add(b1);
    JTextField t1= new JTextField();
    p1.add(t1);
    JLabel l1= new JLabel("This is a Lable");
    p1.add(l1);
    add(p1,BorderLayout.CENTER);


    pack();                                                  <--- PACK frame
    setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    setVisible(true);                                        <--- LAST

}

  • 此外,您应该使用设置列大小的构造函数为文本字段设置大小

  • Also, you should set a size to your text field using the constructor that sets the column size

    JTextField t1 = new JTextField(20);
    

  • 此外,您应该使用pack()而不是setSize().如果只是pack()一切应该可见,因为会尊重所有组件的首选尺寸.

  • Also, you should use pack() instead of setSize(). If you just pack(), everything should be visible, as the preferred sizes of all the components are respected.

    还请注意,如果要将其他任何组件添加到JFrame,则需要为 each 组件指定BorderLayout位置,而 no 职位被多次使用. 请参见 在容器内布置组件

    Also note, if you want to add any other components to the JFrame you need to specify a BorderLayout position for each component, with no positions being used more than once. See Laying out Components Within a Container

    这篇关于Java JPannel不可见的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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