Jump to content


Check out our Community Blogs

Register and join over 40,000 other developers!


Recent Topics

Recent Status Updates

View All Updates

Photo
- - - - -

Making a button

setpreferredsize

  • Please log in to reply
9 replies to this topic

#1 hoku2000_99

hoku2000_99

    CC Newcomer

  • Just Joined
  • PipPip
  • 24 posts

Posted 06 March 2009 - 07:28 PM

My task is to design and implement an application that draws a traffic light and uses a push button to change the state of the light. Derive the drawing surface from the JPanel class and use another panel to organize the drawing surface and the button.

I think I have what I need, I am just having trouble with the button portion of the task.

import javax.swing.JFrame;

public class TrafficLight
{
	//-------------------------------------
	//Creates the main program frame
	//-------------------------------------
	public static void main (String[] args)
	{

	JFrame frame=new JFrame("Traffic Light");
	frame.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);

	TrafficLightPanel panel = new TrafficLightPanel();

	frame.getContentPane().add(new TrafficLightPanel());

	frame.pack();
	frame.setVisible(true);
}
}

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

public class TrafficLightPanel extends JPanel
{
	private int count;
	private JButton status;
	private JLabel label;

	//---------------------------------------------------------
	//Constructor:Sets up the GUI
	//---------------------------------------------------------

	public TrafficLightPanel()
	{

	count=0;

	status = new JButton("Status");
	status.addActionListener(new ButtonListener());

	label = new JLabel ("Status: " + count);

	add(status);
	add(label);
}

	//----------------------------------------------
	//Listener for status button(action) events
	//----------------------------------------------
	private class ButtonListener implements ActionListener
	{
		//-------------------------------------------------------
		//Updates the counter and label when the button is pushed
		//-------------------------------------------------------
		public void actionPerformed (Action event)
		{
			count ++;
			label.setText("Status: " + count);
		}
	}

	//----------------------------------------------------------
	//Constructor:Sets up the main characteristics of this panel
	//----------------------------------------------------------

	public TrafficLightPanel()
	{
		setBackground(Color.black);
		setPreferredSize (new Dimension(320,200));
		setFont(new Font("Arial", Font.BOLD,24));

	}

	//---------------------------------------------------------
	//Draws traffic light
	//---------------------------------------------------------

	public void paintComponent (Graphics page)
	{
		super.paintComponent (page);
		page.setColor(Color.red);
		page.fillOval (20, 30, 50, 65);


		page.setColor(Color.yellow);
		page.fillOval (20, 30, 50, 65);


		page.setColor(Color.green);
		page.fillOval (20, 30, 50, 65);

		page.setColor(Color.white);
		page.drawString("Ready...Set...GO!!!",10,180);



	}
}

  • 0

#2 Zenpron

Zenpron

    CC Lurker

  • Just Joined
  • Pip
  • 9 posts

Posted 08 March 2009 - 10:25 PM

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

public class TrafficLightPanel extends JPanel
{
	private int count;
	private JButton status;
	private JLabel label;

	//---------------------------------------------------------
	//Constructor:Sets up the GUI
	//---------------------------------------------------------

	TrafficLightPanel()
	{

	//---------------------------------------------------------
	//Draws traffic light
	//---------------------------------------------------------
		setBackground(Color.black);
		setPreferredSize (new Dimension(320,200));
		setFont(new Font("Arial", Font.BOLD,24));
		
	count=0;

	status = new JButton("Status");
	status.addActionListener(new ButtonListener());

	label = new JLabel ("Status: " + count);

	add(status);
	add(label);
	}

	//----------------------------------------------
	//Listener for status button(action) events
	//----------------------------------------------
	private class ButtonListener implements ActionListener
	{
		//-------------------------------------------------------
		//Updates the counter and label when the button is pushed
		//-------------------------------------------------------
		public void actionPerformed (ActionEvent ae)
		{
			count ++;
			label.setText("Status: " + count);
		}
	}
	
	public void paintComponent (Graphics page)
	{
		super.paintComponent (page);
		page.setColor(Color.red);
		page.fillOval (20, 30, 50, 65);

		page.setColor(Color.yellow);
		page.fillOval (20, 30, 50, 65);

		page.setColor(Color.green);
		page.fillOval (20, 30, 50, 65);

		page.setColor(Color.white);
		page.drawString("Ready...Set...GO!!!",10,180);
	}
}

There were just a few problems. If you have any questions let me know. BTW I like how you used the private class to handle the button press. I've never done that before.
  • 0

#3 Turk4n

Turk4n

    ???

  • Expert Member
  • PipPipPipPipPipPipPip
  • 1919 posts
  • Location:Sweden
  • Programming Language:C, Java, PHP, Python, Bash
  • Learning:C++, C#, JavaScript, Visual Basic .NET, Others

Posted 09 March 2009 - 04:58 AM

JerrySpock you didn't help him enough?
Almost nothing at all !

hoku I will make your thing "almost done", I will make it simple and post it up...
The rest will be then in your hands to figure out !
  • 0

#4 Turk4n

Turk4n

    ???

  • Expert Member
  • PipPipPipPipPipPipPip
  • 1919 posts
  • Location:Sweden
  • Programming Language:C, Java, PHP, Python, Bash
  • Learning:C++, C#, JavaScript, Visual Basic .NET, Others

Posted 09 March 2009 - 06:15 AM

Double post here is something I think would work out if you gave time with !

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

import javax.swing.event.*;
public class TraficBitchLight extends JFrame implements MouseListener {
	
	private JButton Start = new JButton("Start");
	private JPanel JGreen = new JPanel();
	private JPanel JYellow = new JPanel();
	private JPanel JRed = new JPanel();
	private JPanel JBlue = new JPanel();
	private JLabel Trafic = new JLabel("Trafic Light",JLabel.CENTER);
	private JLabel Mouse = new JLabel("",JLabel.CENTER);

	public TraficBitchLight() {
		setLayout(null);
		
		add(Start); add(JGreen); add(JRed); add(JBlue); add(Trafic); add(JYellow); add(Mouse);
		Trafic.setBounds(50,0,80,20);
		Mouse.setBounds(40,280,100,20);
	
		Start.setBounds(50,45,80,20);
		Start.addMouseListener(this);
		
		JRed.setBounds(60,70,60,60);
		JRed.setBackground(Color.RED);
		JRed.setVisible(true);
		
		JYellow.setBounds(60,140,60,60);
		JYellow.setBackground(Color.YELLOW);
		JYellow.setVisible(false);
		
		JGreen.setBounds(60,210,60,60);
		JGreen.setBackground(Color.GREEN);
		JGreen.setVisible(false);
		
		setVisible(true);
		setSize(200,400);
		setDefaultCloseOperation(EXIT_ON_CLOSE);
		show();
	}
	public void mouseClicked(MouseEvent e) {
		if(e.getSource()==Start) {
		Mouse.setText("1,2,3 Go !");
		//Work from here on...
		}
	}
	public void mouseEntered(MouseEvent e) {
		if(e.getSource()==Start) {
		Mouse.setText("Your near");
		//Work from here on...
		}
	}
	public void mouseExited(MouseEvent e) {
		if(e.getSource()==Start) {
		Mouse.setText("You left");	
		//Work from here on...
		}
	}
	public void mousePressed(MouseEvent e) {
		if(e.getSource()==Start) {
		Mouse.setText("Your pressing");	
		//Work from here on...
		}
	}
	public void mouseReleased(MouseEvent e) {
		//Empty and don't add if you don't want to have something?
	}	
	public static void main(String[] arg) {
		 new TraficBitchLight();
	}
}
So here you go, have fun and if need any explanation try to visit the tutorial I gave about JComponents !
  • 0

#5 Zenpron

Zenpron

    CC Lurker

  • Just Joined
  • Pip
  • 9 posts

Posted 09 March 2009 - 03:15 PM

Sorry, I just thought he wanted to get the button working. Thanks for finishing it.
  • 0

#6 Zenpron

Zenpron

    CC Lurker

  • Just Joined
  • Pip
  • 9 posts

Posted 09 March 2009 - 03:54 PM

Turk, I ran your program, and I wasn't able to get it running without removing the show() command. When I tried to compile it said:

"TraficBitchLight.java uses or overrides a depreciated API."

Is there a reason to include show()? Backward compatibility perhaps?
  • 0

#7 Turk4n

Turk4n

    ???

  • Expert Member
  • PipPipPipPipPipPipPip
  • 1919 posts
  • Location:Sweden
  • Programming Language:C, Java, PHP, Python, Bash
  • Learning:C++, C#, JavaScript, Visual Basic .NET, Others

Posted 09 March 2009 - 10:31 PM

Turk, I ran your program, and I wasn't able to get it running without removing the show() command. When I tried to compile it said:

"TraficBitchLight.java uses or overrides a depreciated API."

Is there a reason to include show()? Backward compatibility perhaps?


You don't have JDK 1.6 then, if you can't run the program.
  • 0

#8 hoku2000_99

hoku2000_99

    CC Newcomer

  • Just Joined
  • PipPip
  • 24 posts

Posted 10 March 2009 - 08:10 PM

Thanks for the help you all! This is the only message board that I know of that actually helped me, someone on another message board questioned me if I actually wrote my class myself.
  • 0

#9 Turk4n

Turk4n

    ???

  • Expert Member
  • PipPipPipPipPipPipPip
  • 1919 posts
  • Location:Sweden
  • Programming Language:C, Java, PHP, Python, Bash
  • Learning:C++, C#, JavaScript, Visual Basic .NET, Others

Posted 11 March 2009 - 12:14 AM

Thanks for the help you all! This is the only message board that I know of that actually helped me, someone on another message board questioned me if I actually wrote my class myself.


Good to know?
  • 0

#10 hoku2000_99

hoku2000_99

    CC Newcomer

  • Just Joined
  • PipPip
  • 24 posts

Posted 11 March 2009 - 06:35 PM

I talked to my teacher, and my teacher told me that there is an easier way to make the button change the color. My teacher said that I could declare 1 for red, 2 for yellow, and 3 for green, in the panel and use if statements in my code. how do I do that?

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

public class TrafficLight
{
//-------------------------------------
//Creates the main program frame
//-------------------------------------

public static void main (String[] args)
{

JFrame frame=new JFrame("Traffic Light");
frame.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);

TrafficLightPanel panel = new TrafficLightPanel();

frame.getContentPane().add(new TrafficLightPanel());

frame.pack();
frame.setVisible(true);
}
}

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

public class TrafficLightPanel extends JPanel
{

private int state;
private JButton status;
private JLabel label;


//--------------------------------------------
//Constructor:Creates three circle objects
//------------------------------------------

public TrafficLightPanel()
{
	int red=1;
	int yellow=2;
	int green=3;


//----------------------------------------------
//Constructor: Sets up the GUI
//----------------------------------------------


status = new JButton ("State");
status.addActionListener (new ButtonListener());

label = new JLabel ();

add(status);
add (label);

setPreferredSize (new Dimension (300,200));
setBackground (Color.black);
}

//-------------------------------------------------------
//Represents a listener for button status (action) events
//--------------------------------------------------------
private class ButtonListener implements ActionListener
{

//---------------------------------------------------------------
//Updates the state of the traffic light when the button is pushed
//----------------------------------------------------------------
public void actionPerformed (ActionEvent event)
{
state++;

status = new JButton("State"+1);
repaint();

}
}

//-------------------------------------------------------------
//Draws this panel by requesting that each circle draw itself
//-------------------------------------------------------------

public void paintComponent (Graphics page)
{
	  super.paintComponent(page);


	  page.setColor(Color.red);
      page.fillOval(112, 60, 60,60);

      page.setColor(Color.yellow);
      page.fillOval(112, 60, 60,60);

      page.setColor(Color.green);
  	  page.fillOval(112, 60, 60,60);

	page.setColor(Color.white);
	page.drawString("Ready...Set...GO!!!",100,180);

}
}

  • 0





Also tagged with one or more of these keywords: setpreferredsize

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download