29 June 2014
26 June 2014
Sample Frame
A Simple Example of Frame by Notepad
Program
import java.awt.*;
import javax.swing.*;
import java.util.*;
public class Grid{
private JFrame f;
private Panel p1,p2,p3,p4;
public Grid(){
f = new JFrame("Hello Grid");
p1= new Panel();
p2= new Panel();
p3= new Panel();
p4= new Panel();
}
public void launchFrame(){
f.setVisible(true);
f.setSize(600,600);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setLayout(new GridLayout(2,2));
p1.setBackground(Color.red);
f.add(p1);
p2.setBackground(Color.blue);
f.add(p2);
p3.setBackground(Color.yellow);
f.add(p3);
p4.setBackground(Color.green);
f.add(p4);
}
public static void main(String args[]){
Grid Mad = new Grid();
Mad.launchFrame();
}
}
Save it as Grid.java ( same as class name)
in cmd prompt type "javac Grid.java"
then "java Grid"
Output Will As
19 June 2014
Java Swing
Java Swing
The java swing package lets you make GUI components for your java applications and is platform independent.The Swing library is built on top of the Java Abstract Widget Toolkit (AWT), an older, platform dependent GUI toolkit.You can use the GUI components like button , textbox etc from the library and do not have to create the components from scratch.
the swing class hierarchy
All components in swing are JComponent which can be added to container classes.
what are container classes ?
Container classes are classes that can have other components on it. So for creating a GUI, we need at least one Container object Three types of containers
- Panel : It is a pure container and is not a window in itself. The sole purpose of a Panel is to organize the components on to a window.
- Frame : It is a fully functioning window with its own title and icons.
- Dialog : It can be thought of as a pop-up window that pops out when message has to be displayed. It is not a fully functioning window like the Frame.
Assignment: To learn designing GUI in Java
Step 1) Copy the following code into an editor
Step 1) Copy the following code into an editor
1
2
3
4
5
6
7
8
9
10
11
| import javax.swing.*;class gui{ public static void main(String args[]){ JFrame frame = new JFrame("My First GUI"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setSize(300,300); JButton button = new JButton("Press"); frame.getContentPane().add(button); // Adds Button to content pane of frame frame.setVisible(true); }} |
Step 2) Save , Compile and Run the code.
Step 3) Now lets Add a Button to our frame. Copy following code into an editor
Step 3) Now lets Add a Button to our frame. Copy following code into an editor
1
2
3
4
5
6
7
8
9
10
11
| import javax.swing.*; class gui{ public static void main(String args[]){ JFrame frame = new JFrame("My First GUI"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setSize(300,300); JButton button1 = new JButton("Press"); frame.getContentPane().add(button1); frame.setVisible(true); }} |
Step 4) Execute the code. You will get a big button --
Step 5) How about adding two buttons. Copy the following code into an editor.
1
2
3
4
5
6
7
8
9
10
11
12
13
| import javax.swing.*;class gui{ public static void main(String args[]){ JFrame frame = new JFrame("My First GUI"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setSize(300,300); JButton button1 = new JButton("Button 1"); JButton button2 = new JButton("Button 2"); frame.getContentPane().add(button1); frame.getContentPane().add(button2); frame.setVisible(true); }} |
Step 6)Save , Compile , & Run the program.
Step 7) Unexpected output = ? Buttons are getting overlapped.
Enter
Step 7) Unexpected output = ? Buttons are getting overlapped.
Enter
Subscribe to:
Posts (Atom)