Results 1 to 3 of 3
  1. #1
    shin777 is offline Member
    Join Date
    May 2013
    Posts
    43
    Rep Power
    0

    Default What am I doing wrong? GUI

    Java Code:
    // Import the libraries
    
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    
    public class ColorFactory extends JFrame
    {
    	// Constants
    	public final int WIDTH = 500;
    	public final int HEIGHT = 300;
    	
    	// Objects
    	private JLabel message;
    	private Container contentPane;
    	private JPanel topPanel;
    	private JPanel bottomPanel;
    	private JButton redButton;
    	private JButton yellowButton;
    	private JButton orangeButton;
    	private JRadioButton greenButton;
    	private JRadioButton blueButton;
    	private JRadioButton cyanButton;
    	private ButtonGroup radioButtonGroup;
    	
    	// Create constructor
    	
    	public ColorFactory()
    	{
    		setTitle("Color Factory");
    		setSize(WIDTH, HEIGHT);
    		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		
    		// Set up content pane and set to border layout
    		contentPane = getContentPane();
    		contentPane.setLayout(new BorderLayout());
    		
    		// Call method to build North panel
    		buildTopPanel();
    		contentPane.add(topPanel, BorderLayout.NORTH);
    		
    		// Call method to build SOUTH panel and add
    		buildBottomPanel();
    		contentPane.add(bottomPanel, BorderLayout.SOUTH);
    		
    		// Create our message
    		message = new JLabel("Top buttons change the panel color " +
    			"and bottom radio buttons change the text color.");
    		contentPane.add(message, BorderLayout.CENTER);
          
          // Display the window
          setVisible(true);
    		
    	}
    	
    	/**
    		build top panel
    	*/
       
       private void buildTopPanel()
    	{
    		topPanel = JPanel();
    		topPanel.setBackground(Color.white);
    		topPanel.setLayout(new FlowLayout());
    		
    		// Create 3 J buttons
    		redButton = new JButton("Red");
    		redButton.setBackground(Color.red);
    		
    		orangeButton = new JButton("Orange");
    		orangeButton.setBackground(Color.orange);
    		
    		yellowButton = new JButton("Yellow");
    		yellowButton.setBackground(Color.yellow);
    		
    	
    		
    		// Add listeners
    		redButton.addActionListener(new ButtonListener());
    		orangeButton.addActionListener(new ButtonListener());
    		yellowButton.addActionListener(new ButtonListener());
    		
    		// Add the J buttons to the panel
    		topPanel.add(redButton);
    		topPanel.add(orangeButton);
    		topPanel.add(yellowButton);
          
          
    	}
    	
    	/**
    		Build Bottom panel
    	*/
    		
    	private void buildBottomPanel()
    	{
    		bottomPanel = JPanel();
    		bottomPanel.setBackground(Color.white);
    		bottomPanel.setLayout(new FlowLayout());
    		
    		// Create 3 radio buttons
    		greenButton = new JRadioButton("Green");
    		greenButton.setForeground(Color.green);
    		
    		blueButton = new JRadioButton("Blue");
    		blueButton.setForeground(Color.blue);
    		
    		cyanButton = new JRadioButton("Cyan");
    		cyanButton.setForeground(Color.cyan);
    		
    		// Create a radio group and add 4 buttons
    		radioButtonGroup = new ButtonGroup();
    		radioButtonGroup.add(greenButton);
    		radioButtonGroup.add(blueButton);
    		radioButtonGroup.add(cyanButton);
    		
    		// Add listeners
    		greenButton.addActionListener(new RadioButtonListener());
    		blueButton.addActionListener(new RadioButtonListener());
    		cyanButton.addActionListener(new RadioButtonListener());
    		
    		// Add the radio buttons to the panel
    		bottomPanel.add(greenButton);
    		bottomPanel.add(blueButton);
    		bottomPanel.add(cyanButton);
          
          
          
    	}		
       
       private class ButtonListener implements ActionListener
    	{
    		public void actionPerformed(ActionEvent e)
    		{
             // get the action command
    	      String actionCommand = e.getActionCommand();
             
             // Action setting  
             if (actionCommand.equals("Red"))
             {
                topPanel.setBackground(Color.red);
             }   
             
             else if (actionCommand.equals("Orange"))
             {
                topPanel.setBackground(Color.orange);
             }
             
             else if (actionCommand.equals("Yellow"))
             {
                topPanel.setBackground(Color.yellow);
             }   
    		}	
       }
          
       private class RadioButtonListener implements ActionListener
    	{
    		public void actionPerformed(ActionEvent e)
    		{
    		   // get the action command
    	      String actionCommand = e.getActionCommand();
             
             // Action setting  
             if (actionCommand.equals("Green"))
             {
                bottomPanel.setForeground(Color.green);
             }   
             
             else if (actionCommand.equals("Blue"))
             {
                bottomPanel.setForeground(Color.blue);
             }
             
             else if (actionCommand.equals("Cyan"))
             {
                bottomPanel.setForeground(Color.cyan);
             }
    		}
    		
    	}
    	
    	//Excute the main method
    	public static void main(String [] args)
    	{
    		new ColorFactory();
    	}
    	
    	
    }
    trying to make end result look like this
    What am I doing wrong? GUI-111.jpg
    when i compile it, i get error message saying can't find symbol on topPanel = JPanel(); and bottomPanel = JPanel();

  2. #2
    finest is offline Member
    Join Date
    Nov 2013
    Posts
    2
    Rep Power
    0

    Default Re: What am I doing wrong? GUI

    you need to create an instance of JPanel() as below for both:

    topPanel = new JPanel()

  3. #3
    shin777 is offline Member
    Join Date
    May 2013
    Posts
    43
    Rep Power
    0

    Default Re: What am I doing wrong? GUI

    Quote Originally Posted by finest View Post
    you need to create an instance of JPanel() as below for both:

    topPanel = new JPanel()
    thank you. now that you mentioned it, i forgot new in front of panel. now it compiles. :)
    i fixed few other errors as well. thx for the help. :)

Similar Threads

  1. Replies: 4
    Last Post: 06-11-2013, 02:37 AM
  2. I can't find anything wrong with this but somehow it's wrong.
    By Biscuit Tickler in forum New To Java
    Replies: 2
    Last Post: 09-12-2012, 10:28 PM
  3. What am I doing wrong?
    By PhilK in forum New To Java
    Replies: 7
    Last Post: 04-15-2012, 09:22 PM
  4. What did I do wrong?
    By GoingThroAPhase in forum New To Java
    Replies: 4
    Last Post: 04-03-2010, 05:51 AM
  5. what is wrong?
    By pinguxxx in forum Advanced Java
    Replies: 3
    Last Post: 07-15-2009, 01:33 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •