Jump to content

  • Log In with Google      Sign In   
  • Create Account

We're offering banner ads on our site from just $5!

1. Details HERE. 2. GDNet+ Subscriptions HERE. 3. Ad upload HERE.


[?][JAVA] JFrame/Swing


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
2 replies to this topic

#1 0Circle0   Members   -  Reputation: 343

Like
0Likes
Like

Posted 27 January 2013 - 06:19 AM

Compiles just fine and runs. Shortened it down to just the one button.  I'd like to know 2 things. Am I on the right track so far. And how do I start drawing? I am stumped as where to go from here. I haven't been one to be able to learn off the, what ever you call those books or wiki links that tell you what every option does (really early in the morning and my brain may give out on me smile.png ) I have always been a learn by example person. So I have been spending a lot of time on YouTube looking at tutorials but I can't figure out where to go from here.

 

I am trying to convert my Web Applet into a runnable application. Starting from scratch no reused code from my web applet.

 

import java.awt.event.*;
import javax.swing.*;
import java.awt.*;
public class SprCre3XP
{
    public static void CreateGUI()
    {
        //Create JFrame
        JFrame frame = new JFrame("Sprite Creator 3");
        frame.setSize(800, 508);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        
        //Create Menu Bar
        JMenuBar menubar = new JMenuBar();
        
        //Add Menu Bar to Frame
        frame.setJMenuBar(menubar);
        
        //Create Menu
        JMenu file = new JMenu("File");
        JMenu help = new JMenu("Help");
        
        //Add Menus to Menu Bar
        menubar.add(file);
        menubar.add(help);
        
        //Create Menu Items with ActionListener()
        JMenuItem saveimg = new JMenuItem("Save");
        saveimg.addActionListener(new ActionListener()
        {
            public void actionPerformed(ActionEvent e)
            {
            }
        });
        file.add(saveimg);
        JMenuItem exit = new JMenuItem("Exit");
        exit.addActionListener(new ActionListener()
        {
            public void actionPerformed(ActionEvent e)
            {
                System.exit(0);
            }
        });
        JMenuItem about = new JMenuItem("About");
        about.addActionListener(new ActionListener()
        {
            public void actionPerformed(ActionEvent e)
            {
            }
        });
        
        //Add Menu items to Menu
        file.add(exit);
        help.add(about);
        
        //Create a JLabel??
        //Create JPanel and color it white
        JPanel panel = new JPanel();
        panel.setBackground(Color.white);
        //I like placing items where I want thank you layout not needed. Waste of programming too.
        panel.setLayout(null);
        JLabel label = new JLabel(); //Not sure what JLabel does here
        panel.add(label);            //But lets add it anyways lol
        
        //Lets add buttons each with their own ActionListener
        JButton MaleB = new JButton("Male");
        MaleB.setBounds(10, 125, 90, 20);
        MaleB.addActionListener(new ActionListener()
        {
            public void actionPerformed(ActionEvent e)
            {
                //System.out.print("Pressed");//works
            }
        });
        panel.add(MaleB);
        
        //add the JPanel panel to the JFrame frame and setVisible
        frame.getContentPane().add(panel);
        //frame.pack(); //Why you make window so small? guessing that is what pack is?
        frame.setVisible(true);
    }
    public static void main (String[] args)
    {
        //Creates the GUI Obviously
        CreateGUI();
    }
    //Where to go from here?
}
 

Sprite Creator 3 VX & XP

WARNING: I edit my posts constantly.


Sponsor:

#2 SHC   Members   -  Reputation: 124

Like
2Likes
Like

Posted 27 January 2013 - 09:17 AM

Just add it to a frame and you can run it well. Add a main method to the applet and create it.

 

public class MyApplet extends Applet {

    //........ Applet code

    public static void main(String[] args){
        JFrame f = new JFrame("MyApplication");
        MyApplet apl = new MyApplet();
        f.add(apl);
        f.setSize(640, 480);
        f.setLocationRelativeTo(null);
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        f.setVisible(true);

        // init the applet
     
        apl.start();
        apl.init();
    }

}

 

 

This allows the same class to be run as both applet and application.


Harsha...ch


#3 0Circle0   Members   -  Reputation: 343

Like
0Likes
Like

Posted 27 January 2013 - 11:52 AM

All I had to do was literally add that code and add

from

Back_Ground = getImage(this.getClass().getResource(ImageLocation + "Background/background.png"));

to

Back_Ground = Toolkit.getDefaultToolkit().getImage(this.getClass().getResource(ImageLocation + "Background/background.png"));

 

and that was it. You are awesome. Thanks a lot, I didn't know you could put the applet on a frame. Guess it makes sense now that I think about it.


Sprite Creator 3 VX & XP

WARNING: I edit my posts constantly.





Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS