java - JButton in separate class file -


i wrote class contain simple jframe , want add button other class jframe

when i'm trying write

panel.add(new cancelbutton()) 

i got error: loginwindow.java:29: error: no suitable method found add(cancelbutton)

can me it?

import javax.swing.*; import java.awt.*;   public class loginwindow {  public loginwindow(){  //login window jframe frame = new jframe("movie date base"); frame.setsize(500,500); frame.setvisible(true); frame.setdefaultcloseoperation(jframe.exit_on_close); //label jlabel label = new jlabel("welcom movie date base! "); label.setfont(new font("verdana", font.plain, 18)); //panel jpanel panel = new jpanel(new gridbaglayout()); jpanel panel1 = new jpanel(new gridbaglayout()); //add panel frame frame.add(panel); frame.getcontentpane().add(panel, borderlayout.north); frame.getcontentpane().add(panel1, borderlayout.south);  //grid layout gridbagconstraints c = new gridbagconstraints(); c.insets = new insets(100,10,10,10); panel.add(label,c);     } }  import javax.swing.*;  public class cancelbutton { public cancelbutton(){  jbutton cancel = new jbutton("cancel");    }  } 

if want treat cancelbutton class jbutton, has extend jbutton.

public class cancelbutton extends jbutton{    public cancelbutton(){       //call parent-level constructor       super("cancel");    } } 

note i've gotten rid of cancel variable here, since cancelbutton class is-a jbutton now. can use anywhere can use jbutton.

however, if instead want use cancel variable in other classes, need create kind of getter function it:

public class cancelbutton {     jbutton cancel;     public cancelbutton(){       cancel = new jbutton("cancel");    }     public jbutton getbutton(){       return cancel;    } } 

then call getbutton() function, this:

panel.add(new cancelbutton().getbutton()) 

which approach take depends on you're trying do, , how want organize code.


Comments

Popular posts from this blog

java - Custom OutputStreamAppender not run: LOGBACK: No context given for <MYAPPENDER> -

java - UML - How would you draw a try catch in a sequence diagram? -

c++ - No viable overloaded operator for references a map -