java jtextfield设置不可见,Java JPannel不可见

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);

}

}

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

Why is that happening.

解决方案

"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); 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();

setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);

setVisible(true);

}

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

JTextField t1 = new JTextField(20);

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.

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值