1. BorderExample
Ans.
import java.awt.*;
import javax.swing.*;
public class BorderExample {
private JFrame f;
private JButton bn, bs, bw, be, bc;
public BorderExample() {
f = new JFrame("Border Layout");
bn = new JButton("Button 1");
bc = new JButton("Button 2");
bw = new JButton("Button 3");
bs = new JButton("Button 4");
be = new JButton("Button 5");
}
public void launchFrame() {
f.add(bn, BorderLayout.NORTH);
f.add(bs, BorderLayout.SOUTH);
f.add(bw, BorderLayout.WEST);
f.add(be, BorderLayout.EAST);
f.add(bc, BorderLayout.CENTER);
f.setSize(400,200);
f.setVisible(true);
}
public static void main(String args[]) {
BorderExample guiWindow2 = new BorderExample();
guiWindow2.launchFrame();
}
}
Ans.
import java.awt.*;
import javax.swing.*;
public class BorderExample {
private JFrame f;
private JButton bn, bs, bw, be, bc;
public BorderExample() {
f = new JFrame("Border Layout");
bn = new JButton("Button 1");
bc = new JButton("Button 2");
bw = new JButton("Button 3");
bs = new JButton("Button 4");
be = new JButton("Button 5");
}
public void launchFrame() {
f.add(bn, BorderLayout.NORTH);
f.add(bs, BorderLayout.SOUTH);
f.add(bw, BorderLayout.WEST);
f.add(be, BorderLayout.EAST);
f.add(bc, BorderLayout.CENTER);
f.setSize(400,200);
f.setVisible(true);
}
public static void main(String args[]) {
BorderExample guiWindow2 = new BorderExample();
guiWindow2.launchFrame();
}
}
2. FlowExample
Ans
import java.awt.*;
import javax.swing.*;
public class FlowExample {
private JFrame f;
private JButton b1;
private JButton b2;
private JButton b3;
private JButton b4;
private JButton b5;
public FlowExample() {
f = new JFrame("GUI example");
b1 = new JButton("Button 1");
b2 = new JButton("Button 2");
b3 = new JButton("Button 3");
b4 = new JButton("Button 4");
b5 = new JButton("Button 5");
}
public void launchFrame() {
f.setLayout(new FlowLayout());
f.add(b1);
f.add(b2);
f.add(b3);
f.add(b4);
f.add(b5);
f.pack();
f.setVisible(true);
}
public static void main(String args[]) {
FlowExample guiWindow = new FlowExample();
guiWindow.launchFrame();
}
}
3. FrameExample
Ans
import java.awt.*;
public class FrameExample {
private Frame f;
public FrameExample() {
f = new Frame("Hello Out There!");
}
public void launchFrame() {
f.setSize(170,170);
f.setBackground(Color.blue);
f.setVisible(true);
}
public static void main(String args[]) {
FrameExample guiWindow = new FrameExample();
guiWindow.launchFrame();
}
}
4. GridExample
Ans
import java.awt.*;
import javax.swing.*;
public class GridExample {
private JFrame f;
private JButton b1, b2, b3, b4, b5;
public GridExample() {
f = new JFrame("Grid Example");
b1 = new JButton("Button 1");
b2 = new JButton("Button 2");
b3 = new JButton("Button 3");
b4 = new JButton("Button 4");
b5 = new JButton("Button 5");
}
public void launchFrame() {
f.setLayout (new GridLayout(3,2));
f.add(b1);
f.add(b2);
f.add(b3);
f.add(b4);
f.add(b5);
f.pack();
f.setVisible(true);
}
public static void main(String args[]) {
GridExample grid = new GridExample();
grid.launchFrame();
}
}
5. HelloWorldSwing
Ans
import javax.swing.*;
public class HelloWorldSwing {
private static void createAndShowGUI() {
JFrame frame = new JFrame("HelloWorldSwing");
//Set up the window.
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JLabel label = new JLabel("Hello World");
// Add Label
frame.add(label);
frame.setSize(300,200);
// Display Window
frame.setVisible(true);}
public static void main(String[] args) {
javax.swing.SwingUtilities.invokeLater(new Runnable() {
//Schedule for the event-dispatching thread:
//creating,showing this app's GUI.
public void run() {createAndShowGUI();}
});
}
}
No comments:
Post a Comment