Logo 
Search:

Java Forum

Ask Question   UnAnswered
Home » Forum » Java       RSS Feeds

what wrong wit it

  Asked By: Arland    Date: Oct 14    Category: Java    Views: 569
  

i am totally confused i am made my own class for button which
extends the jbutton class ; but probe is that button is added to the
pane but other attributes doesnt apply to the button dont know why
...pleae see the code and advise me what went wrong and what is the
correct methods
*********************************************************************
import javax.swing.*;
import java.awt.*;
class mybutton extends JButton
{
mybutton(String name)
{
JButton jb=new JButton(name);
jb.setBackground(Color.red);
jb.setForeground(Color.white);
jb.setBorder(new javax.swing.border.CompoundBorder(new

javax.swing.border.SoftBevelBorder
(javax.swing.border.BevelBorder.RAISED), new

javax.swing.border.BevelBorder
(javax.swing.border.BevelBorder.RAISED)));
jb.setBackground(Color.blue);
jb.setForeground(Color.white);

}
}
public class calcc
{
public static void main(String[] args)
{
JFrame jf=new JFrame("asd");
jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
jf.setBounds(300/2 ,200/2,300,250);
jf.setResizable(false);
Container c=jf.getContentPane();
JPanel jp=new JPanel();
jp.setLayout(new GridLayout(5,3,5,5));

c.add(jp,BorderLayout.CENTER);
mybutton my=new mybutton("check");
jp.add(my);
jf.setVisible(true);
}}



Share: 

 

3 Answers Found

 
Answer #1    Answered By: Mike Stephens     Answered On: Oct 14

try this oo way, not other po way. :)

import javax.swing.*;
import java.awt.*;
class mybutton extends JButton
{
mybutton(String name)
{
super(name); //JButton jb=new JButton(name);
setBackground(Color.red);
setForeground(Color.white);
setBorder(new javax.swing.border.CompoundBorder(new
javax.swing.border.SoftBevelBorder
(javax.swing.border.BevelBorder.RAISED), new
javax.swing.border.BevelBorder
(javax.swing.border.BevelBorder.RAISED)));
setBackground(Color.blue);
setForeground(Color.white);
}
}

 
Answer #2    Answered By: Adalric Fischer     Answered On: Oct 14

The button  is not being initialized correctly because you set the
attributes of a local JButton which gets destroyed as soon as the
c'tor exits. Instead, use the super() function to call the appropriate
superclass c'tor and then set the remaining attributes  as necessary
for the object being constructed.

class mybutton extends JButton {
mybutton(String name) {
super(name);
setBackground(Color.red);
setForeground(Color.white);
setBorder(
new javax.swing.border.CompoundBorder(
new javax.swing.border.SoftBevelBorder(
javax.swing.border.BevelBorder.RAISED),
new javax.swing.border.BevelBorder(
javax.swing.border.BevelBorder.RAISED)));
setBackground(Color.blue);
setForeground(Color.white);
}
}

This uses the JButton(String) c'tor to set the button name, then sets
the attributes of the mybutton being created. Since mybutton
subclasses JButton, all JButton functions will work on mybutton.

 
Answer #3    Answered By: Julia Flores     Answered On: Oct 14

create an instance of your own class  and try to set these properties to JButton

mybutton obj = new mybutton("button-name");
obj.setBackground(Color.colorname);
setForeground(Color.colorname);

etc...

this might solve your problem..

 
Didn't find what you were looking for? Find more on what wrong wit it Or get search suggestion and latest updates.




Tagged: