help Displaying shapes after button is clicked

help

  • (2 Pages)
  • +
  • 1
  • 2

29 Replies - 9871 Views - Last Post: 09 April 2010 - 07:32 AM Rate Topic: -----

#16 NeoTifa  Icon User is offline

  • NeoTifa Codebreaker, the Scourge of Devtester
  • member icon





Reputation: 4099
  • View blog
  • Posts: 18,184
  • Joined: 24-September 08

Re: help Displaying shapes after button is clicked

Posted 08 April 2010 - 10:37 AM

It's basically a general statement. I would put the JLabel shapeLabel in the 5th panel. You can set a default text if you'd like though. In the action listeners for the buttons just set shapeLabel to whatever the buttons text is.

public void actionPerformed(ActionEvent e) {
Object source = e.getSource();
shapeLabel.setText(source.getText());
}


Was This Post Helpful? 0
  • +
  • -

#17 wpdaddy7  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 20
  • Joined: 05-April 10

Re: help Displaying shapes after button is clicked

Posted 08 April 2010 - 11:18 AM

That's where the ActionListener comes in. You'll have to add an action listener to the buttons. In the Listener you build the constructor, JLabel, to be displayed. The link above to the ActionListener in the API should give you what you need.
Was This Post Helpful? 0
  • +
  • -

#18 ryclegman  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 83
  • Joined: 24-February 10

Re: help Displaying shapes after button is clicked

Posted 08 April 2010 - 05:12 PM

okay i tryed and i still get errors with the .gettext ...... heres my code... this is so frustrating... 4 hrs and still no solution

import javax.swing.*;
import java.awt.FlowLayout;
import java.awt.Frame;
import javax.swing.JPanel;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.SwingConstants;
import javax.swing.Icon;
import javax.swing.ImageIcon;
import java.awt.event.*;
import java.awt.*;


public class RAC11_01 implements ActionListener { 
	JButton button1;
	JButton button2;
	JButton button3;
	JButton button4;
	
	/**
	 * @param args
	 */
	public static void main(String[] args) {
		RAC11_01 gui = new RAC11_01();
		gui.go();
	}

	
public void go(){
	
		
		JFrame frame = new JFrame("Test Frame");
	   JPanel RectPanel = new JPanel ();
	   JPanel OvalPanel = new JPanel ();
	   JPanel ArcPanel = new JPanel ();
	   JPanel PolyPanel= new JPanel  ();
	   JPanel panel5  = new JPanel (); 
//----------------------------------------------------------------------------- FRAME FOR ....
	   
	   panel5.setBackground(Color.PINK);
	   frame.getContentPane().add(panel5);
	   frame.getContentPane () . add (BorderLayout. EAST, panel5);
	    frame.setSize(600,150);
	    
	    
	   
	  
	    
	     frame.setVisible(true);
	     
	   
		
//----------------------------------------------------------------------	   panel1
	   RectPanel.setBackground(Color.BLACK);
	   frame.getContentPane().add(RectPanel);
	   
	   frame.getContentPane().setLayout(new FlowLayout());
	    frame.setSize(300,150);
	    
	    JButton button1 = new JButton("Show Rectangle");
	   
	    button1.addActionListener(this);
	    RectPanel.add(button1);
	  
	     frame.setVisible(true);
	     
//---------------------------------------------------------------------------	panel2     
	    
	     OvalPanel.setBackground(Color.red);
		   frame.getContentPane().add(OvalPanel);
		   
		   frame.getContentPane().setLayout(new FlowLayout());
		    frame.setSize(300,150);
		    
		    
		    JButton button2 = new JButton("Show Oval");
		    button2.addActionListener(this);
	
		    OvalPanel.add(button2);
		    
		    
		    frame.setVisible(true);
	     
	     
//--------------------------------------------------------------------------	     panel3
		    ArcPanel.setBackground(Color.GREEN);
	   
	    frame.getContentPane().add(ArcPanel);

	    JButton button3 = new JButton("Show Arc");
	   
	    button3.addActionListener(this);
	 
	    ArcPanel.add(button3);
	   
	;
	   frame.setSize(300,150);
	    frame.setVisible(true);
	
	   
	   frame.setDefaultCloseOperation(
	    		JFrame.EXIT_ON_CLOSE);
	   
//--------------------------------------------------------------------------------------panel4
	   PolyPanel.setBackground(Color.blue);
	   
	    frame.getContentPane().add(PolyPanel);

	    JButton button4 = new JButton("Show Poly");
	    
	    button1.addActionListener(this);
	 
	    PolyPanel.add(button4);
	    	
	   frame.setSize(300,150);
	    frame.setVisible(true);
	
	   
	   frame.setDefaultCloseOperation(
	    		JFrame.EXIT_ON_CLOSE);
	   
	   
	   
	}



	
	
			
//----------------------------------------------------------
				
	
public void actionPerformed(ActionEvent e) {
	Object source = e.getSource();
	button1.setText(source.getText("Rect"));
	button2.setText(source.getText("something"));
	button3.setText(source.getText("something"));
	button4.setText(source.getText("something"));


}

}

			

 

Was This Post Helpful? 0
  • +
  • -

#19 macosxnerd101  Icon User is online

  • Games, Graphs, and Auctions
  • member icon




Reputation: 12186
  • View blog
  • Posts: 45,250
  • Joined: 27-December 08

Re: help Displaying shapes after button is clicked

Posted 08 April 2010 - 05:34 PM

In the future, please specifically describe your errors, including the specific code that triggers it.

button1.setText(source.getText("Rect")); 
button2.setText(source.getText("something")); 
button3.setText(source.getText("something")); 
button4.setText(source.getText("something")); 


As for your problem, the getText() methods do not accept params. Also, Object source = e.getSource();, Objects do not have a getText() method. You will have to cast source down to the specific type of Component to use the getText() method.
Was This Post Helpful? 1
  • +
  • -

#20 ryclegman  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 83
  • Joined: 24-February 10

Re: help Displaying shapes after button is clicked

Posted 08 April 2010 - 05:40 PM

View Postmacosxnerd101, on 08 April 2010 - 04:34 PM, said:

In the future, please specifically describe your errors, including the specific code that triggers it.

button1.setText(source.getText("Rect")); 
button2.setText(source.getText("something")); 
button3.setText(source.getText("something")); 
button4.setText(source.getText("something")); 


As for your problem, the getText() methods do not accept params. Also, Object source = e.getSource();, Objects do not have a getText() method. You will have to cast source down to the specific type of Component to use the getText() method.


okay and how might i get this to work.... if someone could show or post an example of 1 frame and 1 button doing this it would be greatly appreciated because i am having such a hard time at understanding this..
Was This Post Helpful? 0
  • +
  • -

#21 macosxnerd101  Icon User is online

  • Games, Graphs, and Auctions
  • member icon




Reputation: 12186
  • View blog
  • Posts: 45,250
  • Joined: 27-December 08

Re: help Displaying shapes after button is clicked

Posted 08 April 2010 - 05:45 PM

Can you explain exactly what you are trying to do?
Was This Post Helpful? 0
  • +
  • -

#22 ryclegman  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 83
  • Joined: 24-February 10

Re: help Displaying shapes after button is clicked

Posted 08 April 2010 - 05:47 PM

View Postmacosxnerd101, on 08 April 2010 - 04:45 PM, said:

Can you explain exactly what you are trying to do?

this is exactly what i am trying to do :

Quote

Write a program with four buttons: “Show Rectangle”, “Show Oval”, “Show Arc”, “Show Polygon” When you click a button, a corresponding panel (RectPanel, OvalPanel, ArcPanel and PolyPanel) is shown in the frame.

Was This Post Helpful? 0
  • +
  • -

#23 macosxnerd101  Icon User is online

  • Games, Graphs, and Auctions
  • member icon




Reputation: 12186
  • View blog
  • Posts: 45,250
  • Joined: 27-December 08

Re: help Displaying shapes after button is clicked

Posted 08 April 2010 - 05:56 PM

So to do this, I would use CardLayout. You will specifically find the CardLayout show(Container, String) method helpful, as you will want to show the specific panel on button click.

So there is no need to modify the button texts at all.
Was This Post Helpful? 0
  • +
  • -

#24 ryclegman  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 83
  • Joined: 24-February 10

Re: help Displaying shapes after button is clicked

Posted 08 April 2010 - 06:00 PM

View Postmacosxnerd101, on 08 April 2010 - 04:56 PM, said:

So to do this, I would use CardLayout. You will specifically find the CardLayout show(Container, String) method helpful, as you will want to show the specific panel on button click.

So there is no need to modify the button texts at all.

shouldnt it be easy enough just to post the word into the default frame that changes depending on what you click? instead of changing it all?
Was This Post Helpful? 0
  • +
  • -

#25 macosxnerd101  Icon User is online

  • Games, Graphs, and Auctions
  • member icon




Reputation: 12186
  • View blog
  • Posts: 45,250
  • Joined: 27-December 08

Re: help Displaying shapes after button is clicked

Posted 08 April 2010 - 06:02 PM

You can modify the text of a JComponent like a JLabel, which would be easier. However, if your assignment is to change JPanels according to button clicks, CardLayout is the way to go.
Was This Post Helpful? 0
  • +
  • -

#26 ryclegman  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 83
  • Joined: 24-February 10

Re: help Displaying shapes after button is clicked

Posted 08 April 2010 - 06:06 PM

View Postmacosxnerd101, on 08 April 2010 - 05:02 PM, said:

You can modify the text of a JComponent like a JLabel, which would be easier. However, if your assignment is to change JPanels according to button clicks, CardLayout is the way to go.

kk well im going to try and stick with what i have because that what we have learned so far... ha okay so im am kinda on a short dead line for this assignment.. what do i need to change and fix up to get this thing finaly finished.
Was This Post Helpful? 0
  • +
  • -

#27 macosxnerd101  Icon User is online

  • Games, Graphs, and Auctions
  • member icon




Reputation: 12186
  • View blog
  • Posts: 45,250
  • Joined: 27-December 08

Re: help Displaying shapes after button is clicked

Posted 08 April 2010 - 06:09 PM

View Postmacosxnerd101, on 08 April 2010 - 09:02 PM, said:

You can modify the text of a JComponent like a JLabel, which would be easier.

You don't need 4 JPanels then if you aren't going to use CardLayout, and you will need a JLabel. Beyond that, I think you have enough information to make a good-faith attempt at a revision.
Was This Post Helpful? 0
  • +
  • -

#28 ryclegman  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 83
  • Joined: 24-February 10

Re: help Displaying shapes after button is clicked

Posted 08 April 2010 - 06:12 PM

View Postmacosxnerd101, on 08 April 2010 - 05:09 PM, said:

View Postmacosxnerd101, on 08 April 2010 - 09:02 PM, said:

You can modify the text of a JComponent like a JLabel, which would be easier.

You don't need 4 JPanels then if you aren't going to use CardLayout, and you will need a JLabel. Beyond that, I think you have enough information to make a good-faith attempt at a revision.

okay... i believe though that my instructor does want 4 jpanels but i could be wrong, and what i have was a good-faith attemp at this. i am utterly lost now.. ha im truthfuly about ready to give up but i know i shouldnt... i wish is was as easy as if button1 == what was clicked then panel5== Rectpanel.......


CAN someone please help me!!!

This post has been edited by ryclegman: 08 April 2010 - 06:22 PM

Was This Post Helpful? 0
  • +
  • -

#29 macosxnerd101  Icon User is online

  • Games, Graphs, and Auctions
  • member icon




Reputation: 12186
  • View blog
  • Posts: 45,250
  • Joined: 27-December 08

Re: help Displaying shapes after button is clicked

Posted 08 April 2010 - 06:25 PM

Well, if you are required to use the 4 JPanels, then CardLayout is your best bet. Once you set the Layout of the main JPanel to CardLayout, you can add the other 4 JPanels to the CardLayout object. Then, just change JPanels on button clicks using the show() method I mentioned above.

Quote

and what i have was a good-faith attemp at this.

I think you missed the part where I said revision. :) I feel that I've helped you the best I can without correcting your code for you or holding your hand through it. Now it is up to you to attempt a revision.

I will provide a simple CardLayout example, however, as it sounds like you haven't covered it. This example shows how to set up GUI layout using CardLayout, flipping Container every 5 seconds. Beyond that, the methods you will need are covered in more detail in the API.

class MyFrame extends JFrame{
    
     private JPanel main;
     private JPanel[] cards;
     private CardLayout deck;
     private int count = 0;

     public MyFrame(){
         this.setSize(300,300); 
         this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

         //instantiate the main panel and CardLayout at same time
         //then set the layout of main to the CardLayout object
         main = new JPanel((deck = new CardLayout()));

         cards = new JPanel[3];
         for(int i = 0; i < cards.length; i++){
             cards[i] = new JPanel();
             cards[i].add(new JLabel(i + "")); //display some filler text
             deck.addLayoutComponent(cards[i], i + ""); //add the JPanel to the CardLayout
          }

         this.add(main);
         this.setVisible(true);

         Timer t = new Timer(5000, new ActionListener(){
            public void actionPerformed(ActionEvent e){

                 count = (count+1)%cards.length;
                 deck.show(main, count + "");
             }
         });
     }//end constructor
}//end class


Was This Post Helpful? 0
  • +
  • -

#30 NeoTifa  Icon User is offline

  • NeoTifa Codebreaker, the Scourge of Devtester
  • member icon





Reputation: 4099
  • View blog
  • Posts: 18,184
  • Joined: 24-September 08

Re: help Displaying shapes after button is clicked

Posted 09 April 2010 - 07:32 AM

View Postmacosxnerd101, on 08 April 2010 - 06:34 PM, said:

In the future, please specifically describe your errors, including the specific code that triggers it.

button1.setText(source.getText("Rect")); 
button2.setText(source.getText("something")); 
button3.setText(source.getText("something")); 
button4.setText(source.getText("something")); 


As for your problem, the getText() methods do not accept params. Also, Object source = e.getSource();, Objects do not have a getText() method. You will have to cast source down to the specific type of Component to use the getText() method.



Thanks. It's been a while >_>
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2