Results 1 to 3 of 3

Thread: JFrame help

  1. #1
    mbschutlz97 is offline Member
    Join Date
    Jan 2014
    Posts
    30
    Rep Power
    0

    Default JFrame help

    I'm trying to make a program with two buttons... when you click the first a label shows a red square when you click the second it shows a green square... right now i have the imageicons as a comment because when they weren't a comment and i ran the program i saw nothing... the same thing happens when i add label2 to panel3
    Java Code:
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    public class GUI3{
    	public static void main(String[] args){
    	//frame
    	JFrame frame = new JFrame("Test");
    	frame.setSize(600,400);
    	frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    	frame.setVisible(true);
    	//panels
    	JPanel panel1 = new JPanel();
    	JPanel panel2 = new JPanel();
    	JPanel panel3 = new JPanel();
    	panel1.setLayout(new BoxLayout(panel1, BoxLayout.LINE_AXIS));
    	//labels
    	JLabel label1 = new JLabel("Test",SwingConstants.CENTER);
    	final JLabel label2 = new JLabel();
    	//buttons
    	JButton button1 = new JButton("red");
    	JButton button2 = new JButton("green");
    	//pictures
    	//ImageIcon red = new ImageIcon("/images/red.jpg");
    	//ImageIcon green = new ImageIcon("images/green.jpg");
    	//event
    	button1.addActionListener(new ActionListener(){
    			public void actionPerformed(ActionEvent e){
    			label2.setText("red");
    			}
    		});
    	//adding
    	panel2.add(button1);
    	panel2.add(button2);
    	//panel3.add(label2);
    	panel1.add(panel2);
    	panel1.add(panel3);
    	frame.add(label1,BorderLayout.NORTH);
    	frame.add(panel1);
    	}
    }

  2. #2
    mbschutlz97 is offline Member
    Join Date
    Jan 2014
    Posts
    30
    Rep Power
    0

    Default Re: JFrame help

    i just tried to add an action listener for button2 and when i run it nothing shows up

  3. #3
    jim829 is online now Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,525
    Rep Power
    5

    Default Re: JFrame help

    Try changing the text on label1 and not label2 (since you haven't added label2 to the panel). But you will have to declare label1 final since it is a local variable.

    Regards,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

Similar Threads

  1. Click in JFrame Titlebar sets the JFrame Opaque
    By tim8w in forum AWT / Swing
    Replies: 1
    Last Post: 02-02-2013, 07:29 AM
  2. Replies: 3
    Last Post: 11-27-2011, 05:18 AM
  3. Replies: 5
    Last Post: 06-14-2010, 06:10 PM
  4. Passing data from one JFrame to another JFrame
    By tarami in forum New To Java
    Replies: 3
    Last Post: 08-06-2009, 05:44 PM
  5. Replies: 4
    Last Post: 04-22-2009, 11:02 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
  •