Tampilkan postingan dengan label Sample is Program. Tampilkan semua postingan
Tampilkan postingan dengan label Sample is Program. Tampilkan semua postingan

Sample Handling Program Textarea in Java

Senin, 19 Desember 2011 | 0 komentar

The following program examples demonstrate the Textarea input handling in java. This textarea input can be made with a JTextArea object. The program will display two text-area where most or all of the selected text in text-area first to be copied / moved to a second text-area.

Here is the display program:


contoh-program-textarea-java 

Here is an example program


01 import java.awt .*;
02
03 import java.awt.event .*;
04
05 import javax.swing .*;
06
07 public class extends JFrame {TextAreaDemo
08
09 private JTextArea textArea1, textArea2;
10
11 private JButton btnCopy;
12
13 public TextAreaDemo () {
14
15 super ("Displays textarea");
16
17 Box box = Box.createHorizontalBox ();
18
19 String string = "This is just an example aja ya";
20
21 textArea1 = new JTextArea (string, 10, 15);
22
23 box.add (new JScrollPane (textArea1));
24
25 btnCopy = new JButton ("Copy >>");
26
27 box.add (btnCopy);
28
29 btnCopy.addActionListener (
30
31 new ActionListener () {
32
33 public void actionPerformed (ActionEvent e) {
34
35 textArea2.setText (textArea1.getSelectedText ());
36
37}
38
39}
40
41); / / end of addActionListener
42
43 textArea2 = new JTextArea (10.15);
44
45 textArea2.setEditable (false);
46
47 box.add (new JScrollPane (textArea2));
48
49 Container container = getContentPane ();
50
51 container.add (box);
52
53 setSize (425, 200);
54
55 setResizable (false);
56
57 setLocationRelativeTo (null);
58
59 setVisible (true);
60
61}
62
63 public static void main (String args []) {
64
65 JFrame.setDefaultLookAndFeelDecorated (true);
66
67 TextAreaDemo TextAreaDemo test = new ();
68
69 test.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
70
71}
72
73}

may be useful

Creating a Simple Slide in Java

| 0 komentar

Examples The following java program is a variation of the previous sample program list. Within this program, the list was made in order to be selected more than one option.

Here is the display program:


contoh-program-slider-java 

Here is an example program:


01 import java.awt .*;
02
03 import java.awt.event .*;
04
05 import javax.swing .*;
06
07 import javax.swing.event .*;
08
09 public class extends JFrame {SimpleSliderDemo
10
11 private JSlider slider;
12
13 private JTextField txtValue;
14
15 public SimpleSliderDemo () {
16
17 super ("JSlider Simple");
18
19 Container container = getContentPane ();
20
21 container.setLayout (new FlowLayout ());
22
23 txtValue = new JTextField (20);
24
25 txtValue.setBackground (Color.WHITE);
26
27 txtValue.setEditable (false);
28
29 slider = new JSlider (SwingConstants.HORIZONTAL, 0,100,50);
30
31 txtValue.setText (String.valueOf (slider.getValue ()));
32
Slider.setMinorTickSpacing 33 (5);
34
Slider.setMajorTickSpacing 35 (20);
36
37 slider.setPaintTicks (true);
38
39 slider.setLabelTable (slider.getLabelTable ());
40
41 slider.setPaintLabels (true);
42
43 slider.addChangeListener (
44
45 new ChangeListener () {
46
47 public void stateChanged (ChangeEvent e) {
48
49 txtValue.setText (String.valueOf (slider.getValue ()));
50
51}
52
53}
54
55);
56
57 container.add (txtValue);
58
59 container.add (slider);
60
61 setSize (280.200);
62
63 setVisible (true);
64
65}
66
67 public static void main (String args []) {
68
69 JFrame.setDefaultLookAndFeelDecorated (true);
70
71 SimpleSliderDemo SimpleSliderDemo test = new ();
72
73 test.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
74
75}
76
77}

Creating Menus in Java

| 0 komentar

Examples The following program demonstrates how to create a menu in java. Menus created with two main classes JMenu and JMenuItem.

Here is an example looks:


contoh-program-menu-java 

Berikut ini contoh program lengkapnya:
001import java.awt.*;
002 
003import java.awt.event.*;
004 
005import javax.swing.*;
006 
007public class MenuTest extends JFrame {
008 
009    private JMenuBar bar;
010 
011    private JMenu mnuFile, mnuHelp, submnuNew;
012 
013    private JMenuItem itemNew, itemOpen, itemExit, itemAbout, itemHelp,
014 
015        subitemFile, subitemProject;
016 
017    public MenuTest() {
018 
019        super ("Membuat menu");
020 
021        bar = new JMenuBar();
022 
023        setJMenuBar (bar);
024 
025        mnuFile = new JMenu ("File");
026 
027        mnuFile.setMnemonic('F');
028 
029        mnuHelp = new JMenu ("Help");
030 
031        mnuHelp.setMnemonic('H');
032 
033        submnuNew = new JMenu ("New");
034 
035        submnuNew.setMnemonic('N');
036 
037        mnuFile.add(submnuNew);
038 
039        subitemFile = new JMenuItem ("File...");
040 
041        subitemFile.setIcon(new ImageIcon("images/new.gif"));
042 
043        subitemFile.setAccelerator(KeyStroke.getKeyStroke(
044 
045            KeyEvent.VK_N, ActionEvent.CTRL_MASK));
046 
047        subitemFile.addActionListener(
048 
049            new ActionListener() {
050 
051                public void actionPerformed(ActionEvent e) {
052 
053                    JOptionPane.showMessageDialog(null, "New File");
054 
055                }
056 
057            }
058 
059        );
060 
061        submnuNew.add(subitemFile);
062 
063        subitemProject = new JMenuItem ("Project...");
064 
065        subitemProject.setMnemonic ('P');
066 
067        submnuNew.add(subitemProject);
068 
069        itemOpen = new JMenuItem ("Open");
070 
071        itemOpen.setMnemonic('O');
072 
073        mnuFile.add(itemOpen);
074 
075        mnuFile.addSeparator();
076 
077        itemExit = new JMenuItem ("Exit");
078 
079        itemExit.setMnemonic('E');
080 
081        itemExit.addActionListener(
082 
083            new ActionListener() {
084 
085                public void actionPerformed(ActionEvent e) {
086 
087                    if (JOptionPane.showConfirmDialog(null,"Apakah Anda yakin akan keluar?",
088 
089                        "Konfirmasi",JOptionPane.OK_CANCEL_OPTION,JOptionPane.QUESTION_MESSAGE)
090 
091                        == JOptionPane.OK_OPTION) {
092 
093                        System.exit(0);
094 
095                    }
096 
097                }
098 
099            }
100 
101        );
102 
103        mnuFile.add(itemExit);
104 
105        itemHelp = new JMenuItem ("Help");
106 
107        itemHelp.setIcon(new ImageIcon("images/help.gif"));
108 
109        itemHelp.setMnemonic('e');
110 
111        mnuHelp.add(itemHelp);
112 
113        itemAbout = new JMenuItem ("About");
114 
115        itemAbout.setMnemonic('A');
116 
117        itemAbout.setIcon(new ImageIcon("images/info.gif"));
118 
119        mnuHelp.add(itemAbout);
120 
121        bar.add(mnuFile);
122 
123        bar.add(mnuHelp);
124 
125        setSize (400,200);
126 
127        setLocationRelativeTo(null);
128 
129        setVisible (true);
130 
131    }
132 
133    public static void main (String args[]) {
134 
135        JFrame.setDefaultLookAndFeelDecorated(true);
136 
137        MenuTest test = new MenuTest();
138 
139        test.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
140 
141    }
142 
143}

Creating a Popup Menu in Java

| 0 komentar

Popup menu is a menu that is displayed when you right-click in the window. The following program is an example of a program to make a popup menu in Java. Class that is used is the JPopupMenu.

Here is the display program:


contoh-program-menu-popup-java 

Here's the source code program:
01 import java.awt .*;02 import java.awt.event .*;03 import javax.swing .*;0405 public class extends JFrame {PopupMenuTest06 private JRadioButtonMenuItem items [];ColorValues ​​07 private final Color [] = {Color.BLUE, Color.YELLOW, Color.red};08 private JPopupMenu popMenu;0910 public PopupMenuTest () {11 super ("Popup Menu");1213 ItemHandler ItemHandler handler = new ();14 colorNames String [] = {"Blue", "Yellow", "Red"};1516 ButtonGroup colorGroup = new ButtonGroup ();17 popMenu = new JPopupMenu ();18 items = new JRadioButtonMenuItem [colorValues.length];1920 for (int i = 0; i <items.length; i + +) {21 items [i] = new JRadioButtonMenuItem (colorNames [i]);22 popMenu.add (items [i]);23 colorGroup.add (items [i]);24 items [i]. AddActionListener (handler);25}2627 getContentPane (). SetBackground (Color.WHITE);2829 addMouseListener (30 new MouseAdapter () {31 public void mousePressed (MouseEvent e) {ShowPopupMenu 32 (e);33}3435 public void mouseReleased (MouseEvent e) {ShowPopupMenu 36 (e);37}3839 private void showPopupMenu (MouseEvent e) {40 if (e.isPopupTrigger ())41 popMenu.show (e.getComponent (), e.getX (), e.getY ());42}43} / / end of anonymous class44); / / end of addMouseListener4546 setSize (400.300);47 setLocationRelativeTo (null);48 setVisible (true);49}5051 public static void main (String args []) {52 JFrame.setDefaultLookAndFeelDecorated (true);53 PopupMenuTest PopupMenuTest test = new ();54 test.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);55}5657 private class implements ActionListener {ItemHandler58 public void actionPerformed (ActionEvent e) {59 / /60 for (int i = 0; i <items.length; i + +) {61 if (e.getSource () == items [i]) {62 getContentPane (). SetBackground (colorValues ​​[i]);63 return;64}65}66}67}68}
May be useful. [OOT] Visit this site to get a free book PHP and MySQL.

Sample program in Java Internal Frame

| 0 komentar

Here's an example of a simple program to create InternalFrame in Java. Internal Frame is an internal frame (inside the main frame). With this capability, we can make a lot of windows (multi-window) in one frame. To make use JInternalFrame class.

Here is an example looks:


 


Here is an example program:

001 import java.awt .*;002003 import java.awt.event .*;004005 import javax.swing .*;006007 public class extends JFrame {InternalFrameDemo008009 private JDesktopPane deskPane;010011 private JMenuBar bar;012013 private JMenu mnuFile;014015 private JMenuItem itemNew, itemClose;016017 public InternalFrameDemo () {018019 super ("Creating the Internal Frame");020021 JDesktopPane deskPane = new ();022023 getContentPane (). Add (deskPane);024025 bar = new JMenuBar ();026027 mnuFile = new JMenu ("File");028029 mnuFile.setMnemonic ('F');030031 itemNew = new JMenuItem ("New");032033 itemNew.setMnemonic ('N');034035 itemNew.setAccelerator (KeyStroke.getKeyStroke (KeyEvent.VK_N, ActionEvent.CTRL_MASK));036037 itemNew.addActionListener (038039 new ActionListener () {040041 private int count = 0;042043 public void actionPerformed (ActionEvent e) {044045 String title = "Internal Frame" + (+ + number);046047 Color bg = new Color ((float) Math.random (), (float) Math.random (), (float) Math.random ());048049 JInternalFrame iframe = new JInternalFrame (title, true, true, true, true);050051 / / (title, resizable, closable, maximizable, iconizable)052053 / / remove the title bar of an iframe054055 ((javax.swing.plaf.basic.BasicInternalFrameUI) iframe.getUI ()). SetNorthPane (null);056Iframe.setSize 057 (200.150);058059 iframe.setBackground (bg);060061 deskPane.add (iframe);062063 iframe.setVisible (true);064065}066067}068069);070071 itemClose = new JMenuItem ("Close");072ItemClose.setMnemonic 073 ('C');074075 itemClose.addActionListener (076077 new ActionListener () {078079 public void actionPerformed (ActionEvent e) {080081 System.exit (0);082083}084085}086087);088089 mnuFile.add (itemNew);090091 mnuFile.add (itemClose);092093 bar.add (mnuFile);094SetJMenuBar 095 (bar);096SetSize 097 (500.400);098099 setLocationRelativeTo (null);100101 setVisible (true);102103}104105 public static void main (String args []) {106107 InternalFrameDemo InternalFrameDemo test = new ();108109 test.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);110111}112113}
May be useful

Tabbed Panel Example Program in Java

| 0 komentar

Examples The following program demonstrates how to make the panels in the form of Tab in java. JTabbedPane class is used.

The following looks:



contoh-program-tabbed-java 

Here is an example program:

01 import java.awt .*;
02 import java.awt.event .*;
03 import javax.swing .*;
04
05 public class extends JFrame {TabbedPaneDemo
06 private JTabbedPane tabPane;
07 private JLabel label1, label2, label3;
08 private JPanel Panel1, panel2, panel3;
09
10 public TabbedPaneDemo () {
11 super ("Tabbed Pane Demo");
12
13 tabPane = new JTabbedPane ();
14 label1 = new JLabel ("first panel", SwingConstants.CENTER);
15 Panel1 = new JPanel ();
16 panel1.add (label1);
17 tabPane.addTab (​​"First Tab", null, Panel1, "First Panel");
18
19 label2 = new JLabel ("second panel", SwingConstants.CENTER);
20 panel2 = new JPanel ();
21 panel2.setBackground (Color.ORANGE);
22 panel2.add (label2);
23 Icon icon = new ImageIcon ("images / new.gif");
24 tabPane.addTab (​​"Second Tab" icon, panel2, "Second Panel");
25
26 label3 = new JLabel ("third panel", SwingConstants.CENTER);
27 panel3 = new JPanel ();
28 panel3.setBackground (Color.YELLOW);
29 panel3.setLayout (new BorderLayout ());
30 panel3.add (new JButton ("North"), BorderLayout.NORTH);
31 panel3.add (new JButton ("West"), BorderLayout.WEST);
32 panel3.add (new JButton ("East"), BorderLayout.EAST);
33 panel3.add (new JButton ("South"), BorderLayout.SOUTH);
34 panel3.add (label3);
35 tabPane.addTab (​​"Tab Three", null, panel3, "Third Panel");
36
37 getContentPane (). Add (tabPane);
38
39 setSize (400.300);
40 setLocationRelativeTo (null);
41 setVisible (true);
42}
43
44 public static void main (String args []) {
45 TabbedPaneDemo TabbedPaneDemo test = new ();
46 test.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
47}
48}

may be useful
 

Sample Program Event Handling in Java

| 0 komentar

In the following example is added confirmation window when exit button is pressed.

The following looks:



 

Examples of programs:

01import java.awt.*;
02 
03import java.awt.event.*;
04 
05import javax.swing.*;
06 
07public class ClickMe3 extends JFrame {
08 
09    private JButton tombol, btnExit;
10 
11    public ClickMe3() {
12 
13        super ("Event Handling");
14 
15        Container container = getContentPane();
16 
17        container.setLayout(new FlowLayout());
18 
19        ClickListener cl = new ClickListener ();
20 
21        tombol = new JButton ("Click Me!");
22 
23        tombol.addActionListener(cl);
24 
25        container.add(tombol);
26 
27        btnExit = new JButton ("Exit");
28 
29        btnExit.addActionListener(cl);
30 
31        container.add(btnExit);
32 
33        setSize (200,100);
34 
35        setVisible (true);
36 
37    }
38 
39    public static void main (String arg[]) {
40 
41        ClickMe3 test = new ClickMe3();
42 
43        test.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
44 
45    }
46 
47    //inner class
48 
49    private class ClickListener implements ActionListener {
50 
51        public void actionPerformed (ActionEvent e) {
52 
53            if (e.getSource() == tombol) {
54 
55                JOptionPane.showMessageDialog(null, "You click me again, guys !!!");
56 
57            } else if (e.getSource() == btnExit){
58 
59                if ( JOptionPane.showConfirmDialog(null, "Apakah Anda yakin akan keluar ?","Konfirmasi",
60 
61                        JOptionPane.OK_CANCEL_OPTION,JOptionPane.QUESTION_MESSAGE) == JOptionPane.OK_OPTION) {
62 
63                        System.exit(0);
64 
65                    }
66 
67            }
68 
69        }
70 
71    }
72 
73}
 

Program examples in Java KeyEvent

| 0 komentar

Here's an example Java program to demonstrate how the corresponding event handling button. The program will detect any keyboard key presses. Listener classes used are KeyListener which has 3 (three) keyTyped abstract method (), keyPressed () and keyReleased ().

The following looks:


contoh-program-key-event-java 

01import java.awt.*;
02import java.awt.event.*;
03import javax.swing.*;
04 
05public class KeyEventTest extends JFrame implements KeyListener {
06    private String baris1="", baris2="", baris3="";
07    private JTextArea textArea;
08 
09    public KeyEventTest() {
10        super ("Mencoba Key Event");
11 
12        textArea = new JTextArea (10,15);
13        textArea.setText("Tekan sembarang tombol di keyboard...");
14        textArea.setEnabled(false);
15        textArea.setDisabledTextColor(Color.BLACK);
16        getContentPane().add(textArea);
17 
18        addKeyListener (this);
19 
20        setSize (300,150);
21        setLocationRelativeTo(null);
22        setVisible(true);
23    }
24 
25    public void keyPressed (KeyEvent e) {
26        baris1 = "Tombol yang ditekan : " + e.getKeyText(e.getKeyCode());
27        setLines2and3 (e);
28    }
29 
30    public void keyReleased (KeyEvent e) {
31        baris1 = "Tombol yang dilepas : " + e.getKeyText(e.getKeyCode());
32        setLines2and3 (e);
33    }
34 
35    public void keyTyped (KeyEvent e) {
36        baris1 = "Tombol yang ditulis : " + e.getKeyChar();
37        setLines2and3 (e);
38    }
39 
40    private void setLines2and3 (KeyEvent e) {
41        baris2 = "This key is "+ (e.isActionKey() ? "" : "not ") + "an action key";
42        String temp = e.getKeyModifiersText(e.getModifiers());
43        baris3 = "Modifier key pressed : " + (temp.equals("") ? "none" : temp);
44        textArea.setText(baris1 + "\n" + baris2 + "\n" + baris3 + "\n");
45    }
46 
47    public static void main (String args[]) {
48            KeyEventTest test = new KeyEventTest();
49            test.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
50        }
51}
Semoga bermanfaat
 
© Copyright 2010-2011 materials informatics All Rights Reserved.
Template Design by Moch Ramdhan | Published by Ramdhan Templates | Powered by Blogger.com.