当前位置: 代码迷 >> Java相关 >> 又遇到问题了,请教一下。关于JFrame容器添加组件的.
  详细解决方案

又遇到问题了,请教一下。关于JFrame容器添加组件的.

热度:136   发布时间:2006-01-29 02:37:00.0
又遇到问题了,请教一下。关于JFrame容器添加组件的.

想做一个有界面的,公制和英制转换的.
遇到一个问题.添加组件到容器的时候用的方法是从书上看来的.
错误提示是要我加")".
环境里好象没有识别这个方法.
具体代码如下,希望大家有空指点一下.不要笑话我.

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class Conversion extends JFrame{
private static final double KILOMETERS_PER_GALLON=1.609344;
private static final double LITERS_PER_GALLON=3.785411784;
private static final double GRAMS_PER_OUNCE=28.349523125;
private static final double HECTARES_PER_ACRE=0.40468564;
GridBagConstraints gbc;
GridBagLayout gb;
public Conversion(){
MyPanel mp=new MyPanel();
Container con=getContentPane();
con.setLayout(gb);

gbc=new GridBagConstraints();

JTextField texMile=new JTextField();
JTextField texGallon=new JTextField();
JTextField texOunce=new JTextField();
JTextField texAcre=new JTextField();

JLable lbMile=new JLable("=");
JLable lbGallon=new JLable("=");
JLable lbOunce=new JLable("=");
JLable lbAcre=new JLable("=");

JTextField texKillometers=new JTextField();
JTextField texLiters=new JTextField();
JTextField texGrams=new JTextField();
JTextField texHectares=new JTextField();

gbc.fill=GridBagConstraints.HORIZONTAL;
addComponent(texMile,0,0,2,1);
gbc.fill=GridBagConstraints.HORIZONTAL;
addComponent(lbMile,0,1,2,1);


}

public void addComponent( Component c,int row,int col,int nrow,int,ncol ){
gbc.gridx = row;
gbc.gredy = col;

gbc.gridwidth=ncol;
gbc.gridheight=nrow;

gb.setConstraints(c,gbc);
add(c);
}

}
还没有完成.就是方法addComponent提示有问题.

搜索更多相关的解决方案: JFrame  组件  容器  

----------------解决方案--------------------------------------------------------
//there are so much mistakes in yr code ,
//i use red color to mark them all ,
//hope u can do it better next time^_^
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class Conversion extends JFrame{
private static final double KILOMETERS_PER_GALLON=1.609344;
private static final double LITERS_PER_GALLON=3.785411784;
private static final double GRAMS_PER_OUNCE=28.349523125;
private static final double HECTARES_PER_ACRE=0.40468564;
GridBagConstraints gbc;
GridBagLayout gb;
public Conversion(){
MyPanel mp=new MyPanel(); //MyPanel must be yr owen class,right?
Container con=getContentPane();
con.setLayout(gb);

gbc=new GridBagConstraints();

JTextField texMile=new JTextField();
JTextField texGallon=new JTextField();
JTextField texOunce=new JTextField();
JTextField texAcre=new JTextField();

JLable lbMile=new JLable("="); //all of 'JLable' should be 'JLabel'
JLable lbGallon=new JLable("=");
JLable lbOunce=new JLable("=");
JLable lbAcre=new JLable("=");

JTextField texKillometers=new JTextField();
JTextField texLiters=new JTextField();
JTextField texGrams=new JTextField();
JTextField texHectares=new JTextField();

gbc.fill=GridBagConstraints.HORIZONTAL;
addComponent(texMile,0,0,2,1);
gbc.fill=GridBagConstraints.HORIZONTAL;
addComponent(lbMile,0,1,2,1);


}

public void addComponent( Component c,int row,int col,int nrow,int,ncol ){ //an unnecessary comma
gbc.gridx = row;
gbc.gredy = col; //should be gbc.gridy

gbc.gridwidth=ncol;
gbc.gridheight=nrow;

gb.setConstraints(c,gbc);
add(c);
}

}

[此贴子已经被作者于2006-1-29 13:36:37编辑过]


----------------解决方案--------------------------------------------------------
首先我没有写注释,给帮忙的兄弟,带来困难,先抱歉一个,以后一定注意.写的清楚.
感谢楼上的,这么热心.

唉,又是拼写错误,真不知道我这么粗心,能不能做个称职的程序员.
----------------解决方案--------------------------------------------------------