java - Why does my ButtonGroup give me an "<identifier> expected" error? -


i'm teaching myself java's swing components creating dialog box 3 radio buttons, text box , "go" button. i've gotten point i'm adding radio buttons, , running error code. i've got 1 class calls constructor display dialog box. text field , "go" button have no functionality yet; they're placeholders.

when run code posted below, dialog box displays expected, able select 3 radio buttons simultaneously. reading oracle's jbutton , "how use radio buttons" documentation , tutorials, appears need apply buttongroup jradiobuttons, automatically groups them select-one-and-only-one relationship.

import javax.swing.*; import java.awt.*; import java.awt.event.*; // public class createbuttonsel {     public static void main(string[] args) {         buttonsel thisbuttonsel = new buttonsel();         final int width = 250;         final int height = 250;         thisbuttonsel.setsize(width,height);         thisbuttonsel.setvisible(true);     } } 

however, when un-comment noted code in buttonsel class, despite appearing follow syntax noted in tutorial buttongroup returns error: <identifier> expected carats under open , close parens of ...button.add(...) statements. error, searching stack, seems associated declaring varibale improperly, or @ wrong place in class. can't spot error. import javax.swing.* should give me access button constructors, right? there should pulling in?

what missing here?

import javax.swing.*; import java.awt.*; import java.awt.event.*; // public class buttonsel extends jframe {     jlabel buttonsellabel = new jlabel("select one");     jradiobutton onebutton = new jradiobutton("one", true);     jradiobutton twobutton = new jradiobutton("two", false);     jradiobutton threebutton = new jradiobutton("three", false); /*    comment out following; runs fine.  *    uncomment, , following error:   *  *    error: <identifier> expected carats under open , close   *    parens of ...button.add(...) statements.   *     buttongroup groupofradiobuttons = new buttongroup();     groupofradiobuttons.add(onebutton);     groupofradiobuttons.add(twobutton);     groupofradiobuttons.add(threebutton);    */     jbutton approvebutton = new jbutton("go");     jtextfield textdisplay = new jtextfield(18);      //     jpanel timerpanel = new jpanel();     jpanel radiopanel = new jpanel(new gridlayout(0, 1));      //     public buttonsel() {         super("buttontest");         setdefaultcloseoperation(jframe.exit_on_close);         setlayout(new flowlayout());         add(buttonsellabel);         add(onebutton);         add(twobutton);         add(threebutton);         add(textdisplay);         add(approvebutton);     } } 

you're trying execute statements here, without them being in method or constructor:

buttongroup groupofradiobuttons = new buttongroup(); groupofradiobuttons.add(onebutton); groupofradiobuttons.add(twobutton); groupofradiobuttons.add(threebutton);  

a class directly contains method, constructor , nested type declarations, , initializer blocks.

you should put 3 statements after declaration constructor - could use initializer block, it's not in terms of readability.

public class buttonsel extends jframe {     ... other fields ...      // note: it's idea make fields private     private buttongroup groupofradiobuttons = new buttongroup();      public buttonsel() {         super("buttontest");         ...         groupofradiobuttons.add(onebutton);         groupofradiobuttons.add(twobutton);         groupofradiobuttons.add(threebutton);     } } 

Comments

Popular posts from this blog

Why does Ruby on Rails generate add a blank line to the end of a file? -

keyboard - Smiles and long press feature in Android -

node.js - Bad Request - node js ajax post -