import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class ButtonWindow {
public static void main(String[] args) {
Frame frame = new Frame("My Window");
frame.setSize(400, 300);
Button button = new Button("Click Me!");
frame.add(button);
button.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
System.out.println("Button clicked!");
}
});
frame.setVisible(true);
}
}
import java.awt.*;
public class BorderLayoutPanel {
public static void main(String[] args) {
Frame frame = new Frame("My Window");
frame.setSize(400, 300);
Panel panel = new Panel();
panel.setLayout(new BorderLayout());
frame.add(panel);
frame.setVisible(true);
}
}
import java.awt.*;
public class BorderLayoutPanelComponents {
public static void main(String[] args) {
Frame frame = new Frame("My Window");
frame.setSize(400, 300);
Panel panel = new Panel();
panel.setLayout(new BorderLayout());
panel.add(new Label("North"), BorderLayout.NORTH);
panel.add(new Label("South"), BorderLayout.SOUTH);
panel.add(new Label("East"), BorderLayout.EAST);
panel.add(new Label("West"), BorderLayout.WEST);
panel.add(new Label("Center"), BorderLayout.CENTER);
frame.add(panel);
frame.setVisible(true);
}
}
import java.awt.*;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
public class CanvasDrawing {
public static void main(String[] args) {
Frame frame = new Frame("My Window");
frame.setSize(400, 300);
Canvas canvas = new Canvas();
canvas.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
Graphics g = canvas.getGraphics();
g.setColor(Color.RED);
g.fillOval(e.getX(), e.getY(), 10, 10);
}
});
frame.add(canvas);
frame.setVisible(true);
}
}
import java.awt.*;
public class CustomComponent extends Component {
@Override
public void paint(Graphics g) {
super.paint(g);
g.setColor(Color.BLUE);
g.drawRect(0, 0, 100, 100);
}
}
import java.awt.*;
public class FontExample {
public static void main(String[] args) {
Font font = new Font("Arial", Font.BOLD, 16);
FontMetrics metrics = getFontMetrics(font);
int width = metrics.stringWidth("Hello World!");
int height = metrics.getHeight();
}
}
import java.awt.*;
import java.awt.image.ImageObserver;
public class ImageExample {
public static void main(String[] args) {
Image image = Toolkit.getDefaultToolkit().getImage("myImage.png");
class MyImageObserver implements ImageObserver {
@Override
public boolean imageUpdate(Image img, int infoflags, int x, int y, int width, int height) {
return false; // Return true if image is completely loaded
}
}
MyImageObserver obs = new MyImageObserver();
image.getWidth(obs);
image.getHeight(obs);
}
}
import java.awt.*;
import java.awt.image.ColorModel;
public class ColorExample {
public static void main(String[] args) {
Color color = new Color(255, 0, 0); // Red
ColorModel model = ColorModel.getRGBdefault();
int[] components = model.getComponents(color, null, 0);
}
}
import java.awt.*;
public class MenuExample {
public static void main(String[] args) {
Frame frame = new Frame("My Window");
frame.setSize(400, 300);
MenuBar menuBar = new MenuBar();
Menu menu = new Menu("File");
MenuItem item1 = new MenuItem("New");
MenuItem item2 = new MenuItem("Open");
menu.add(item1);
menu.add(item2);
menuBar.add(menu);
frame.setMenuBar(menuBar);
frame.setVisible(true);
}
}
import java.awt.*;
public class ScrollbarExample {
public static void main(String[] args) {
Frame frame = new Frame("My Window");
frame.setSize(400, 300);
Scrollbar scrollbar = new Scrollbar(Scrollbar.HORIZONTAL, 0, 10, 0, 100);
frame.add(scrollbar);
frame.setVisible(true);
}
}
import java.awt.*;
public class TextAreaExample {
public static void main(String[] args) {
Frame frame = new Frame("My Window");
frame.setSize(400, 300);
TextArea textArea = new TextArea();
frame.add(textArea);
frame.setVisible(true);
}
}
import java.awt.*;
public class TextFieldExample {
public static void main(String[] args) {
Frame frame = new Frame("My Window");
frame.setSize(400, 300);
TextField textField = new TextField();
frame.add(textField);
frame.setVisible(true);
}
}
import java.awt.*;
public class ChoiceExample {
public static void main(String[] args) {
Frame frame = new Frame("My Window");
frame.setSize(400, 300);
Choice choice = new Choice();
choice.add("Item 1");
choice.add("Item 2");
choice.add("Item 3");
frame.add(choice);
frame.setVisible(true);
}
}
import java.awt.*;
public class CheckboxExample {
public static void main(String[] args) {
Frame frame = new Frame("My Window");
frame.setSize(400, 300);
CheckboxGroup group = new CheckboxGroup();
Checkbox checkbox1 = new Checkbox("Option 1", group, true);
Checkbox checkbox2 = new Checkbox("Option 2", group, false);
frame.add(checkbox1);
frame.add(checkbox2);
frame.setVisible(true);
}
}
import java.awt.*;
public class RadioButtonExample {
public static void main(String[] args) {
Frame frame = new Frame("My Window");
frame.setSize(400, 300);
ButtonGroup group = new ButtonGroup();
RadioButton radio1 = new RadioButton("Option 1", group, true);
RadioButton radio2 = new RadioButton("Option 2", group, false);
frame.add(radio1);
frame.add(radio2);
frame.setVisible(true);
}
}
import java.awt.*;
public class ListExample {
public static void main(String[] args) {
Frame frame = new Frame("My Window");
frame.setSize(400, 300);
List list = new List();
list.add("Item 1");
list.add("Item 2");
list.add("Item 3");
frame.add(list);
frame.setVisible(true);
}
}
import javax.swing.*;
public class JOptionPaneExample {
public static void main(String[] args) {
String name = JOptionPane.showInputDialog("What is your name?");
System.out.println("Hello " + name + "!");
}
}
import javax.swing.*;
public class JDialogExample {
public static void main(String[] args) {
JDialog dialog = new JDialog();
dialog.setTitle("My Dialog");
dialog.setModal(true);
dialog.setSize(300, 200);
JButton button = new JButton("OK");
dialog.add(button);
dialog.setVisible(true);
}
}
import java.awt.*;
import java.awt.geom.Ellipse2D;
public class CustomShapeExample {
public static void main(String[] args) {
Graphics2D g2d = (Graphics2D) getGraphics();
Ellipse2D circle = new Ellipse2D.Double(100, 100, 100, 100);
g2d.draw(circle);
}
}
import java.awt.*;
import java.awt.geom.AffineTransform;
public class AffineTransformExample {
public static void main(String[] args) {
Graphics2D g2d = (Graphics2D) getGraphics();
AffineTransform transform = new AffineTransform();
transform.translate(100, 100);
transform.rotate(Math.PI / 4);
g2d.setTransform(transform);
g2d.drawRect(0, 0, 100, 100);
}
}
import java.awt.*;
public class GridLayoutExample {
public static void main(String[] args) {
Frame frame = new Frame("My Window");
frame.setSize(400, 300);
Panel panel = new Panel();
panel.setLayout(new GridLayout(3, 3));
for (int i = 0; i < 9; i++) {
panel.add(new Button("Button " + i));
}
frame.add(panel);
frame.setVisible(true);
}
}
import java.awt.*;
public class FlowLayoutExample {
public static void main(String[] args) {
Frame frame = new Frame("My Window");
frame.setSize(400, 300);
Panel panel = new Panel();
panel.setLayout(new FlowLayout());
for (int i = 0; i < 10; i++) {
panel.add(new Button("Button " + i));
}
frame.add(panel);
frame.setVisible(true);
}
}
import net.miginfocom.swing.MigLayout;
public class MigLayoutExample {
public static void main(String[] args) {
JFrame frame = new JFrame("My Window");
frame.setSize(400, 300);
frame.setLayout(new MigLayout("debug"));
frame.add(new JLabel("Name:"), "align label");
frame.add(new JTextField(), "growx");
frame.add(new JLabel("Age:"), "align label");
frame.add(new JTextField(), "growx");
frame.setVisible(true);
}
}
import java.awt.*;
public class BorderLayoutConstraintsExample {
public static void main(String[] args) {
Frame frame = new Frame("My Window");
frame.setSize(400, 300);
frame.setLayout(new BorderLayout());
frame.add(new Button("North"), BorderLayout.NORTH);
frame.add(new Button("South"), BorderLayout.SOUTH);
frame.add(new Button("East"), BorderLayout.EAST);
frame.add(new Button("West"), BorderLayout.WEST);
frame.add(new Button("Center"), BorderLayout.CENTER);
frame.setVisible(true);
}
}
import java.awt.*;
public class CardLayoutExample {
public static void main(String[] args) {
Frame frame = new Frame("My Window");
frame.setSize(400, 300);
CardLayout cardLayout = new CardLayout();
Panel panel = new Panel(cardLayout);
panel.add("Card 1", new Button("Card 1"));
panel.add("Card 2", new Button("Card 2"));
frame.add(panel);
cardLayout.show(panel, "Card 2");
frame.setVisible(true);
}
}
import java.awt.*;
public class GridBagLayoutExample {
public static void main(String[] args) {
Frame frame = new Frame("My Window");
frame.setSize(400, 300);
GridBagLayout gridBagLayout = new GridBagLayout();
GridBagConstraints constraints = new GridBagConstraints();
Panel panel = new Panel(gridBagLayout);
constraints.gridx = 0;
constraints.gridy = 0;
panel.add(new Button("Button 1"), constraints);
constraints.gridx = 1;
constraints.gridy = 1;
panel.add(new Button("Button 2"), constraints);
frame.add(panel);
frame.setVisible(true);
}
}
import java.awt.*;
public class SpringLayoutExample {
public static void main(String[] args) {
Frame frame = new Frame("My Window");
frame.setSize(400, 300);
SpringLayout springLayout = new SpringLayout();
Panel panel = new Panel(springLayout);
Spring gap = Spring.constant(5);
springLayout.putConstraint(SpringLayout.WEST, panel, gap, SpringLayout.WEST, frame);
springLayout.putConstraint(SpringLayout.NORTH, panel, gap, SpringLayout.NORTH, frame);
springLayout.putConstraint(SpringLayout.SOUTH, panel, gap, SpringLayout.SOUTH, frame);
springLayout.putConstraint(SpringLayout.EAST, panel, gap, SpringLayout.EAST, frame);
frame.add(panel);
frame.setVisible(true);
}
}
import javax.swing.*;
public class TabbedPaneExample {
public static void main(String[] args) {
JFrame frame = new JFrame("My Window");
frame.setSize(400, 300);
JTabbedPane tabbedPane = new JTabbedPane();
tabbedPane.add("Tab 1", new JPanel());
tabbedPane.add("Tab 2", new JPanel());
frame.add(tabbedPane);
frame.setVisible(true);
}
}
import javax.swing.*;
public class JSplitPaneExample {
public static void main(String[] args) {
JFrame frame = new JFrame("My Window");
frame.setSize(400, 300);
JSplitPane splitPane = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT);
splitPane.setTopComponent(new JPanel());
splitPane.setBottomComponent(new JPanel());
frame.add(splitPane);
frame.setVisible(true);
}
}
import javax.swing.*;
public class JScrollPaneExample {
public static void main(String[] args) {
JFrame frame = new JFrame("My Window");
frame.setSize(400, 300);
JScrollPane scrollPane = new JScrollPane(new JPanel());
frame.add(scrollPane);
frame.setVisible(true);
}
}
import javax.swing.*;
public class JTableExample {
public static void main(String[] args) {
JFrame frame = new JFrame("My Window");
frame.setSize(400, 300);
String[][] data = {{"1", "John Doe"}, {"2", "Jane Doe"}};
String[] columnNames = {"ID", "Name"};
JTable table = new JTable(data, columnNames);
frame.add(new JScrollPane(table));
frame.setVisible(true);
}
}