GUI相关命令(如setPreferredSize())出现大量错误

时间:2016-01-12 07:26:11

标签: java eclipse

我不知道在哪里查明此代码中的错误来源,希望有人可以提供帮助。从本质上讲,这一小部分是我的一个较大项目的一部分,用于创建计算器的GUI。

    JPanel box=new JPanel(); 
    box.setPreferredSize(new Dimension(600,300)); 
    BoxLayout b=new BoxLayout(box, BoxLayout.X_AXIS); 
    box.setLayout(b); 
    box.add(graphOutline); 
    box.add(checks); 
    box.add(checkd); 
    box.add(check2d); 
    box.add(radBut); 
    box.add(yEquals);   
    box.add(y); //}} //}}
    //{{Bottom Buttons //{{ basicbuttons JPanel, includes all the numbers and decimal 

    JPanel basicbuttons=new JPanel(); 
    GridBagLayout gbl=new GridBagLayout(); 
    GridBagConstraints c=new GridBagConstraints(); 

    basicbuttons.setLayout(gbl); 

    c.fill=GridBagConstraints.BOTH; //{{button 1 

    JButton one = new JButton("1");     
    one.setPreferredSize(new Dimension(60,40)); 

    c.gridx=0; 
    c.gridy=2; 
    gbl.setConstraints(one, c); 

    basicbuttons.add(one); //}} //{{button 2 

    JButton two = new JButton("2"); 
    two.setPreferredSize(new Dimension(60,40)); 

    c.gridx=1; 
    c.gridy=2; 
    gbl.setConstraints(two, c); 

    basicbuttons.add(two); //}} //{{button 3 

    JButton three = new JButton("3"); 
    three.setPreferredSize(new Dimension(60,40)); 

    c.gridx=2; 
    c.gridy=2; 
    gbl.setConstraints(three, c); 

    basicbuttons.add(three); //}} //{{button 4 

    JButton four = new JButton("4"); 
    four.setPreferredSize(new Dimension(60,40)); 

    c.gridx=0; 
    c.gridy=1; 
    gbl.setConstraints(four, c); 

    basicbuttons.add(four); //}} //{{button 5 

    JButton five = new JButton("5"); 
    one.setPreferredSize(new Dimension(60,40)); 

    c.gridx=1; 
    c.gridy=1; 
    gbl.setConstraints(five, c);

    basicbuttons.add(five); //}} //{{button 6 

    JButton six = new JButton("6"); 
    six.setPreferredSize(new Dimension(60,40)); 

    c.gridx=2; 
    c.gridy=1; 
    gbl.setConstraints(six, c); 

    basicbuttons.add(six); //}} //{{button 7 

    JButton seven = new JButton("7"); 
    seven.setPreferredSize(new Dimension(60,40)); 

    c.gridx=0; c.gridy=0; 
    gbl.setConstraints(seven, c); 

    basicbuttons.add(seven); //}} //{{button 8 

    JButton eight = new JButton("8"); 
    eight.setPreferredSize(new Dimension(60,40)); 

    c.gridx=1; 
    c.gridy=0; 
    gbl.setConstraints(eight, c); 

    basicbuttons.add(eight); //}} //{{button 9  

    JButton nine = new JButton("9"); 
    nine.setPreferredSize(new Dimension(60,40)); 

    c.gridx=2;  
    c.gridy=0; 
    gbl.setConstraints(nine, c); 

    basicbuttons.add(nine); //}} //{{button 0 

然而,我不断收到诸如

之类的错误
  

令牌上的语法错误" setPreferredSize",=此令牌后的预期

  

令牌上的语法错误"填充",此令牌后预期的VariableDeclaratorId

每当我尝试使用

    box.setPreferredSize(new Dimension(600,300));

    c.fill=GridBagConstraints.BOTH; 

分别

先谢谢!

1 个答案:

答案 0 :(得分:0)

问题是你显然已经直接在课堂上获得了这些代码。将它包装在一个方法中,它将编译。所有非初始化代码必须在方法中。