Java GridBagLayout 无法显示 JScrollPane

问题描述
图片说明
代码
package How;
import java.awt.*;
import javax.swing.*;
public class bin_2 extends JFrame
{
JLabel name=new JLabel(" 用户名");
JLabel key=new JLabel(" 密码");
JTextField t_name=new JTextField();
JPasswordField t_key=new JPasswordField();
JTextArea jTextArea=new JTextArea("我现在不可编辑",10,30);
JScrollPane jScrollPane=new JScrollPane(jTextArea);
public bin_2 ()
{
super("文本组件应用");
this.setLayout(new GridBagLayout());
this.setSize(500, 500);
// this.setResizable(false);
GridBagConstraints gridBagConstraints=new GridBagConstraints();
Container container=this.getContentPane();
myadd(name, container, gridBagConstraints, 0, 0, 1, 1);
myadd(t_name, container, gridBagConstraints, 0, 1, 1, 1);
myadd(key, container, gridBagConstraints, 1, 0, 1, 1);
myadd(t_key, container, gridBagConstraints, 1, 1,1,1);
myadd(jScrollPane, container, gridBagConstraints, 2, 0, 10, 4);
this.setVisible(true);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public static void main(String[] args)
{
bin_2 how=new bin_2();
}
public void myadd(Component c, Container con, GridBagConstraints gridBagConstraints,int row, int col, int width,int height)
{
gridBagConstraints.fill=GridBagConstraints.HORIZONTAL;
gridBagConstraints.anchor=GridBagConstraints.CENTER;
if(c==jScrollPane)
{
gridBagConstraints.fill=GridBagConstraints.BOTH;
}
gridBagConstraints.gridx=col;
gridBagConstraints.gridy=row;
gridBagConstraints.gridwidth=width;
gridBagConstraints.gridheight=height;
gridBagConstraints.weightx=1;
gridBagConstraints.weighty=1;
con.add(c,gridBagConstraints);
}
}

1个回答

weixin_40473219
BinHow 非常感谢
接近 2 年之前 回复
Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问