Jump to content
  • Advertisement

Archived

This topic is now archived and is closed to further replies.

jen6677

Need help with Java-Swing

This topic is 5841 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi Im trying to go through Suns tutorial on Swing. I''m having a little trouble tho. I can do most of the basics like createbuttons and add them to a pane, and then add the pane to a frame. But what i want to do is... to be able to click on a button on the gui which will bring me to another pane. So for example something like, having the options to click button "red" , and if i click on button "red" the current pane is replaced with a new pane (lets say with new pane would contain the label "red"). I tried to add code inside the event listener for the button but i havent had any success. Could you please give me a simple example or explantion on how I can go from 1 pane to another buy clicking a button. thnx- I hope the Q was clear. Here is the code that is availabe from Suns tutorial. Perhaps you could tell me where to modify this code. THX
  
import javax.swing.*;          //This is the final package name.

//import com.sun.java.swing.*; //Used by JDK 1.2 Beta 4 and all

                               //Swing releases before Swing 1.1 Beta 3.

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

public class SwingApplication {
    private static String labelPrefix = "Number of button clicks: ";
    private int numClicks = 0;

    public Component createComponents() {
        final JLabel label = new JLabel(labelPrefix + "0    ");

        JButton button = new JButton("I''m a Swing button!");
        button.setMnemonic(KeyEvent.VK_I);
        button.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                numClicks++;
                label.setText(labelPrefix + numClicks);
            }
        });
        label.setLabelFor(button);

        /*
         * An easy way to put space between a top-level container
         * and its contents is to put the contents in a JPanel
         * that has an "empty" border.
         */
        JPanel pane = new JPanel();
        pane.setBorder(BorderFactory.createEmptyBorder(
                                        30, //top

                                        30, //left

                                        10, //bottom

                                        30) //right

                                        );
        pane.setLayout(new GridLayout(0, 1));
        pane.add(button);
        pane.add(label);

        return pane;
    }

    public static void main(String[] args) {
        try {
            UIManager.setLookAndFeel(
                UIManager.getCrossPlatformLookAndFeelClassName());
        } catch (Exception e) { }

        //Create the top-level container and add contents to it.

        JFrame frame = new JFrame("SwingApplication");
        SwingApplication app = new SwingApplication();
        Component contents = app.createComponents();
        frame.getContentPane().add(contents, BorderLayout.CENTER);

        //Finish setting up the frame, and show it.

        frame.addWindowListener(new WindowAdapter() {
            public void windowClosing(WindowEvent e) {
                System.exit(0);
            }
        });
        frame.pack();
        frame.setVisible(true);
    }
}
  

Share this post


Link to post
Share on other sites
Advertisement

    
public Component createComponents()
{
//...

button.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
myMethod()
}
});
//...

}



private void myMethod()
{
// the stuff you wanna do when the button is clicked

}


[edited by - misterX on July 25, 2002 7:21:34 AM]

[edited by - misterX on July 25, 2002 7:23:26 AM]

Share this post


Link to post
Share on other sites
JTabbedPane might do what you want - or at least you could open up the source for JTabbedPane and see how Sun do things...

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!