64
Tugas Pemograman 2 Komponen Swing MATA KULIAH : PEMOGRAMAN 2 DOSEN PENGAMPU : ADI DEWANTO DISUSUN OLEH : Claudia Oktaviani SP (14520241005) Agung Subastian (14520241009) Rita Septiana (14520241017) Maulidina Achmad (14520241027) Arif Sulistya (14520241036) PROGRAM STUDI PENDIDIKAN TEKNIK INFORMATIKA JURUSAN PENDIDIKAN TEKNIK ELEKTRONIKA FAKULTAS TEKNIK

Komponen Swing_05_09_17_27_36

Embed Size (px)

DESCRIPTION

jost

Citation preview

Tugas Pemograman 2

Komponen Swing

MATA KULIAH : PEMOGRAMAN 2

DOSEN PENGAMPU : ADI DEWANTO

DISUSUN OLEH :

Claudia Oktaviani SP(14520241005)

Agung Subastian(14520241009)

Rita Septiana(14520241017)

Maulidina Achmad(14520241027)

Arif Sulistya(14520241036)

PROGRAM STUDI PENDIDIKAN TEKNIK INFORMATIKA

JURUSAN PENDIDIKAN TEKNIK ELEKTRONIKA

FAKULTAS TEKNIK

UNIVERSITAS NEGERI YOGYAKARTA

2015

1. JPanel

JPanel adalah versi swing tentang Panel kelas AWT dan menggunakan tata letak standar yang sama, FlowLayout. JPanel adalah keturunan langsung dari JComponent.

Contoh Program:

import javax.swing.*;

public class Panel {

public static void main(String[] args) {

JFrame f = new JFrame("JPanelExample");

JPanel p = new JPanel();

JButton b = new JButton("Button");

p.add(b);

f.setContentPane(p);

f.show();

f.setSize(300,100);

}

}

Tampilan:

2. JScrollPane

Scroll pane merupakan tombol penggulung yan letakkanya pasti secara horizontal maupun vertical berbeda dengan scrollbar.

Contoh Program:

import java.awt.BorderLayout;

import java.awt.GridLayout;

import javax.swing.*;

public class ScrollPane extends JFrame {

JScrollPane scrollpane;

public ScrollPane() {

super("JScrollPaneExample");

setSize(300, 200);

setDefaultCloseOperation(EXIT_ON_CLOSE);

init();

setVisible(true);

}

public void init() {

JRadioButton form[][] = new JRadioButton[12][5];

String counts[] = { "", "0-1", "2-5", "6-10", "11-100", "101+" };

String categories[] = { "Household", "Office", "Extended Family",

"Company (US)", "Company (World)", "Team", "Will",

"Birthday Card List", "High School", "Country", "Continent",

"Planet" };

JPanel p = new JPanel();

p.setSize(600, 400);

p.setLayout(new GridLayout(13, 6, 10, 0));

for (int row = 0; row < 13; row++) {

ButtonGroup bg = new ButtonGroup();

for (int col = 0; col < 6; col++) {

if (row == 0) {

p.add(new JLabel(counts[col]));

} else {

if (col == 0) {

p.add(new JLabel(categories[row - 1]));

} else {

form[row - 1][col - 1] = new JRadioButton();

bg.add(form[row - 1][col - 1]);

p.add(form[row - 1][col - 1]);

}

}

}

}

scrollpane = new JScrollPane(p);

getContentPane().add(scrollpane, BorderLayout.CENTER);

}

public static void main(String args[]) {

new ScrollPane();

}

}

Tampilan:

3. JTabbedPane

Turuanan JComponent. Yang memiliki fungsi membuat beberapa pane dalam suatu frame.

Contoh Program:

import java.awt.event.WindowAdapter;

import java.awt.event.WindowEvent;

import javax.swing.*;

public class JTabbedPaneExample extends JFrame {

public static void main( String[] argv ) {

JTabbedPaneExample myExample = new JTabbedPaneExample( "Contoh JTabbedPane" );

}

public JTabbedPaneExample( String title ) {

super( title );

setSize( 150, 150 );

addWindowListener( new WindowAdapter() {

public void windowClosing( WindowEvent we ) {

dispose();

System.exit( 0 );

}

} );

init();

pack();

setVisible( true );

}

private void init() {

JTabbedPane jtb = new JTabbedPane();

for( int i = 1; i < 4; i++ ) {

ImageIcon icon = new ImageIcon( i + ".gif" );

JTextArea jta = new JTextArea( 20, 40 );

jta.setText( "This is text within tab number " + i );

JScrollPane jsp = new JScrollPane( jta );

jtb.addTab( i + "-tab", icon, jsp );

}

getContentPane().add( jtb );

jtb.setBorder( BorderFactory.createEtchedBorder() );

}

}

Tampilan:

4. JRootPane

Root pane merupakan layer yang berada di terletak antara layered pane dan frame sehingga root pane memiliki fungsi yang sama dengan layered pane ataupun content pane dan glass pane.

Contoh Program:

import javax.swing.*;

import javax.swing.border.*;

import javax.accessibility.*;

import java.awt.*;

import java.awt.event.*;

/*

* RootLayeredPaneExample.java requires images/dukeWaveRed.gif.

*/

public class RootLayeredPaneExample extends JPanel

implements ActionListener,

MouseMotionListener {

private int[] layers = {-30000, 0, 301};

private String[] layerStrings = { "Yellow (-30000)",

"Magenta (0)",

"Cyan (301)" };

private Color[] layerColors = { Color.yellow,

Color.magenta,

Color.cyan };

private JLayeredPane layeredPane;

private JLabel dukeLabel;

private JCheckBox onTop;

private JComboBox layerList;

//Action commands

private static String ON_TOP_COMMAND = "ontop";

private static String LAYER_COMMAND = "layer";

//Adjustments to put Duke's toe at the cursor's tip.

private static final int XFUDGE = 40;

private static final int YFUDGE = 57;

//Initial layer of dukeLabel.

private static final int INITIAL_DUKE_LAYER_INDEX = 1;

public RootLayeredPaneExample(JLayeredPane layeredPane) {

super(new GridLayout(1,1));

//Create and load the duke icon.

final ImageIcon icon = createImageIcon("images/dukeWaveRed.gif");

//Create and set up the layered pane.

this.layeredPane = layeredPane;

layeredPane.addMouseMotionListener(this);

//This is the origin of the first label added.

Point origin = new Point(10, 100);

//This is the offset for computing the origin for the next label.

int offset = 35;

//Add several overlapping, colored labels to the layered pane

//using absolute positioning/sizing.

for (int i = 0; i < layerStrings.length; i++) {

JLabel label = createColoredLabel(layerStrings[i],

layerColors[i], origin);

layeredPane.add(label, new Integer(layers[i]));

origin.x += offset;

origin.y += offset;

}

//Create and add the Duke label to the layered pane.

dukeLabel = new JLabel(icon);

if (icon != null) {

dukeLabel.setBounds(15, 225,

icon.getIconWidth(),

icon.getIconHeight());

} else {

System.err.println("Duke icon not found; using black square instead.");

dukeLabel.setBounds(15, 225, 30, 30);

dukeLabel.setOpaque(true);

dukeLabel.setBackground(Color.BLACK);

}

layeredPane.add(dukeLabel,

new Integer(layers[INITIAL_DUKE_LAYER_INDEX]),

0);

//Add control pane to this JPanel.

add(createControlPanel());

}

/** Returns an ImageIcon, or null if the path was invalid. */

protected static ImageIcon createImageIcon(String path) {

java.net.URL imgURL = RootLayeredPaneExample.class.getResource(path);

if (imgURL != null) {

return new ImageIcon(imgURL);

} else {

System.err.println("Couldn't find file: " + path);

return null;

}

}

//Create and set up a colored label.

private JLabel createColoredLabel(String text,

Color color,

Point origin) {

JLabel label = new JLabel(text);

label.setVerticalAlignment(JLabel.TOP);

label.setHorizontalAlignment(JLabel.CENTER);

label.setOpaque(true);

label.setBackground(color);

label.setForeground(Color.black);

label.setBorder(BorderFactory.createLineBorder(Color.black));

label.setBounds(origin.x, origin.y, 140, 140);

return label;

}

//Create the control pane for the top of the frame.

private JPanel createControlPanel() {

onTop = new JCheckBox("Top Position in Layer");

onTop.setSelected(true);

onTop.setActionCommand(ON_TOP_COMMAND);

onTop.addActionListener(this);

layerList = new JComboBox(layerStrings);

layerList.setSelectedIndex(INITIAL_DUKE_LAYER_INDEX);

layerList.setActionCommand(LAYER_COMMAND);

layerList.addActionListener(this);

JPanel controls = new JPanel();

controls.add(layerList);

controls.add(onTop);

controls.setBorder(BorderFactory.createTitledBorder(

"Choose Duke's Layer and Position"));

return controls;

}

//Make Duke follow the cursor.

public void mouseMoved(MouseEvent e) {

dukeLabel.setLocation(e.getX()-XFUDGE, e.getY()-YFUDGE);

}

public void mouseDragged(MouseEvent e) {} //do nothing

//Handle user interaction with the check box and combo box.

public void actionPerformed(ActionEvent e) {

String cmd = e.getActionCommand();

if (ON_TOP_COMMAND.equals(cmd)) {

if (onTop.isSelected())

layeredPane.moveToFront(dukeLabel);

else

layeredPane.moveToBack(dukeLabel);

} else if (LAYER_COMMAND.equals(cmd)) {

int position = onTop.isSelected() ? 0 : -1;

layeredPane.setLayer(dukeLabel,

layers[layerList.getSelectedIndex()],

position);

}

}

/**

* Create the GUI and show it. For thread safety,

* this method should be invoked from the

* event-dispatching thread.

*/

private static void createAndShowGUI() {

//Create and set up the window.

JFrame frame = new JFrame("RootLayeredPaneExample");

frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

//Create and set up the content pane.

RootLayeredPaneExample newContentPane = new RootLayeredPaneExample(

frame.getLayeredPane());

newContentPane.setOpaque(true); //content panes must be opaque

frame.setContentPane(newContentPane);

//Display the window.

frame.setSize(new Dimension(300, 350));

frame.setVisible(true);

}

public static void main(String[] args) {

//Schedule a job for the event-dispatching thread:

//creating and showing this application's GUI.

javax.swing.SwingUtilities.invokeLater(new Runnable() {

public void run() {

createAndShowGUI();

}

});

}

}

Tampilan:

5. JInternalFrame

berfungsi seperti JFrame tapi di gunakan untuk Form Child(form terdapat form di dalamnya lagi).

Contoh Program:

import javax.swing.JInternalFrame;

import javax.swing.JDesktopPane;

import javax.swing.JMenu;

import javax.swing.JMenuItem;

import javax.swing.JMenuBar;

import javax.swing.JFrame;

import java.awt.event.*;

import java.awt.*;

public class JInternalFrameExample extends JFrame {

JDesktopPane jdpDesktop;

static int openFrameCount = 0;

public JInternalFrameExample() {

super("JInternalFrameExample");

// Make the main window positioned as 50 pixels from each edge of the

// screen.

int inset = 50;

Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();

setBounds(inset, inset, screenSize.width - inset * 2,

screenSize.height - inset * 2);

// Add a Window Exit Listener

addWindowListener(new WindowAdapter() {

public void windowClosing(WindowEvent e) {

System.exit(0);

}

});

// Create and Set up the GUI.

jdpDesktop = new JDesktopPane();

// A specialized layered pane to be used with JInternalFrames

createFrame(); // Create first window

setContentPane(jdpDesktop);

setJMenuBar(createMenuBar());

// Make dragging faster by setting drag mode to Outline

jdpDesktop.putClientProperty("JDesktopPane.dragMode",

"outline");

}

protected JMenuBar createMenuBar() {

JMenuBar menuBar = new JMenuBar();

JMenu menu = new JMenu("Frame");

menu.setMnemonic(KeyEvent.VK_N);

JMenuItem menuItem = new JMenuItem("New IFrame");

menuItem.setMnemonic(KeyEvent.VK_N);

menuItem.addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent e) {

createFrame();

}

});

menu.add(menuItem);

menuBar.add(menu);

return menuBar;

}

protected void createFrame() {

MyInternalFrame frame = new MyInternalFrame();

frame.setVisible(true);

// Every JInternalFrame must be added to content pane using JDesktopPane

jdpDesktop.add(frame);

try {

frame.setSelected(true);

} catch (java.beans.PropertyVetoException e) {

}

}

public static void main(String[] args) {

JInternalFrameExample frame = new JInternalFrameExample();

frame.setVisible(true);

}

class MyInternalFrame extends JInternalFrame {

static final int xPosition = 30, yPosition = 30;

public MyInternalFrame() {

super("IFrame #" + (++openFrameCount), true, // resizable

true, // closable

true, // maximizable

true);// iconifiable

setSize(300, 300);

// Set the window's location.

setLocation(xPosition * openFrameCount, yPosition

* openFrameCount);

}

}

}

Tampilan:

6. JSplitPane

JSplitPane adalah membagi 2 komponen dalam Frame Java,Dua Komponen grafis dibagi berdasarkan tampilan dan implementasi, dan dua komponennya dapat diubah ukurannya secara interaktif oleh user/pengguna.

Contoh Program:

import java.awt.*;

import javax.swing.*;

public class SplitPane extends JFrame

{

Container konten = getContentPane();

private JLabel lblText,lblPratinjau,lblPreviewGambar,lblGambar;

private JTextField txtText;

private JButton btnCari;

private JTextArea ta1,ta2;

private JPanel panel1,panel2,panel3,panelGambar;

private JSplitPane vSplit,hSplit1,hSplit2;

private JScrollPane pane;

private ImageIcon gambar;

//Membuat Konstruktor

public SplitPane()

{

setTitle("Membuat SplitPane");

setSize(700,500);

setVisible(true);

setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

setLocationRelativeTo(null); //Mengatur Tampilan Frame di tengah

lblText = new JLabel("Text");

lblPratinjau = new JLabel("Pratinjau");

lblPreviewGambar = new JLabel("Preview Gambar");

txtText = new JTextField(" " ,20);

btnCari = new JButton("Cari");

ta1 = new JTextArea();

ta2 = new JTextArea();

//Panel1

panel1 = new JPanel();

panel1.setLayout(new BorderLayout());

panel1.add(lblText,BorderLayout.WEST); //Mengatur lblText tampil di kiri

panel1.add(txtText,BorderLayout.CENTER); //Mengatur txtText tampil di tengah

panel1.add(btnCari,BorderLayout.EAST); //Mengatur btnCari tampil di kanan

pane = new JScrollPane();

pane.add(ta1);

//Mengatur bagian yang akan dibagi(split)

hSplit1 = new JSplitPane();

hSplit1.setOrientation(JSplitPane.VERTICAL_SPLIT);

hSplit1.setTopComponent(panel1);

hSplit1.setBottomComponent(pane);

//Panel2

panel2 = new JPanel();

panel2.setLayout(new BorderLayout());

panel2.add(lblPratinjau,BorderLayout.NORTH); //Mengatur lblPratinjau tampil di atas

panel2.add(ta2,BorderLayout.CENTER); //Mengatur TextArea2 tampil di tengah

//Panel3

panel3 = new JPanel();

panel3.setLayout(new BorderLayout());

panel3.add(lblPreviewGambar,BorderLayout.NORTH);

lblGambar = new JLabel();

lblGambar.setIcon(new ImageIcon("src/D/IMG_20150421_085004.jpg"));

panel3.add(lblGambar,BorderLayout.CENTER);

//Mengatur bagian yang akan dibagi

hSplit2 = new JSplitPane();

hSplit2.setOrientation(JSplitPane.VERTICAL_SPLIT);

hSplit2.setTopComponent(panel2);

hSplit2.setBottomComponent(panel3);

vSplit = new JSplitPane();

vSplit.setOrientation(JSplitPane.HORIZONTAL_SPLIT);

vSplit.setTopComponent(hSplit1);

vSplit.setBottomComponent(hSplit2);

konten.add(vSplit);

} //Akhir Konstruktor

public static void main(String[] ar)

{

new SplitPane();

}

}

Tampilan:

7. JLayeredPane

Merupakan wadah komponen yang membolehkan komponen-konponen didalamnya secara bersamaan

Contoh Program:

import javax.swing.*;

import javax.swing.border.*;

import javax.accessibility.*;

import java.awt.*;

import java.awt.event.*;

public class LayeredPaneDemo extends JPanel

implements ActionListener,

MouseMotionListener {

private String[] layerStrings = { "Yellow (0)", "Magenta (1)",

"Cyan (2)", "Red (3)",

"Green (4)" };

private Color[] layerColors = { Color.yellow, Color.magenta,

Color.cyan, Color.red,

Color.green };

private JLayeredPane layeredPane;

private JLabel dukeLabel;

private JCheckBox onTop;

private JComboBox layerList;

//Action commands

private static String ON_TOP_COMMAND = "ontop";

private static String LAYER_COMMAND = "layer";

//Adjustments to put Duke's toe at the cursor's tip.

private static final int XFUDGE = 40;

private static final int YFUDGE = 57;

public LayeredPaneDemo() {

setLayout(new BoxLayout(this, BoxLayout.PAGE_AXIS));

//Create and load the duke icon.

final ImageIcon icon = createImageIcon("images/dukeWaveRed.gif");

//Create and set up the layered pane.

layeredPane = new JLayeredPane();

layeredPane.setPreferredSize(new Dimension(300, 310));

layeredPane.setBorder(BorderFactory.createTitledBorder(

"Move the Mouse to Move Duke"));

layeredPane.addMouseMotionListener(this);

//This is the origin of the first label added.

Point origin = new Point(10, 20);

//This is the offset for computing the origin for the next label.

int offset = 35;

//Add several overlapping, colored labels to the layered pane

//using absolute positioning/sizing.

for (int i = 0; i < layerStrings.length; i++) {

JLabel label = createColoredLabel(layerStrings[i],

layerColors[i], origin);

layeredPane.add(label, new Integer(i));

origin.x += offset;

origin.y += offset;

}

//Create and add the Duke label to the layered pane.

dukeLabel = new JLabel(icon);

if (icon != null) {

dukeLabel.setBounds(15, 225,

icon.getIconWidth(),

icon.getIconHeight());

} else {

System.err.println("Duke icon not found; using black square instead.");

dukeLabel.setBounds(15, 225, 30, 30);

dukeLabel.setOpaque(true);

dukeLabel.setBackground(Color.BLACK);

}

layeredPane.add(dukeLabel, new Integer(2), 0);

//Add control pane and layered pane to this JPanel.

add(Box.createRigidArea(new Dimension(0, 10)));

add(createControlPanel());

add(Box.createRigidArea(new Dimension(0, 10)));

add(layeredPane);

}

/** Returns an ImageIcon, or null if the path was invalid. */

protected static ImageIcon createImageIcon(String path) {

java.net.URL imgURL = LayeredPaneDemo.class.getResource(path);

if (imgURL != null) {

return new ImageIcon(imgURL);

} else {

System.err.println("Couldn't find file: " + path);

return null;

}

}

//Create and set up a colored label.

private JLabel createColoredLabel(String text,

Color color,

Point origin) {

JLabel label = new JLabel(text);

label.setVerticalAlignment(JLabel.TOP);

label.setHorizontalAlignment(JLabel.CENTER);

label.setOpaque(true);

label.setBackground(color);

label.setForeground(Color.black);

label.setBorder(BorderFactory.createLineBorder(Color.black));

label.setBounds(origin.x, origin.y, 140, 140);

return label;

}

//Create the control pane for the top of the frame.

private JPanel createControlPanel() {

onTop = new JCheckBox("Top Position in Layer");

onTop.setSelected(true);

onTop.setActionCommand(ON_TOP_COMMAND);

onTop.addActionListener(this);

layerList = new JComboBox(layerStrings);

layerList.setSelectedIndex(2); //cyan layer

layerList.setActionCommand(LAYER_COMMAND);

layerList.addActionListener(this);

JPanel controls = new JPanel();

controls.add(layerList);

controls.add(onTop);

controls.setBorder(BorderFactory.createTitledBorder(

"Choose Duke's Layer and Position"));

return controls;

}

//Make Duke follow the cursor.

public void mouseMoved(MouseEvent e) {

dukeLabel.setLocation(e.getX()-XFUDGE, e.getY()-YFUDGE);

}

public void mouseDragged(MouseEvent e) {} //do nothing

//Handle user interaction with the check box and combo box.

public void actionPerformed(ActionEvent e) {

String cmd = e.getActionCommand();

if (ON_TOP_COMMAND.equals(cmd)) {

if (onTop.isSelected())

layeredPane.moveToFront(dukeLabel);

else

layeredPane.moveToBack(dukeLabel);

} else if (LAYER_COMMAND.equals(cmd)) {

int position = onTop.isSelected() ? 0 : 1;

layeredPane.setLayer(dukeLabel,

layerList.getSelectedIndex(),

position);

}

}

private static void createAndShowGUI() {

//Create and set up the window.

JFrame frame = new JFrame("LayeredPaneExamPle");

frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

//Create and set up the content pane.

JComponent newContentPane = new LayeredPaneDemo();

newContentPane.setOpaque(true); //content panes must be opaque

frame.setContentPane(newContentPane);

//Display the window.

frame.pack();

frame.setVisible(true);

}

public static void main(String[] args) {

//Schedule a job for the event-dispatching thread:

//creating and showing this application's GUI.

javax.swing.SwingUtilities.invokeLater(new Runnable() {

public void run() {

createAndShowGUI();

}

});

}

}

Tampilan:

8. JOptionPane

Turunan Jcomponent. Disediakan untuk mempermudah menampilkan pop- up kotak dialog.

Contoh Program:

import javax.swing.*;

public class OptionTampilan {

public static void main(String[] args) {

JOptionPane.showMessageDialog(null,"JOptionPane","JOptionPaneExample",JOptionPane.INFORMATION_MESSAGE);

}

}

Tampilan:

9. JLabel

JLabel, keturunan dari JComponent, digunakan untuk membuat label teks.

Contoh Program:

import javax.swing.*;

public class Label {

public static void main(String[] args) {

JFrame f = new JFrame("JLabelExample");

JPanel p = new JPanel();

JLabel b = new JLabel("Label");

b.setBounds(10, 10, 270, 10);

p.add(b);

f.setContentPane(p);

f.show();

f.setSize(300,100);

}

}

Tampilan :

10. JComboBox

Turunan Jcomponent. Yang berfungsi untuk memberikan list pada pilihan box

Contoh Program:

import javax.swing.*;

public class ComboBox {

public static void main(String[] args) {

JFrame f = new JFrame("JComboBoxExample");

JPanel p = new JPanel();

// JComboBox(String nama)

String[] menu = {"Nasi","Ayam","Goreng"};

JComboBox b = new JComboBox(menu);

p.add(b);

f.setContentPane(p);

f.show();

f.setSize(300,100);

}

}

Tampilan:

11. JMenuBar

JMenuBar dapat berisi beberapa JMenu ini. Setiap JMenu dapat berisi serangkaian JMenuItem 's yang dapat Anda pilih. Ayunan menyediakan dukungan untukpull-down dan menu popup.

Contoh Program:

import javax.swing.*;

public class MenuBar {

public static void main(String[] args) {

JFrame f = new JFrame("JMenuBarExample");

JMenuBar menuBar = new JMenuBar();

JMenu menu = new JMenu("Menu");

JMenuItem menuItem = new JMenuItem("Menu Item");

menuBar.add(menu);

menu.add(menuItem);

f.setContentPane(menuBar);

f.show();

f.setSize(300,100);

}

}

Tampilan :

12. JToolBar

JToolbar berisi sejumlah komponen yang jenis ini biasanya beberapa jenis tombol yang juga dapat mencakup pemisah pada komponen grup terkaitdalam toolbar.

Contoh Program :

import javax.swing.*;

import java.awt.*;

import java.awt.event.*;

public class JToolBarExample extends JFrame {

protected JTextArea textArea;

protected String newline = "\n";

public JToolBarExample() {

//Do frame stuff.

super("Contoh JToolBar");

addWindowListener(new WindowAdapter() {

public void windowClosing(WindowEvent e) {

System.exit(0);

}

});

JToolBar toolBar = new JToolBar();

toolBar.setFloatable(false);

addButtons(toolBar);

textArea = new JTextArea(5, 30);

JScrollPane scrollPane = new JScrollPane(textArea);

//Lay out the content pane.

JPanel contentPane = new JPanel();

contentPane.setLayout(new BorderLayout());

contentPane.setPreferredSize(new Dimension(400, 100));

contentPane.add(toolBar, BorderLayout.NORTH);

contentPane.add(scrollPane, BorderLayout.CENTER);

setContentPane(contentPane);

}

protected void addButtons(JToolBar toolBar) {

JButton button = null;

button = new JButton(new ImageIcon("images/left.gif"));

button.setToolTipText("This is the left button");

button.addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent e) {

displayResult("Action for first button");

}

});

toolBar.add(button);

button = new JButton(new ImageIcon("images/middle.gif"));

button.setToolTipText("This is the middle button");

button.addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent e) {

displayResult("Action for second button");

}

});

toolBar.add(button);

button = new JButton(new ImageIcon("images/right.gif"));

button.setToolTipText("This is the right button");

button.addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent e) {

displayResult("Action for third button");

}

});

toolBar.add(button);

toolBar.addSeparator();

button = new JButton("Another button");

button.addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent e) {

displayResult("Action for fourth button");

}

});

toolBar.add(button);

JTextField textField = new JTextField("A text field");

toolBar.add(textField);

}

protected void displayResult(String actionDescription) {

textArea.append(actionDescription + newline);

}

public static void main(String[] args) {

JToolBarExample frame = new JToolBarExample();

frame.pack();

frame.setVisible(true);

}

}

Tampilan :

13. JProgressBar

Progress bar menampilkan grafik yang menggambarkan beberapa lama suatu proses akan selesai.

Contoh Progam:

import javax.swing.*;

import java.awt.*;

public class Progress {

private JProgressBar JP;

private JFrame frame;

private JPanel panel;

public static void main(String[] args) {

new Progress();

}

public Progress() {

frame = new JFrame("JProgressBarExample");

panel = new JPanel();

panel.setPreferredSize(new Dimension(300, 55));

JP = new JProgressBar(0, 200);

JP.setStringPainted(true);

panel.add(JP);

frame.setDefaultCloseOperation(frame.EXIT_ON_CLOSE);

frame.setContentPane(panel);

frame.pack();

frame.setVisible(true);

Task();

}

public void Task() {

for (int i = 0; i Swing Code Examples

http://java.happycodings.com/swing/, di akses pada Kamis,30 April 2015 pukul 16.39 WIB

Judul : Lesson: Using Swing Components

https://docs.oracle.com/javase/tutorial/uiswing/components/index.html , di akses pada Jumat,1 Mei 2015 pukul 07.43 WIB