Welcome to Hackers Alliance!

[JAVA] Making a window

  • Archived

    The forum is archived and used for testing. It is currently read-only to visitors.
    It has been upgraded from vBulletin 3.8.x to XenForo for security purposes and future-proofing. Proprietary code and modifications (such as code database and HA bot) are broken with XenForo and will stay only with vBulletin.

Double_0_negative

Hackers Alliance Veteran
Administrator
Sep 18, 2010
444
8
0
hello guys. today im going to show you how to make a simpe java program that makes a window and adds some stuff to it using the swing class.

although knowing how to make console programs will help with this, its not really needed.

if you dont have a compiler, i recomend downloading Bluej. its a lightweight 5.5mb compiler/IDE do you can make your programs
BlueJ - Download

once its installed, make a new project, and click add class. call it whatever. then double click on your class that you just added, and it will open the editor, and then delete all the code in the editor.



the first thing we need to do to start is to import some things
the only thing we need right now is the swing class, so lets import it
Code:
import javax.swing.*;
now lets create our class

Code:
public class HaSwing
{

public static void main(String[] args){
now we need to create our window, set some defualt actions, tell it what size to be, and tell it to be visible

Code:
JFrame frame = new JFrame("Hackers Alliance Java Tut");
frame.setVisible(true);
frame.setSize(700,430);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
next, we need to add some stuff to it. lets add some text and a button. but first, we need to create a panel for it all to be on
Code:
JPanel panel = new JPanel();
frame.add(panel);

JLabel label = new JLabel("Hello world");
JButton button = new JButton("Click me!");
now we have added a panel and created a button and some text. next, we need to add these to our panel
Code:
panel.add(label);
panel.add(button);
}
}
and your done! close the editor, and you should be back at the main screen. right click on your class, and click where it says void main(string[] args). click okay, and your code will run. you should get a window that looks like this



heres the full code so you can copy and paste
Code:
import javax.swing.*;
public class HaSwing
{

public static void main(String[] args){

JFrame frame = new JFrame("Hackers Alliance Java Tut");
frame.setVisible(true);
frame.setSize(700,430);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);


JPanel panel = new JPanel();
frame.add(panel);

JLabel label = new JLabel("Hello world");
JButton button = new JButton("Click me!");


panel.add(label);
panel.add(button);
}
}
 
This site has been archived and is no longer accepting new content.

About us

  • Hackers Alliance is a small community forum about gaming and console hacking. Join our humble community to share ideas, game cheats, mods, and be part of an amazing growing community!

Quick Navigation

User Menu