哪位擅长java的,帮我把补充问题里的程序改对啊!

importjavax.swing.*;importjava.awt.*;importjava.awt.event.*;publicclassJSQ//定义一个类继承JP... import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

public class JSQ //定义一个类继承JPanel类
{
static final int w=300;
static final int h=400;
public void add1(Component c,GridBagConstraints constraints,int x,int y,int ww,int hh)//此方法把控件添加到网格中
{
constraints.gridx=x;
constraints.gridy=y;
constraints.gridwidth=ww;
constraints.gridheight=hh;
add1(c,constraints);
}
JSQ()
{
JFrame jf=new JFrame("计算器");
GridBagLayout lay=new GridBagLayout();
JPanel contentPane=new JPanel();
jf.setContentPane(contentPane);
contentPane.setLayout(lay);//添加布局管理器
Toolkit kit=Toolkit.getDefaultToolkit();//让窗口在屏幕中剧中
Dimension screenSize=kit.getScreenSize();
int width=screenSize.width;
int height=screenSize.height;
int x=(width-w)/2;
int y=(height-h)/2;
jf.setLocation(x,y);
JTextField tf=new JTextField(15);
JButton b1=new JButton("1");
JButton b2=new JButton("2");
JButton b3=new JButton("3");
JButton b4=new JButton("4");
JButton b5=new JButton("5");
JButton b6=new JButton("6");
JButton b7=new JButton("7");
JButton b8=new JButton("8");
JButton b9=new JButton("9");
JButton b10=new JButton("0");
JButton b11=new JButton("+");
JButton b12=new JButton("-");
JButton b13=new JButton("*");
JButton b14=new JButton("/");
JButton b15=new JButton("=");
JButton b16=new JButton("c");
GridBagConstraints constraints=new GridBagConstraints();
constraints.fill=GridBagConstraints.NONE;
constraints.anchor=GridBagConstraints.CENTER;
constraints.weightx=4;
constraints.weighty=5;
add1(tf,constraints,0,0,4,1);
add1(b1,constraints,1,0,1,1);
add1(b2,constraints,1,1,1,1);
add1(b3,constraints,1,2,1,1);
add1(b4,constraints,1,3,1,1);
add1(b5,constraints,2,0,1,1);
add1(b6,constraints,2,1,1,1);
add1(b7,constraints,2,2,1,1);
add1(b8,constraints,2,3,1,1);
add1(b9,constraints,3,0,1,1);
add1(b10,constraints,3,1,1,1);
add1(b11,constraints,3,2,1,1);
add1(b12,constraints,3,3,1,1);
add1(b13,constraints,4,0,1,1);
add1(b14,constraints,4,1,1,1);
add1(b15,constraints,4,2,1,1);
add1(b16,constraints,4,3,1,1);
jf.setResizable(false);
jf.setVisible(true);
}
public static void main(String[] args) //主函数(入口)
{
new JSQ();
}
}
展开
 我来答
wwxjse
2013-01-09 · TA获得超过215个赞
知道小有建树答主
回答量:274
采纳率:100%
帮助的人:166万
展开全部
package t;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

public class asd
{
static final int w=300;
static final int h=400;
public void add1(JFrame jf,Component c,GridBagConstraints constraints,int x,int y,int ww,int hh)/轮首/此方法把控件添加到网格中
{
constraints.gridx=x;
constraints.gridy=y;
constraints.gridwidth=ww;
constraints.gridheight=hh;
jf.add(c);

}
asd()
{
JFrame jf=new JFrame("计算器");
GridBagLayout lay=new GridBagLayout();
JPanel contentPane=new JPanel();
jf.setContentPane(contentPane);
contentPane.setLayout(lay);//腊弊数添加布局管理器
Toolkit kit=Toolkit.getDefaultToolkit();//让窗口在屏幕中剧中
Dimension screenSize=kit.getScreenSize();
int width=screenSize.width;
int height=screenSize.height;
int x=(width-w)/2;
int y=(height-h)/2;
jf.setLocation(x,y);
JTextField tf=new JTextField(15);
JButton b1=new JButton("1");
JButton b2=new JButton("2");
JButton b3=new JButton("3");
JButton b4=new JButton("卜高4");
JButton b5=new JButton("5");
JButton b6=new JButton("6");
JButton b7=new JButton("7");
JButton b8=new JButton("8");
JButton b9=new JButton("9");
JButton b10=new JButton("0");
JButton b11=new JButton("+");
JButton b12=new JButton("-");
JButton b13=new JButton("*");
JButton b14=new JButton("/");
JButton b15=new JButton("=");
JButton b16=new JButton("c");
GridBagConstraints constraints=new GridBagConstraints();
constraints.fill=GridBagConstraints.NONE;
constraints.anchor=GridBagConstraints.CENTER;
constraints.weightx=4;
constraints.weighty=5;
add1(jf,tf,constraints,0,0,4,1);
add1(jf,b1,constraints,1,0,1,1);
add1(jf,b2,constraints,1,1,1,1);
add1(jf,b3,constraints,1,2,1,1);
add1(jf,b4,constraints,1,3,1,1);
add1(jf,b5,constraints,2,0,1,1);
add1(jf,b6,constraints,2,1,1,1);
add1(jf,b7,constraints,2,2,1,1);
add1(jf,b8,constraints,2,3,1,1);
add1(jf,b9,constraints,3,0,1,1);
add1(jf,b10,constraints,3,1,1,1);
add1(jf,b11,constraints,3,2,1,1);
add1(jf,b12,constraints,3,3,1,1);
add1(jf,b13,constraints,4,0,1,1);
add1(jf,b14,constraints,4,1,1,1);
add1(jf,b15,constraints,4,2,1,1);
add1(jf,b16,constraints,4,3,1,1);
jf.setSize(800,400);
jf.setResizable(false);
jf.setVisible(true);
}
public static void main(String[] args) //主函数(入口)
{
new asd();
}
}
更多追问追答
追问

我改了之后为什麽文本框在按钮哪行为什麽?

追答
新建个JPanel ,如p1,将JFrame的layout设置为null
然后将textfield添加到新建的p1中,
p1.setBounds(0,0,200,25);
contentPane.setBounds(0,25,300,300);

jf.add(p1);
jf.add(contentPane);
注释掉下面这行
jf.setContentPane(contentPane);
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式