oldmoon 2005年03月19日
最近用网格袋布局,仿WINXP做一个计算器,出了一点问题,不知那个7怎么会跑上去!请各位指点!!
如题!
程序如下:import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class JSQ extends JFrame
{
int i,j,k;
JPanel jp,jp2,jp3;
JMenuBar jm;
JMenu jme1,jme2,jme3;
JMenuItem jmi1,jmi2,jmi3,jmi4,jmi5,jmi6,jmi7;
JTextField jf;
JButton btn[]=new JButton[28];
String num[]={"NULL","Backspace","CE","C","MC","MR","MS","M+","7","8","9","/","sqrt","4","5","6","*","%","1","2","3","-","1/x","0","+/-",".","+","="};
void buildConstraints(GridBagConstraints gbc,int gx,int gy,int gw,int gh,int wx,int wy)
{
gbc.gridx=gx;
gbc.gridy=gy;
gbc.gridwidth=gw;
gbc.gridheight=gh;
gbc.weightx=wx;
gbc.weighty=wy;
}
public void PaiBan()
{

GridBagLayout gbg=new GridBagLayout();
GridBagConstraints constraints=new GridBagConstraints();
jp=new JPanel(gbg);
//jp2=new JPanel();
//jp3=new JPanel();
jm=new JMenuBar();
jme1=new JMenu("编辑(E)");
jmi1=new JMenuItem("复制");
jmi2=new JMenuItem("粘贴");
jme1.add(jmi1);
jme1.add(jmi2);
jme2=new JMenu("查看(V)");
jmi3=new JMenuItem("标准型");
jmi4=new JMenuItem("科学型");
jmi5=new JMenuItem("数学分组");
jme2.add(jmi3);
jme2.add(jmi4);
jme2.add(jmi5);
jme3=new JMenu("帮助(H)");
jmi6=new JMenuItem("帮助主题");
jmi7=new JMenuItem("关于计算器");
jme3.add(jmi6);
jme3.add(jmi7);
jm.add(jme1);
jm.add(jme2);
jm.add(jme3);
jf=new JTextField("0");
//btn[]=new JButton[28];
for (i=0;i<num.length;i++)
{
btn[i]=new JButton(num[i]);
}
buildConstraints(constraints,0,0,3,1,0,8);
constraints.fill=GridBagConstraints.NONE;
constraints.anchor=GridBagConstraints.WEST;
gbg.setConstraints(jm,constraints);
jp.add(jm);
buildConstraints(constraints,0,1,6,1,8,5);
constraints.fill=GridBagConstraints.BOTH;
//constraints.anchor=GridBagConstraints.EAST;
gbg.setConstraints(jf,constraints);
jp.add(jf);
for(i=0;i<4;i++)
{
buildConstraints(constraints,i,2,1,1,0,8);
constraints.fill=GridBagConstraints.NONE;
constraints.anchor=GridBagConstraints.WEST;
gbg.setConstraints(btn[i],constraints);
jp.add(btn[i]);
}
for(i=3,j=4;i<8;i++,j++)
{
buildConstraints(constraints,0,i,1,1,16,5);
constraints.fill=GridBagConstraints.NONE;
constraints.anchor=GridBagConstraints.WEST;
gbg.setConstraints(btn[j],constraints);
jp.add(btn[j]);
}

for(i=3,j=8,k=1;j<num.length;j++,k++)
{
buildConstraints(constraints,k,i,1,1,0,0);
constraints.fill=GridBagConstraints.NONE;
constraints.anchor=GridBagConstraints.CENTER;
gbg.setConstraints(btn[j],constraints);
jp.add(btn[j]);
if(k%6==0)
{i++;k=1;}
}
setContentPane(jp);
//jp.setJMenuBar(jm);
}
public static void main(String arg[])
{
JSQ jsq=new JSQ();
jsq.PaiBan();
jsq.setTitle("计算器");
jsq.setSize(500,500);
jsq.show();
jsq.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}


...全文
101 点赞 收藏 9
写回复
9 条回复

还没有回复,快来抢沙发~

发动态
发帖子
Java SE
创建于2007-09-28

3.4w+

社区成员

30.7w+

社区内容

Java 2 Standard Edition
社区公告
暂无公告