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

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
    关注
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 HFSS 中的 H 场图与 MATLAB 中绘制的 B1 场 部分对应不上
  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?