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,
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,