import javax.swing.JFrame;
public class Window {
public static void main(String[] args) {
JFrame jf = new JFrame("Hello, World!");
jf.setBounds(0, 0, 800, 600);
// Remember, the method show() is deprecated
jf.setVisible(true);
}
}
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException e) {
e.printStackTrace();
}
FlowLayout.LEFT, RIGHT, CENTER, LEADING, TRAILING
)
import javax.swing.JFrame;
import javax.swing.JButton;
import java.awt.FlowLayout;
public class FlowLayoutDemo {
public static void main(String[] args) {
JFrame jf = new JFrame("Hello, World!");
jf.setBounds(0, 0, 800, 600);
jf.setLayout(new FlowLayout());
jf.add(new JButton("button 1"));
jf.add(new JButton("button 2"));
jf.add(new JButton("button 3"));
// Remember, the method show() is deprecated
jf.setVisible(true);
}
}
import javax.swing.JFrame;
import javax.swing.JButton;
import java.awt.BorderLayout;
public class BorderLayoutDemo {
public static void main(String[] args) {
JFrame jf = new JFrame("Hello, World!");
jf.setBounds(0, 0, 800, 600);
jf.setLayout(new BorderLayout());
jf.add(new JButton("north"), BorderLayout.NORTH);
jf.add(new JButton("south"), BorderLayout.SOUTH);
jf.add(new JButton("center"), BorderLayout.CENTER);
jf.add(new JButton("west"), BorderLayout.WEST);
jf.add(new JButton("east"), BorderLayout.EAST);
// Remember, the method show() is deprecated
jf.setVisible(true);
}
}
import javax.swing.JFrame;
import javax.swing.JButton;
import java.awt.GridLayout;
public class GridLayoutDemo {
public static void main(String[] args) {
JFrame jf = new JFrame("Hello, World!");
jf.setBounds(0, 0, 800, 600);
jf.setLayout(new GridLayout(3, 2));
jf.add(new JButton("(1, 1)"));
jf.add(new JButton("(1, 2)"));
jf.add(new JButton("(2, 1)"));
jf.add(new JButton("(2, 2)"));
jf.add(new JButton("(3, 1)"));
jf.add(new JButton("(3, 2)"));
// Remember, the method show() is deprecated
jf.setVisible(true);
}
}
import javax.swing.JFrame;
import javax.swing.JButton;
import javax.swing.JOptionPane;
import java.awt.BorderLayout;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
public class EventDemo {
class AlertAction implements ActionListener {
private JFrame parent;
AlertAction(JFrame parent) {
this.parent = parent;
}
@Override public void actionPerformed(ActionEvent e) {
JOptionPane.showMessageDialog(parent, "information", "Button Pressed!!", JOptionPane.INFORMATION_MESSAGE);
}
}
public EventDemo() {
JFrame jf = new JFrame("Hello, World!");
JButton jb = new JButton("Click Me!");
jf.setBounds(0, 0, 800, 600);
jf.setLayout(new BorderLayout());
jf.add(jb, BorderLayout.CENTER);
jb.addActionListener(new AlertAction(jf));
// Remember, the method show() is deprecated
jf.setVisible(true);
}
public static void main(String[] args) {
new EventDemo();
}
}
import javax.swing.JFrame;
import javax.swing.JComponent;
import java.awt.BorderLayout;
import java.awt.Graphics;
import java.awt.Font;
public class TextDemo {
class GraphicPane extends JComponent {
public GraphicPane() {
super();
}
@Override public void paint(Graphics g) {
g.setFont(new Font(Font.SANS_SERIF, Font.ITALIC, 14));
g.drawString("Hello, World!", 30, 30);
}
}
public TextDemo() {
JFrame jf = new JFrame("Hello, World!");
GraphicPane gp = new GraphicPane();
jf.setBounds(0,0, 800, 600);
jf.setLayout(new BorderLayout());
jf.add(gp, BorderLayout.CENTER);
// Remember, the method show() is deprecated
jf.setVisible(true);
}
public static void main(String[] args) {
new TextDemo();
}
}
import javax.swing.JFrame;
import javax.swing.JComponent;
import java.awt.BorderLayout;
import java.awt.Graphics;
import java.awt.Font;
import java.awt.Color;
public class ColorDemo {
class GraphicPane extends JComponent {
public GraphicPane() {
super();
}
@Override public void paint(Graphics g) {
g.setFont(new Font(Font.SANS_SERIF, Font.ITALIC, 14));
g.setColor(Color.ORANGE);
g.drawString("Hello, World! (orange)", 30, 30);
g.setColor(new Color(250, 100, 120));
g.drawString("Hello, World! (red ... almost)", 80, 80);
}
}
public ColorDemo() {
JFrame jf = new JFrame("Hello, World!");
GraphicPane gp = new GraphicPane();
jf.setBounds(0,0, 800, 600);
jf.setLayout(new BorderLayout());
jf.add(gp, BorderLayout.CENTER);
// Remember, the method show() is deprecated
jf.setVisible(true);
}
public static void main(String[] args) {
new ColorDemo();
}
}
import javax.swing.JFrame;
import javax.swing.JComponent;
import java.awt.BorderLayout;
import java.awt.Graphics;
import java.awt.Font;
import java.awt.Color;
public class ShapeDemo {
class GraphicPane extends JComponent {
public GraphicPane() {
super();
}
@Override public void paint(Graphics g) {
//// line
g.setColor(Color.BLACK);
// drawLine(int x1, int y1, int x2, int y2)
g.drawLine(0, 0, 100, 100);
//// ovals
g.setColor(new Color(250, 100, 120));
// drawOval(int x, int y, int width, int height)
g.drawOval(30, 30, 100, 200);
// same, but it fills the oval with the current color
g.fillOval(300, 30, 100, 200);
//// Rectangle
g.setColor(Color.BLUE);
// drawRect(int x, int y, int width, int height)
g.drawRect(30, 300, 100, 200);
// fillRect(int x, int y, int width, int height)
g.fillRect(300, 300, 100, 200);
}
}
public ShapeDemo() {
JFrame jf = new JFrame("Hello, World!");
GraphicPane gp = new GraphicPane();
jf.setBounds(0,0, 800, 600);
jf.setLayout(new BorderLayout());
jf.add(gp, BorderLayout.CENTER);
// Remember, the method show() is deprecated
jf.setVisible(true);
}
public static void main(String[] args) {
new ShapeDemo();
}
}
import javax.swing.JFrame;
import javax.swing.JComponent;
import javax.imageio.ImageIO;
import java.io.File;
import java.io.IOException;
import java.awt.BorderLayout;
import java.awt.Graphics;
import java.awt.image.BufferedImage;
public class ImageDemo {
class GraphicPane extends JComponent {
private BufferedImage bi;
private JFrame parent;
public GraphicPane(JFrame parent) {
this.parent = parent;
try {
// reads a file
this.bi = ImageIO.read(new File("planes.jpg"));
} catch (IOException ioe) {
System.err.println("Could not load image");
}
}
@Override public void paint(Graphics g) {
// drawImage(Image img, int x, int y, ImageObserver observer)
g.drawImage(bi, 0, 0, parent);
}
}
public ImageDemo() {
JFrame jf = new JFrame("Hello, World!");
GraphicPane gp = new GraphicPane(jf);
jf.setBounds(0,0, 800, 600);
jf.setLayout(new BorderLayout());
jf.add(gp, BorderLayout.CENTER);
jf.setVisible(true);
}
public static void main(String[] args) {
new ImageDemo();
}
}