instance of classes

D

dendeezen

Hi,

Several days ago I asked the same question . Hereby now some code to
explain what I mean.

Working is:

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


public class testinstance extends JFrame{

JLabel tekst ;
basis base;
public testinstance () {

tekst = new JLabel();
tekst.setHorizontalAlignment( JLabel.CENTER );
tekst.setText("not pushed");

base = new basis(this);;

Container ContentPane = this.getContentPane();
GridBagLayout gridbag = new GridBagLayout();
GridBagConstraints c = new GridBagConstraints();
ContentPane.setLayout(gridbag);

c.gridx = 0; c.gridy = 0;
c.gridheight = 1; c.gridwidth = 5;
c.weightx = 1; c.weighty = 1;
c.fill = GridBagConstraints.BOTH;
ContentPane.add(tekst,c);

c.gridx = 0; c.gridy = 1;
c.gridheight = 5; c.gridwidth = 5;
c.weightx = 2; c.weighty = 2;
c.fill = GridBagConstraints.BOTH;
ContentPane.add(base,c);

GridLayout gr = new GridLayout(2,0);
ContentPane.setLayout(gr);
ContentPane.add(tekst);
ContentPane.add(base);
}


public static void main(String[] args) {
testinstance f = new testinstance();
//Display full window.
Dimension screenSize =
Toolkit.getDefaultToolkit().getScreenSize();
f.setSize(screenSize);
f.setVisible(true);
f.setDefaultCloseOperation(EXIT_ON_CLOSE);
}


public void schrijftekst(String _text) {
tekst.setText(_text);
}
}
class basis extends JPanel{


basis(testinstance ti) {
GridLayout grid = new GridLayout(3,0);
setLayout(grid);
geg g = new geg(ti);
beta b = new beta();
this.add(g);
this.add(b);
}
}
class geg extends JPanel implements ActionListener {

JButton knop;
testinstance ti;

public geg (testinstance ti) {

setBackground(Color.green);
knop = new JButton("Push");

this.ti = ti;
FlowLayout fl = new FlowLayout();
setLayout(fl);
this.add(knop);
knop.addActionListener(this);
}

public void actionPerformed(ActionEvent e) {
if(e.getSource() == knop) {
ti.schrijftekst("Button pushed!");
}
}
public void keyTyped(KeyEvent e) {}
public void keyReleased(KeyEvent e) {}
}

class beta extends JPanel {
JButton kn;
public beta() {
setBackground(Color.blue);
kn = new JButton("betabutton");

FlowLayout flow = new FlowLayout();
setLayout(flow);
this.add(kn);
}
}

When I try to instantiate the classes geg en beta (in 'real life'
public classes)in each other ( to exchange data) I got in trouble in
the main method from the class testinstance . The code:

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


public class testinstance extends JFrame{

JLabel tekst ;
private basis base;
public testinstance (basis ba) {

tekst = new JLabel();
tekst.setHorizontalAlignment( JLabel.CENTER );
tekst.setText("not pushed");

this.base = ba;

Container ContentPane = this.getContentPane();
GridBagLayout gridbag = new GridBagLayout();
GridBagConstraints c = new GridBagConstraints();
ContentPane.setLayout(gridbag);

c.gridx = 0; c.gridy = 0;
c.gridheight = 1; c.gridwidth = 5;
c.weightx = 1; c.weighty = 1;
c.fill = GridBagConstraints.BOTH;
ContentPane.add(tekst,c);

c.gridx = 0; c.gridy = 1;
c.gridheight = 5; c.gridwidth = 5;
c.weightx = 2; c.weighty = 2;
c.fill = GridBagConstraints.BOTH;
ContentPane.add(base,c);

GridLayout gr = new GridLayout(2,0);
ContentPane.setLayout(gr);
ContentPane.add(tekst);
ContentPane.add(base);
}


public static void main(String[] args) {

// ' this ' is not allowed in a static !
//this.base = base;
//the constructor basis() is undefined
basis ba = new basis();
// but, what is the solution?
testinstance f = new testinstance(ba);
//Display full window.
Dimension screenSize =
Toolkit.getDefaultToolkit().getScreenSize();
f.setSize(screenSize);
f.setVisible(true);
f.setDefaultCloseOperation(EXIT_ON_CLOSE);
}


public void schrijftekst(String _text) {
tekst.setText(_text);
}
}
class basis extends JPanel{


basis(testinstance ti, beta be, geg ge) {
GridLayout grid = new GridLayout(3,0);
setLayout(grid);
geg g = new geg(ti, be);
beta b = new beta(ge);
this.add(g);
this.add(b);
}
}
class geg extends JPanel implements ActionListener {

JButton knop;
testinstance ti;
beta be;
public geg (testinstance ti, beta be) {

setBackground(Color.green);
knop = new JButton("Push");

this.ti = ti;
this.be = be;
FlowLayout fl = new FlowLayout();
setLayout(fl);
this.add(knop);
knop.addActionListener(this);
}

public void actionPerformed(ActionEvent e) {
if(e.getSource() == knop) {
ti.schrijftekst("Button pushed!");
}
}
public void keyTyped(KeyEvent e) {}
public void keyReleased(KeyEvent e) {}
}

class beta extends JPanel {
JButton kn;
geg ge;
public beta(geg ge) {
setBackground(Color.blue);
kn = new JButton("betabutton");
this.ge = ge;


FlowLayout flow = new FlowLayout();
setLayout(flow);
this.add(kn);
}
}

Thanks for helping a newbie,
 
A

Andrew Thompson

dendeezen wrote:
..
Several days ago I asked the same question .

It might have made more sense to continue the
discussion on that thread, but since this post has
all relevant information (though the question was
cleverly hidden!) I will continue it here.
..Hereby now some code ..

Self contained examples, no less! :)
..to explain what I mean.

'Let the code do the talking'? I am comfortable
with that.
Thanks for helping a newbie,

Note that a good group for those starting Java
is comp.lang.java.help, this group is better suited
to folks that have more complex problems.

But to your code.

When compiling, I got the error 'cannot find symbol
symbol : constructor basis()' that confused me for
a few moments, as I expected it to get a 'default
constructor' but a bit of googling later suggested to me
that the default constructor is *only* created when
there are *no* other constructors defined - and the
basis class already had one that took three arguments.

The 'simplest solution' is to explicitly add a 'no args.'
constructor, but note that is *probably* not the entire
solution. The compilable form of the basis class, is
as follows..

<snippet>
class basis extends JPanel{

basis() {
}

basis(testinstance ti, beta be, geg ge) {
GridLayout grid = new GridLayout(3,0);
setLayout(grid);
geg g = new geg(ti, be);
beta b = new beta(ge);
this.add(g);
this.add(b);
}
}
</snippet>

HTH
 
D

dendeezen

dendeezen wrote:

.


It might have made more sense to continue the
discussion on that thread, but since this post has
all relevant information (though the question was
cleverly hidden!) I will continue it here.


Self contained examples, no less! :)


'Let the code do the talking'? I am comfortable
with that.


Note that a good group for those starting Java
is comp.lang.java.help, this group is better suited
to folks that have more complex problems.

But to your code.

When compiling, I got the error 'cannot find symbol
symbol : constructor basis()' that confused me for
a few moments, as I expected it to get a 'default
constructor' but a bit of googling later suggested to me
that the default constructor is *only* created when
there are *no* other constructors defined - and the
basis class already had one that took three arguments.

The 'simplest solution' is to explicitly add a 'no args.'
constructor, but note that is *probably* not the entire
solution. The compilable form of the basis class, is
as follows..

<snippet>
class basis extends JPanel{

basis() {
}

basis(testinstance ti, beta be, geg ge) {
GridLayout grid = new GridLayout(3,0);
setLayout(grid);
geg g = new geg(ti, be);
beta b = new beta(ge);
this.add(g);
this.add(b);
}}

</snippet>

HTH


Dear Andrew,

I tried some 'newbie' groups, but could not get a solution...

As you could see, I already wrote the compilable form of the basis
class.

regards,

Dendeezen
 
A

Andrew Thompson

dendeezen wrote:
..
I tried some 'newbie' groups, but could not get a solution...

Please refrain from 'full-quoting'.

But two notes:
- I see I was wrong about posting to c.l.j.help,
you already are!
- My web interface to usenet showed this as
a new post, hence mysuggestion about putting
this on the old thread - but again, you *did* do that.
..my apologies for any confusion my comments
might have caused.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,969
Messages
2,570,161
Members
46,708
Latest member
SherleneF1

Latest Threads

Top