473,461 Members | 1,435 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Having trouble understanding inheritance

I'm new to Java, and for the life of me I don't understand the whole inheritance concept apparently. I'm trying to create and exit button on a JWindow.

my main Class reads like this
Expand|Select|Wrap|Line Numbers
  1.  public static void main(String[] args) {
  2.        JWindow MainWindow = new JWindow();
  3.        MainWindow.setVisible(true);
  4.        MainWindow.setSize(500,500);
  5.        MainWindow.setLocation(100,100);
  6.        ExitButton Fred = new ExitButton();
  7.        MainWindow.add(Fred);
  8.     }
  9.  
Add my ExitButton Class Reads
Expand|Select|Wrap|Line Numbers
  1. public class ExitButton extends JButton implements ActionListener {
  2.     JButton button = new JButton("Exit");
  3.  
  4.     ExitButton()
  5.     {
  6.         addActionListener(this);
  7.         setSize(10,10);
  8.         setVisible(true);
  9.     }
  10.  
  11.     public void actionPerformed(ActionEvent evt)
  12.     {
  13.         if(evt.getSource()==button)
  14.             {
  15.             System.exit(0);
  16.             }
  17.     }
  18.  
  19. }
  20.  
I'm new to java so at the moment I'm just playing around trying to learn some skills. But the problem I'm having with first off all is when the JWindow Shows up the button also shows up but there is no label. From what I understand when I create "Fred" it should run my Class of ExitButton which tells it to create a button with "Exit" printed on it, all I get is a button that is blank. The other problem is the button isn't doing anything.
More importantly than just fixing my code, I want to know what is wrong with it.
Thank You,
casey
Sep 22 '10 #1

✓ answered by Nepomuk

Hi Casey!

When a class, in this case ExitButton extends another class, in this case JButton, an object created from the first is also an object of the second with some changed properties. So, in the ExitButton code, you don't need the line
Expand|Select|Wrap|Line Numbers
  1. JButton button = new JButton("Exit");
but instead you should have the line
Expand|Select|Wrap|Line Numbers
  1. super("Exit");
at the beginning of the constructor. This means "Call the constructor of the parent class with the argument 'Exit'." Now, as the constructor of JButton with the argument "Exit" will create a button with the label "Exit".

Greetings,
Nepomuk

2 1625
Nepomuk
3,112 Expert 2GB
Hi Casey!

When a class, in this case ExitButton extends another class, in this case JButton, an object created from the first is also an object of the second with some changed properties. So, in the ExitButton code, you don't need the line
Expand|Select|Wrap|Line Numbers
  1. JButton button = new JButton("Exit");
but instead you should have the line
Expand|Select|Wrap|Line Numbers
  1. super("Exit");
at the beginning of the constructor. This means "Call the constructor of the parent class with the argument 'Exit'." Now, as the constructor of JButton with the argument "Exit" will create a button with the label "Exit".

Greetings,
Nepomuk
Sep 23 '10 #2
That clears up some things for me.


Thank you for your help.
Casey
Sep 25 '10 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

5
by: Krumble Bunk | last post by:
Hello! First things first (but not necessarily in that order), this is a really great group, and has helped me understand more and more C everytime I read the postings, so thanks for a great...
7
by: Mike Joseph | last post by:
I have a data file that was produced in MicroSoft Access. It's a table that was exported as a fixed format, sequential access ASCII file. It contains over 80,000 records. Basically, all the stats...
3
by: RSH | last post by:
I am slowly getting the hang of objects and creating my own. I was given some help in assigning an object to a ComboBox collection. I have been looking at it and I get the concept which is very...
3
by: Jake Barnes | last post by:
I thought I understood this article: http://simon.incutio.com/archive/2004/05/26/addLoadEvent It seems like a smart, insightful function: function addLoadEvent(func) { var oldonload =...
11
by: KraftDiner | last post by:
This is not working the way I think it should.... it would appear that fromfile and getName are calling the baseClass methods which are simple passes.... What have I done wrong? class...
9
by: Pyenos | last post by:
Approach 1: class Class1: class Class2: def __init__(self):self.variable="variable" class Class3: def method():print Class1().Class2().variable #problem Approach 1.1:
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.