The Artima Developer Community
Sponsored Link

Legacy Java Answers Forum
January 2002

Advertisement

Advertisement

This page contains an archived post to the Java Answers Forum made prior to February 25, 2002. If you wish to participate in discussions, please visit the new Artima Forums.

Message:

Help with inheritance (super) and extends ?

Posted by Avin Sinanan on February 02, 2002 at 7:16 PM

Hello am kinda new with inheritance and extending
JButtons. Ive been trying to write the code to do the following:

1).There are 2 classes. One class makes a JButton
with a the label "hello" and sets the border painted around the button to false.

2)The second class is suppose to extend this class
and give that button additional features. It is suppose to add a picture to the button.

3) So when the main motho calls the second class it returns a button with the label "hello" , a false painted border and a gif picture attached to it.

I know you must be saying shy didn't I just combine everything into one class..I can do that.. but I have to get familiar with inheritance so that I can do a bigger project later on.

here is the code..can someone correct it for me please.. thanks in advance

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
import java.util.*;


public class ButtonExtend
{
public static void main(String[] args)
{
JFrame frame = new JFrame("Hello");
JPanel pane = new JPanel();

Button2 item = new Button2();
JButton b1 = item.button2();
pane.add(b1);

frame.getContentPane().add(pane);
frame.setSize(400,400);
frame.setVisible(true);


}
}

/*------------------------------*/
class Button1
{
public Button1()
{
JButton buttonOne = new JButton("Hello");
buttonOne.setBorderPainted(false);
}

public void button1()
{

}
}

/*--------------------------------*/

class Button2 extends Button1
{
public Button2()
{

}

public JButton button2()
{
super.button1();
JButton button = new JButton("",new ImageIcon("HLR.gif"));
return button;
}
}




Replies:

Sponsored Links



Google
  Web Artima.com   
Copyright © 1996-2009 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use - Advertise with Us