Sie sind auf Seite 1von 22

Creating Frame using JFrame Sample Code

import javax.swing.JFrame;

/* This class demonstrate the usage of JFrame */


public class FrameDemo {

public void createAndShowGUI(){

/* Create new frame with title */


JFrame frame = new JFrame("Frame Demo");

/* Close frame when exit button is clicked */


frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

/* Set the frame size to 300 x 100 */


frame.setSize(300,100);

/* Make the frame visible on screen */


frame.setVisible(true);
}

/* This class launch frame on screen */


public static void main(String[] args) {

FrameDemo frameDemo = new FrameDemo();


frameDemo.createAndShowGUI();

}
Creating Panel using JPanel Sample Code

import java.awt.Color;
import javax.swing.JFrame;
import javax.swing.JPanel;

public class PanelDemo {

public void createAndShowGUI(){

/* Create new frame with title */


JFrame frame = new JFrame("Panel Demo");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(300,100);
frame.setLayout(null);

/* Creating new Panel with yellow and size 100 * 50 */


JPanel panel = new JPanel();
panel.setSize(100, 50);
panel.setBackground(Color.YELLOW);

/* Adding panel into frame */


frame.add(panel);
frame.setVisible(true);
}

public static void main(String[] args) {

/* Launch frame on screen */


PanelDemo apps = new PanelDemo();
apps.createAndShowGUI();

}
Setting Layout using Flow Layout Sample Code

import java.awt.FlowLayout;
import javax.swing.JButton;
import javax.swing.JFrame;

/* This class demonstrate the usage of FlowLayout manager */


public class FlowLayoutDemo {

public void createAndLoadGUI() {

/* Creating GUI components */


JButton button1 = new JButton("Button 1");
JButton button2 = new JButton("Long-Named Button 2");
JButton button3 = new JButton("Button 1");
JFrame frame = new JFrame("FlowLayout Demo");

/* Setting layout to frame */


frame.setLayout(new FlowLayout());

/* Adding button to frame */


frame.add(button1);
frame.add(button2);
frame.add(button3);

frame.setSize(300, 100);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}

public static void main(String[] args) {

/* Launch frame on screen */


FlowLayoutDemo flDemo = new FlowLayoutDemo();
flDemo.createAndLoadGUI();
}

}
Setting Layout using Border Layout Sample Code

import java.awt.BorderLayout;
import javax.swing.JButton;
import javax.swing.JFrame;

/* This class demonstrate the usage of BorderLayout manager */


public class BorderLayoutDemo {

public void createAndLoadGUI() {

/* Creating GUI components */


JButton btnNorth = new JButton("North");
JButton btnSouth = new JButton("South");
JButton btnCenter = new JButton("Center");
JButton btnEast = new JButton("East");
JButton btnWest = new JButton("West");
JFrame frame = new JFrame("BorderLayout Demo");

/* Adding button to frame using default layout */


frame.add(btnNorth, BorderLayout.NORTH);
frame.add(btnSouth, BorderLayout.SOUTH);
frame.add(btnCenter, BorderLayout.CENTER);
frame.add(btnEast, BorderLayout.EAST);
frame.add(btnWest, BorderLayout.WEST);

frame.setSize(300, 200);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
public static void main(String[] args) {

/* Launch frame on screen */


BorderLayoutDemo blDemo = new BorderLayoutDemo();
blDemo.createAndLoadGUI();
}
}
Setting Layout using Grid Layout Sample Code

import java.awt.GridLayout;
import javax.swing.JButton;
import javax.swing.JFrame;

public class GridLayoutDemo {

public void createAndLoadGUI() {

/* Creating GUI components */


JButton btn1 = new JButton("B1");
JButton btn2 = new JButton("B2");
JButton btn3 = new JButton("B3");
JButton btn4 = new JButton("B4");
JButton btn5 = new JButton("B5");
JButton btn6 = new JButton("B6");
JFrame frame = new JFrame("GridLayout Demo");

/* Setting layout to frame */


frame.setLayout(new GridLayout(3,2));

/* Adding button to frame using default layout */


frame.add(btn1);
frame.add(btn2);
frame.add(btn3);
frame.add(btn4);
frame.add(btn5);
frame.add(btn6);

frame.setSize(300, 200);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
public static void main(String[] args) {

/* Launch frame on screen */


GridLayoutDemo glDemo = new GridLayoutDemo();
glDemo.createAndLoadGUI();

}
}
Creating Button using JButton Sample Code

import javax.swing.JButton;
import javax.swing.JFrame;

public class ButtonDemo {

public void createAndShowGUI(){

/* Creating GUI components */


JFrame frame = new JFrame("Button Demo");
JButton button = new JButton("Sample");

/* Adding button to frame */


frame.add(button);

frame.setSize(300,100);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}

public static void main(String[] args) {

/* Launch frame on screen */


ButtonDemo btnDemo = new ButtonDemo();
btnDemo.createAndShowGUI();

}
Creating Label using JLabel Sample Code

package gui;

import java.awt.Color;
import java.awt.GridLayout;

import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.Icon;

public class LabelDemo {

public void createAndShowGUI(){

/* Creating GUI components */


JFrame frame = new JFrame("Label Demo");
JLabel lblDefault = new JLabel("Default Alignment");
JLabel lblRight = new JLabel("Right Alignment");
JLabel lblCenter = new JLabel("Center Alignment");
Icon icon = new ImageIcon("back.png");
JLabel lblIcon = new JLabel("Label with Icon",
icon, JLabel.CENTER);

frame.setLayout(new GridLayout(4,1));

/* Setting alignment for label */


lblRight.setHorizontalAlignment(JLabel.RIGHT);
lblCenter.setHorizontalAlignment(JLabel.CENTER);

/* Set color for label */


lblRight.setBackground(Color.CYAN);
lblRight.setOpaque(true);
lblCenter.setBackground(Color.ORANGE);
lblCenter.setOpaque(true);
lblIcon.setBackground(Color.WHITE);
lblIcon.setOpaque(true);
/* Adding label to frame */
frame.add(lblDefault);
frame.add(lblIcon);
frame.add(lblRight);
frame.add(lblCenter);

frame.setSize(300,200);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}

public static void main(String[] args) {

/* Launch frame on screen */


LabelDemo lblDemo = new LabelDemo();
lblDemo.createAndShowGUI();

}
Creating Text Field using JTextField Sample Code

import java.awt.FlowLayout;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextField;

public class TextFieldDemo {

public void createAndShowGUI(){

/* Creating GUI components */


JFrame frame = new JFrame("Text Field Demo");
JLabel lblTitle = new JLabel("Title : ");
JLabel lblVenue = new JLabel("Venue : ");
JTextField txtTitle = new JTextField("Enter title ", 20);
JTextField txtVenue = new JTextField(20);

frame.setLayout(new FlowLayout());

/* Adding label to frame */


frame.add(lblTitle);
frame.add(txtTitle);
frame.add(lblVenue);
frame.add(txtVenue);

frame.setSize(300,100);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}

public static void main(String[] args) {

/* Launch frame on screen */


TextFieldDemo tfDemo = new TextFieldDemo();
tfDemo.createAndShowGUI();

}
Creating Text Area using JTextArea Sample Code

import java.awt.FlowLayout;

import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextArea;

public class TextAreaDemo {

public void createAndShowGUI(){

/* Creating GUI components */


JFrame frame = new JFrame("Text Area Demo");
JLabel lblComments = new JLabel("Leave Comments : ");
JTextArea txtComments = new JTextArea(5,30);

frame.setLayout(new FlowLayout());

/* Adding label to frame */


frame.add(lblComments);
frame.add(txtComments);

frame.setSize(500,150);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}

public static void main(String[] args) {

/* Launch frame on screen */


TextAreaDemo taDemo = new TextAreaDemo();
taDemo.createAndShowGUI();

}
}
Creating List Box using JList Sample Code

import java.awt.FlowLayout;

import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JList;

public class ListDemo {

public void createAndShowGUI(){

String transport[] = {"Car", "Bus", "Taxi",


"Bicycle", "Motorcycle"};

/* Creating GUI components */


JFrame frame = new JFrame("List Box Demo");
JLabel lblTransport = new JLabel("Select a Person");

JList<String> listTransport = new JList<String>(transport);

frame.setLayout(new FlowLayout());

/* Adding label to frame */


frame.add(lblTransport);
frame.add(listTransport);

frame.setSize(300,150);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}

public static void main(String[] args) {

/* Launch frame on screen */


ListDemo lstDemo = new ListDemo();
lstDemo.createAndShowGUI();

}
Creating Combo Box using JComboBox Sample Code

import java.awt.FlowLayout;

import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;

public class ComboBoxDemo {

public void createAndShowGUI(){

String transport[] = {"Car", "Bus", "Taxi",


"Bicycle", "Motorcycle"};

/* Creating GUI components */


JFrame frame = new JFrame("Combo Box Demo");
JLabel lblTransport = new JLabel("Select a Person");

JComboBox<String> cmbTransport =
new JComboBox<String>(transport);

frame.setLayout(new FlowLayout());

/* Adding label to frame */


frame.add(lblTransport);
frame.add(cmbTransport);

frame.setSize(300,150);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}

public static void main(String[] args) {

/* Launch frame on screen */


ComboBoxDemo cmbDemo = new ComboBoxDemo();
cmbDemo.createAndShowGUI();

}
Creating Check Box using JCheckBox Sample Code

import java.awt.FlowLayout;
import javax.swing.JCheckBox;
import javax.swing.JFrame;

public class CheckBoxDemo {

public void createAndShowGUI(){

/* Creating GUI components */


JFrame frame = new JFrame("Check Box Demo");
JCheckBox chkCar = new JCheckBox("Car", true);
JCheckBox chkMotorcycle = new JCheckBox("Motorcycle");
JCheckBox chkBike = new JCheckBox("Bicycle");

frame.setLayout(new FlowLayout());

/* Adding check box to frame */


frame.add(chkCar);
frame.add(chkMotorcycle);
frame.add(chkBike);

frame.setSize(300,100);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}

public static void main(String[] args) {

/* Launch frame on screen */


CheckBoxDemo chkDemo = new CheckBoxDemo();
chkDemo.createAndShowGUI();

}
Creating Radio Button using JRadioButton Sample Code

import java.awt.FlowLayout;
import javax.swing.ButtonGroup;
import javax.swing.JFrame;
import javax.swing.JRadioButton;

public class RadioButtonDemo {

public void createAndShowGUI(){

/* Creating GUI components */


JFrame frame = new JFrame("Radio Button Demo");
JRadioButton rbCar = new JRadioButton("Car", true);
JRadioButton rbMotorcycle = new JRadioButton("Motorcycle");
JRadioButton rbBicycle = new JRadioButton("Bicycle");

/* Grouping the radio button to allow unique selection */


ButtonGroup group = new ButtonGroup();
group.add(rbBicycle);
group.add(rbCar);
group.add(rbMotorcycle);

frame.setLayout(new FlowLayout());

/* Adding radio button to frame */


frame.add(rbCar);
frame.add(rbMotorcycle);
frame.add(rbBicycle);

frame.setSize(300,100);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
public static void main(String[] args) {

/* Launch frame on screen */


RadioButtonDemo rbDemo = new RadioButtonDemo();
rbDemo.createAndShowGUI();

}
Creating Menu Bar using JMenuBar Sample Code

import javax.swing.JFrame;
import javax.swing.JMenuBar;

/* This class demonstrate the usage of JMenuBar */


public class MenuBarDemo {

/* Declaration of GUI objects */


JFrame frame;
JMenuBar menuBar;

public void createAndShowGUI() {

/* Creating GUI object */


frame = new JFrame("Menu Bar - Demo");
menuBar = new JMenuBar();

/* Adding menu bar to the frame */


frame.setJMenuBar(menuBar);

frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(200,100);
frame.setVisible(true);
}

/* Main entry point of program. This will launch the GUI */


public static void main(String[] args) {

MenuBarDemo mbDemo = new MenuBarDemo();


mbDemo.createAndShowGUI();
}

}
Creating Menu using JMenu Sample Code

import javax.swing.JFrame;
import javax.swing.JMenuBar;
import javax.swing.JMenu;

/* This class demonstrate the usage of JMenu */


public class MenuDemo {

/* GUI object declaration */


JFrame frame;
JMenuBar menuBar;
JMenu menuFile, menuEdit;

public void createAndShowGUI() {

/* Creating GUI object */


frame = new JFrame("Menu - Demo");
menuBar = new JMenuBar();

/* Creating menu with File and Edit as title */


menuFile = new JMenu("File");
menuEdit = new JMenu("Edit");

/* Adding menu bar to the frame */


frame.setJMenuBar(menuBar);

/* Adding menu object to menubar */


menuBar.add(menuFile);
menuBar.add(menuEdit);

frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(300,100);
frame.setVisible(true);
}
/* Main entry point of program. This will launch the GUI */
public static void main(String[] args) {

MenuDemo menuDemo = new MenuDemo();


menuDemo.createAndShowGUI();
}
}
Creating Menu Item using JMenuItem and JCheckboxMenuItem
Sample Code

import javax.swing.JCheckBoxMenuItem;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;

/* This class demonstrate the usage of JMenuItem */


public class MenuItemDemo {

/* Declare GUI component object */


JFrame frame;
JMenuBar menuBar;
JMenu menuFile, menuEdit;
JMenuItem miNew, miOpen, miPrint, miExit;
JCheckBoxMenuItem miCheck;

public void createAndShowGUI() {

/* Construct frame object */


frame = new JFrame("Menu - Demo");

/* Construct menu bar object */


menuBar = new JMenuBar();

menuFile = new JMenu("File");


menuEdit = new JMenu("Edit");

/* Construct menu item */


miNew = new JMenuItem ("New");
miOpen = new JMenuItem ("Open");
miPrint = new JMenuItem ("Print");
miExit = new JMenuItem ("Exit");
/* Construct menu item with check box */
miCheck = new JCheckBoxMenuItem("Check it", false);

/* Add menu bar to frame */


frame.setJMenuBar(menuBar);

/* Add menu to menu bar */


menuBar.add(menuFile);
menuBar.add(menuEdit);

/* Add menu item to menu */


menuFile.add(miNew);
menuFile.add(miOpen);
menuFile.add(miCheck);
menuFile.add(miPrint);
menuFile.add(miExit);

frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(300,100);
frame.setVisible(true);
}

/* Main entry point of program. This will launch the GUI */


public static void main(String[] args) {

MenuItemDemo menuDemo = new MenuItemDemo();


menuDemo.createAndShowGUI();
}

Das könnte Ihnen auch gefallen