Jumat, 31 Januari 2014

“Control Properties” dengan Java Swing (NetBeans IDE 7.4)

ControlPropertiesApp

Contoh sederhana event handler swing control seperti jScrollBar, jComboBox, buttonGroup, jRadioButton, jCheckBox1, jPanel, jButton.

Reverensi menggunaan editor NetBeans IDE 7.3 Sumber

 

 

1. Jalankan NetBeans IDE 7.4.

2. Klik File > New Project atau tekan tombol Ctrl+Shift+N pada keyboard.

3. Pada jendela New Project, pada bagian Categories pilih “Java”. Pada pilihan Project pilih “Java Application” kemudian klik tombol “Next >”.

4. Pada jendela New Java Application, Pada kotak isian Project Name:ControlPropertiesApp”. Hilangkan pilihan Create Main Class. Kemudian klik tombol Finish.

5. Pada jendela Project, klik kanan ControlPropertiesApp pilih New > JFrame Form…

6. Pada jendela New JFrame Form, Pada kotak isian Class Name:ControlPropertiesAppUI”. Pada kotak isian Package:My.ControlPropertiesApp” kemudian klik tombol “Finish

 

Setting Control Property:

 

ControlPropertiesAppDesign

Object Name Properties Setting
Form jFrame

title
resizable
Generate Center

Control Properties
False
True

Label jLabel1 text Type Your Name:
Text Field jTextField1 text

(Empty)

Text Field jTextField2 editable
background
font
horizontal alignment
text

False
255,255,255
Tahoma 24 Plain
CENTER
(Empty)

Button jButton1 text Show
Panel jPanel1 Border
Title
Titled Border
Background (RGB)
Scroll Bar jScrollBar1 maximum
orientation
visible amount
255
HORIZONTAL
30
Scroll Bar jScrollBar1 maximum
orientation
visible amount
255
HORIZONTAL
30
Scroll Bar jScrollBar1 maximum
orientation
visible amount
255
HORIZONTAL
30
Button jButton2 text Exit
Panel jPanel2 Border
Title
Titled Border
Font Properties
Label jLabel2 text Alignment:
Combo Box jComboBox1    
Panel jPanel3 Border
Title
Titled Border
Color
Button Group buttonGroup1    
Radio Button jRadioButton1 button Group
text
buttonGroup1
Red
Radio Button jRadioButton2 button Group
text
buttonGroup1
Green
Radio Button jRadioButton3 button Group
text
buttonGroup1
Blue
Radio Button jRadioButton4 button Group
text
buttonGroup1
Yellow
Panel jPanel4 Border
text
Titled Border
Style
Check Box jCheckBox1 text Bold
Check Box jCheckBox2 text Italic
Check Box jCheckBox3 text Underline
Check Box jCheckBox4 text Strikethrough

 

Event Program:

private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {                                        
        System.exit(0);
    }                                       

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {                                        
        ActionPerformed(evt);
    }                                       

    private void formWindowOpened(java.awt.event.WindowEvent evt) {                                 
        String[] arrAlignment = {"Please choose one","Left Justify","Center"
                                ,"Right Justify"};
        jComboBox1.setModel(new javax.swing.DefaultComboBoxModel(arrAlignment));
    }                                

    private void jComboBox1ActionPerformed(java.awt.event.ActionEvent evt) {                                          
        switch (jComboBox1.getSelectedItem().toString()) {
            case "Left Justify":
                jTextField2.setHorizontalAlignment(JTextField.LEFT);
                break;
            case "Center":
                jTextField2.setHorizontalAlignment(JTextField.CENTER);
                break;
            case "Right Justify":
                jTextField2.setHorizontalAlignment(JTextField.RIGHT);
                break;
        }
    }                                         

    private void jRadioButton1ActionPerformed(java.awt.event.ActionEvent evt) {                                             
        actionPerformed(evt);
    }                                            

    private void jRadioButton2ActionPerformed(java.awt.event.ActionEvent evt) {                                             
        actionPerformed(evt);
    }                                            

    private void jRadioButton3ActionPerformed(java.awt.event.ActionEvent evt) {                                             
        actionPerformed(evt);
    }                                            

    private void jRadioButton4ActionPerformed(java.awt.event.ActionEvent evt) {                                             
        actionPerformed(evt);
    }                                            

    private void jCheckBox1ItemStateChanged(java.awt.event.ItemEvent evt) {                                           
        itemStateChanged(evt);
    }                                          

    private void jCheckBox2ItemStateChanged(java.awt.event.ItemEvent evt) {                                           
        itemStateChanged(evt);
    }                                          

    private void jCheckBox3ItemStateChanged(java.awt.event.ItemEvent evt) {                                           
        itemStateChanged(evt);
    }                                          

    private void jCheckBox4ItemStateChanged(java.awt.event.ItemEvent evt) {                                           
        itemStateChanged(evt);
    }                                          

    private void jScrollBar1AdjustmentValueChanged(java.awt.event.AdjustmentEvent evt) {                                                  
        adjustmentValueChanged(evt);
    }                                                 

    private void jScrollBar2AdjustmentValueChanged(java.awt.event.AdjustmentEvent evt) {                                                  
        adjustmentValueChanged(evt);
    }                                                 

    private void jScrollBar3AdjustmentValueChanged(java.awt.event.AdjustmentEvent evt) {                                                  
        adjustmentValueChanged(evt);
    }                                                 

    public void actionPerformed(ActionEvent e){
        if(jRadioButton1.isSelected()){
            jTextField2.setForeground(Color.red);
        }else if(jRadioButton2.isSelected()){
            jTextField2.setForeground(Color.green);
        }else if(jRadioButton3.isSelected()){
            jTextField2.setForeground(Color.blue);
        }else if(jRadioButton4.isSelected()){
            jTextField2.setForeground(Color.yellow);
        }
    }
    public void itemStateChanged(ItemEvent e){
        Object source = e.getItemSelectable();
        if(source == jCheckBox1){
            Font font;
            font = new Font(jTextField2.getFont().getName(), Font.BOLD
                    , jTextField2.getFont().getSize());
            jTextField2.setFont(font);
        }else if(source == jCheckBox2){
            Font font;
            font = new Font(jTextField2.getFont().getName(), Font.ITALIC
                    , jTextField2.getFont().getSize());
            jTextField2.setFont(font);
        }else if(source == jCheckBox3){
            Font font;
            font = jTextField2.getFont();
            Map attributes;
            attributes = font.getAttributes();
            attributes.put(TextAttribute.UNDERLINE, TextAttribute.UNDERLINE_ON);
            jTextField2.setFont(font.deriveFont(attributes));
        }else if(source == jCheckBox4){
            Font font;
            font = jTextField2.getFont();
            Map attributes = font.getAttributes();
            attributes.put(TextAttribute.STRIKETHROUGH
                    , TextAttribute.STRIKETHROUGH_ON);
            jTextField2.setFont(font.deriveFont(attributes));
        }
        if(e.getStateChange()== ItemEvent.DESELECTED){
            Font font;
            font = new Font(jTextField2.getFont().getName(), Font.PLAIN
                    , jTextField2.getFont().getSize());
            jTextField2.setFont(font);
        }
        if(source == jCheckBox1 && source == jCheckBox2){
            Font font;
            font = new Font(jTextField2.getFont().getName(), Font.BOLD
                    + Font.ITALIC, jTextField2.getFont().getSize());
            jTextField2.setFont(font);
        }
    }
    public void adjustmentValueChanged(AdjustmentEvent e){
        if(e.getSource()==jScrollBar1){
            jScrollBar1.getValue();
        }else if(e.getSource()==jScrollBar2){
            jScrollBar2.getValue();
        }else if(e.getSource()==jScrollBar3){
            jScrollBar3.getValue();
        }
        this.getContentPane().setBackground(new Color(jScrollBar1.getValue()
                ,jScrollBar2.getValue(),jScrollBar3.getValue()));
        jPanel1.setBackground(new Color(jScrollBar1.getValue()
                ,jScrollBar2.getValue(),jScrollBar3.getValue()));
        jPanel2.setBackground(new Color(jScrollBar1.getValue()
                ,jScrollBar2.getValue(),jScrollBar3.getValue()));
        jPanel3.setBackground(new Color(jScrollBar1.getValue()
                ,jScrollBar2.getValue(),jScrollBar3.getValue()));
        jPanel4.setBackground(new Color(jScrollBar1.getValue()
                ,jScrollBar2.getValue(),jScrollBar3.getValue()));
    }
    public void ActionPerformed(ActionEvent e){
        Object source = e.getSource();
        if(source instanceof JButton){
            switch (e.getActionCommand()) {
                case "Show":
                    jButton1.setText("New");
                    jTextField2.setText(jTextField1.getText());
                    jTextField1.setEditable(false);
                    jTextField1.setBackground(Color.white);
                    break;
                case "New":
                    jButton1.setText("Show");
                    jTextField1.setEditable(true);
                    jTextField1.setText("");
                    jTextField2.setText("");
                    jComboBox1.setSelectedIndex(0);
                    jTextField2.setHorizontalAlignment(JTextField.CENTER);
                    buttonGroup1.clearSelection();
                    jCheckBox1.setSelected(false);
                    jCheckBox2.setSelected(false);
                    jCheckBox3.setSelected(false);
                    jCheckBox4.setSelected(false);
                    jTextField1.grabFocus();
                    break;
            }
        }
    }

7. Jalankan Project, pada ToolBar klik tombol Run Project atau klik F6 pada keyboard

8. Pada jendela Run Project klik tombol OK untuk menjadikan ControlPropertiesAppUI menjadi main class.

Label:

0 Komentar:

Posting Komentar

Berlangganan Posting Komentar [Atom]

<< Beranda