BinHow
2018-10-27 04:43
采纳率: 100%
浏览 557
已采纳

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条回答 默认 最新

  • devmiao 2018-10-27 04:48
    最佳回答
    评论
    解决 无用
    打赏 举报

相关推荐 更多相似问题