1 Replies - 237 Views - Last Post: 30 June 2012 - 03:39 AM Rate Topic: -----

#1 iEpic  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 30-June 12

Please help before i go outside and punch trees.

Posted 30 June 2012 - 02:58 AM

Been sitting here at my computer for about 13 hours and I think my eyes are bleeding.
I found a little gui editor I love called GuiGenie.
It works perfect for creating the window with the buttons and all that good stuff.
The problem is i want to click a button in my first menu and have it open my other menu i made.
I just starting programming 4 weeks ago so I'm a complete noob.
I have a feeling its messing up because of the main methods but I have no idea and 13 hours of sitting here trying millions of things is making me go crazy : )
here is what i got so far
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;

public class MyPanel extends JPanel {
    private JTextField How;
    private JLabel jcomp2;
    private JLabel jcomp3;
    private JButton jcomp4;

    public MyPanel() {
        //construct components
        How = new JTextField (1);
        jcomp2 = new JLabel ("How long were you parked?");
        jcomp3 = new JLabel ("Minutes");
        jcomp4 = new JButton ("openNewWindow");

        //adjust size and set layout
        setPreferredSize (new Dimension (315, 85));
        setLayout (null);

        //add components
        add (How);
        add (jcomp2);
        add (jcomp3);
        add (jcomp4);

        //set component bounds (only needed by Absolute Positioning)
        How.setBounds (245, 50, 60, 25);
        jcomp2.setBounds (35, 30, 185, 50);
        jcomp3.setBounds (250, 30, 60, 20);
        jcomp4.setBounds (0, 0, 315, 25);
		  
		   jcomp4.addActionListener( new ActionListener()
        {
            public void actionPerformed(ActionEvent e)
            {
		
            }
        });
    }


    public static void main (String[] args) {
        JFrame frame = new JFrame ("MyPanel");
        frame.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
        frame.getContentPane().add (new MyPanel());
        frame.pack();
        frame.setVisible (true);
    }
}



When the button is pressed, I want it to open this new window

//Generated by GuiGenie - Copyright (c) 2004 Mario Awad.
//Home Page http://guigenie.cjb.net - Check often for new versions!

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

public class MyPanel2 extends JPanel {
    private JButton jcomp1;
    private JButton jcomp2;
    private JButton jcomp3;
    private JTextField jcomp4;

    public MyPanel2() {
        //construct components
        jcomp1 = new JButton ("test1");
        jcomp2 = new JButton ("test2");
        jcomp3 = new JButton ("test3");
        jcomp4 = new JTextField (5);

        //adjust size and set layout
        setPreferredSize (new Dimension (395, 156));
        setLayout (null);

        //add components
        add (jcomp1);
        add (jcomp2);
        add (jcomp3);
        add (jcomp4);

        //set component bounds (only needed by Absolute Positioning)
        jcomp1.setBounds (20, 45, 100, 25);
        jcomp2.setBounds (135, 60, 100, 25);
        jcomp3.setBounds (260, 35, 100, 25);
        jcomp4.setBounds (105, 115, 100, 25);
    }


    public static void main (String[] args) {
        JFrame frame = new JFrame ("MyPanel");
        frame.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
        frame.getContentPane().add (new MyPanel2());
        frame.pack();
        frame.setVisible (true);
    }
}



If anyone could help I would appreciate it greatly!!
I have a lot of respect for you pros out there because if you are a pro at this, you are probably smarter than 99.9% of the world.
This stuff hurts my brain.

Is This A Good Question/Topic? 0
  • +

Replies To: Please help before i go outside and punch trees.

#2 GregBrannon  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2197
  • View blog
  • Posts: 5,224
  • Joined: 10-September 10

Re: Please help before i go outside and punch trees.

Posted 30 June 2012 - 03:39 AM

The 13 hours at your computer got you pretty close. You're using (or attempting to use) the more advanced approach of adding a listener to a button using an anonymous inner class. That's a good approach, maybe not right for every situation, but I've helped you to the finish line without significantly changing what you'd started.

It's also okay that you have a main() method in both classes. Some people do that routinely as a way to test their classes independently. Notice that the actionPerformed() method in the button's action listener is similar to the main() method in the second class. The major difference is the removal of the exit on close as I don't think you want to exit the application when the second window is closed.

Let us know if you have any questions.

import java.awt.*;
import java.awt.event.*;

import javax.swing.*;
import javax.swing.event.*;

public class MyPanel extends JPanel
{
	private JTextField How;
	private JLabel jcomp2;
	private JLabel jcomp3;
	private JButton jcomp4;

	public MyPanel() {
		//construct components
		How = new JTextField (1);
		jcomp2 = new JLabel ("How long were you parked?");
		jcomp3 = new JLabel ("Minutes");
		jcomp4 = new JButton ("openNewWindow");
		jcomp4.addActionListener( new ActionListener()
		{
			public void actionPerformed( ActionEvent ae )
			{
				JFrame frame = new JFrame( "Frame2" );
				frame.getContentPane().add (new MyPanel2());
				frame.pack();
				frame.setVisible (true);
			}
		});

		//adjust size and set layout
		setPreferredSize (new Dimension (315, 85));
		setLayout (null);

		//add components
		add (How);
		add (jcomp2);
		add (jcomp3);
		add (jcomp4);

		//set component bounds (only needed by Absolute Positioning)
		How.setBounds (245, 50, 60, 25);
		jcomp2.setBounds (35, 30, 185, 50);
		jcomp3.setBounds (250, 30, 60, 20);
		jcomp4.setBounds (0, 0, 315, 25);
	}

	public static void main (String[] args) {
		JFrame frame = new JFrame ("MyPanel2");
		frame.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
		frame.getContentPane().add (new MyPanel());
		frame.pack();
		frame.setVisible (true);
	}
}

Was This Post Helpful? 1
  • +
  • -

Page 1 of 1