Let classes talk to one another

Hi,

I am doing a project in java but first I wanted to do a simple program that has three class - class with main, class for displaying buttons and a class that has a jtextarea. When you click on a button it then displays a message in the textarea.

I am having trouble letting the classes communicate. I want to have the main program as the overall controller. Please help asap.

This is the code

import javax.swing.*;

import java.awt.*;

import java.awt.event.*;

import java.awt.event.ActionListener;

public class project

{

public static void main(String[] args)

{

//Time time = new Time();

//System.out.println(time.getTime());

right r = new right();

left l = new left();

JPanel p = new JPanel();

JPanel p2 = new JPanel();

JPanel p3 = new JPanel();

JFrame aFrame = new JFrame();

aFrame.setTitle("Swing 3");

aFrame.setBounds(0, 0, 700, 500);

Container contentPane = aFrame.getContentPane();

p.add(l);

p.add(r);

p3.add(p);

//p3.add(p2);

contentPane.add(p3);

aFrame.setVisible(true);

}

}

class left extends project implements ActionListener

{

private JButton redButton,greenButton,blueButton,yellowButton;

public left()

{

///this.r=r;

JPanel pp = new JPanel();

setLayout(new BorderLayout());

redButton = new JButton("Red");

redButton.addActionListener(this); // step 4

greenButton = new JButton("Green");

greenButton.addActionListener(this);

blueButton = new JButton("Blue");

blueButton.addActionListener(this);

yellowButton = new JButton("Yellow");

yellowButton.addActionListener(this);

pp.add(redButton);

pp.add(greenButton);

pp.add(blueButton);

pp.add(yellowButton);

add(pp,BorderLayout.NORTH);

}

public void actionPerformed(ActionEvent e)

{

// e.getSource() // returns name of the button

if (e.getSource() == redButton){

r.textArea.setText("red");

}

if (e.getSource() == greenButton)

{

r.textArea.setText("green");

}

if (e.getSource() == blueButton)

{

r.textArea.setText("blue");

}

if (e.getSource() == yellowButton)

{

r.textArea.setText("yellow");

}

}

}

import javax.swing.*;

import java.awt.*;

import java.awt.event.*;

import java.awt.event.ActionListener;

class right extends project

{

JButton oneButton,twoButton;

JTextArea textArea;

public right()

{

JPanel jp = new JPanel();

setLayout(new BorderLayout());

oneButton = new JButton("one");

twoButton = new JButton("two");

textArea = new JTextArea();

// add(textArea,BorderLayout.SOUTH);

jp.add(oneButton);

jp.add(twoButton);

add(jp,BorderLayout.SOUTH);

add(textArea,BorderLayout.NORTH);

}

}

[3021 byte] By [javaLa] at [2007-11-26 22:47:22]
# 1

> Hi,

>

> I am doing a project in java but first I wanted to do

> a simple program that has three class - class with

> main, class for displaying buttons and a class that

> has a jtextarea. When you click on a button it then

> displays a message in the textarea.

Google for MVC pattern (Model View Control pattern).

> I am having trouble letting the classes communicate.

> I want to have the main program as the overall

> controller. Please help asap.

Don't write asap the next time please: it sounds rude. Why should your problem be more important than other people's questions (or more important than other people's time)?

Also, when posting code, use code tags: http://forum.java.sun.com/help.jspa?sec=formatting

Thanks.

prometheuzza at 2007-7-10 12:06:17 > top of Java-index,Java Essentials,Java Programming...