Totally lost - can anyone please help?
Hi,
Can anyone please help with the following...
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.awt.event.ActionListener;
publicclass project
{
publicstaticvoid 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();
// add(new left());
// add(new right());
//p.add(new left());
//p.add(new right());
r.textArea.setText("Red");
p.add(l);
p.add(r);
p3.add(p);
///p3.add(p2);
contentPane.add(p3);
aFrame.setVisible(true);
}
}
===================================
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.awt.event.ActionListener;
publicclass leftextends JPanelimplements ActionListener
{
public JButton redButton,greenButton,blueButton,yellowButton;
public left()
{
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);
}
publicvoid actionPerformed(ActionEvent e)
{
// e.getSource() // returns name of the button
if (e.getSource() == redButton){
// r.setT("red");
System.out.println("red button");
}
if (e.getSource() == greenButton)
{
//r.textArea.setText("green");
System.out.println("green button");
}
if (e.getSource() == blueButton)
{
//r.textArea.setText("blue");
System.out.println("blue button");
}
if (e.getSource() == yellowButton)
{
// r.textArea.setText("yellow");
System.out.println("yellow button");
}
}
}
===================================
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.awt.event.ActionListener;
publicclass rightextends JPanel
{
JButton oneButton,twoButton;
public JTextArea textArea;
public right()
{
JPanel jp =new JPanel();
setLayout(new BorderLayout());
oneButton =new JButton("one");
twoButton =new JButton("two");
textArea =new JTextArea();
jp.add(oneButton);
jp.add(twoButton);
add(jp,BorderLayout.SOUTH);
add(textArea,BorderLayout.NORTH);
}
}
In the left class I want to be able to click a button and then display a message in the JTextArea. I want main to be global - so the r object can be accessed anywhere.
If anyone could get this to work I'd be really grateful.
import java.awt.*;
import java.awt.event.*;
import java.awt.event.ActionListener;
import javax.swing.*;
public class ProjectRx
{
public static void main(String[] args)
{
// For the buttons in left to set text in the textArea
// in right the two classes must communicate in some way.
// Left must have a way to talk to right.
// Option 1:
//Pass a reference to right to left which left can use
//to access the textArea in right.
ProjectRightRx r = new ProjectRightRx();
r.textArea.setText("Red");
ProjectLeftRx l = new ProjectLeftRx(r);
// Option 2:
//You could send a reference to the textArea to left
//instead of the reference to its enclosing class.
//r.textArea.setText("Red");
//ProjectLeftRx l = new ProjectLeftRx(r.textArea);
JFrame aFrame = new JFrame();
aFrame.setTitle("Swing 3");
aFrame.setSize(400, 400);
aFrame.setLocation(100,100);
Container contentPane = aFrame.getContentPane();
contentPane.add(r, "First");
contentPane.add(l, "Last");
aFrame.setVisible(true);
}
}
import java.awt.*;
import java.awt.event.*;
import java.awt.event.ActionListener;
import javax.swing.*;
public class ProjectLeftRx extends JPanel implements ActionListener
{
ProjectRightRx r;
public JButton redButton,greenButton,blueButton,yellowButton;
public ProjectLeftRx(ProjectRightRx r)
{
this.r = r;
JPanel pp = new JPanel();
pp.setBorder(BorderFactory.createTitledBorder("Left"));
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 object that generated this event
// To get the name of the button you can try:
JButton button = (JButton)e.getSource();
String name = button.getActionCommand(); // or,
// button.getText();
String ac = e.getActionCommand();
System.out.println("name = " + name + " ac = " + ac);
if (e.getSource() == redButton){
//r.setT("red");
System.out.println("red button");
}
if (e.getSource() == greenButton)
{
r.textArea.setText("green");
System.out.println("green button");
}
if (e.getSource() == blueButton)
{
r.textArea.setText("blue");
System.out.println("blue button");
}
if (e.getSource() == yellowButton)
{
r.textArea.setText("yellow");
System.out.println("yellow button");
}
}
}
import java.awt.*;
import java.awt.event.*;
import java.awt.event.ActionListener;
import javax.swing.*;
public class ProjectRightRx extends JPanel
{
JButton oneButton,twoButton;
public JTextArea textArea;
public ProjectRightRx()
{
JPanel jp = new JPanel();
setLayout(new BorderLayout());
oneButton = new JButton("one");
twoButton = new JButton("two");
textArea = new JTextArea(10,25);
jp.add(oneButton);
jp.add(twoButton);
add(new JLabel("Right", JLabel.CENTER), BorderLayout.NORTH);
add(jp,BorderLayout.SOUTH);
add(textArea,BorderLayout.CENTER);
}
}