Thứ Sáu, 9 tháng 11, 2012

Bài tập thực hành JAVA (ItemListener,Vertical,Horizontal)

GỚI THIÊU THÔNG TIN

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package BUOI3;

import com.sun.org.apache.bcel.internal.generic.InstructionComparator;
import com.sun.org.apache.bcel.internal.generic.Select;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import javax.swing.*;
import javax.swing.border.Border;
import javax.swing.border.TitledBorder;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import sun.awt.DisplayChangedListener;
import sun.java2d.loops.CompositeType;
import sun.java2d.loops.DrawLine;
import sun.java2d.loops.SurfaceType;

/**
 *
 * @author HoMinhChung
 */
public class CAU3 implements ItemListener{
    private final JFrame frame;
    private final JLabel kq;
    private final JComboBox combo1;
    private final JComboBox combo2;
    private final JComboBox combo3;
    private final JComboBox combo4;
    private final String title = "Grapes";
    private final String horizotal[] = {"CENTER","LEFT","RIGHT"};
    private final String vertical[] = {"CENTER","TOP","BOTTOM"};
    private final ImageIcon anh;
    public CAU3(){
    frame = new JFrame("Example3-www.vn-full.com");
    frame.setLocationRelativeTo(frame);
    frame.setSize(380, 280);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setLayout(new GridLayout(2,1));
    anh = new ImageIcon("Image/username.png");
  
    JPanel centerPanel = new JPanel(new BorderLayout());
    kq = new JLabel();
    {
        kq.setText("TITLE");
        kq.setFont(new Font("Arial",Font.BOLD,25));
        kq.setForeground(Color.red);
        kq.setIcon(anh);
        kq.setHorizontalAlignment(SwingConstants.CENTER);
        kq.setVerticalAlignment(SwingConstants.CENTER);
        kq.setHorizontalTextPosition(SwingConstants.CENTER);
        kq.setVerticalTextPosition(SwingConstants.CENTER);
    }
    centerPanel.add(kq,BorderLayout.CENTER);
    JPanel southPanel = new JPanel(new BorderLayout());
    JPanel csouthPanel = new JPanel(new GridLayout(1,2));
        JPanel lcsouthPanel = new JPanel(new GridLayout(1,2,3,3));
        Border borderLcsouthPanel = BorderFactory.createLineBorder(Color.LIGHT_GRAY);
        TitledBorder TitleBorderLcsouthPanel = new TitledBorder(borderLcsouthPanel, "Horizontal Alignment");
        lcsouthPanel.setBorder(TitleBorderLcsouthPanel);
            JPanel lcsouthPanel1 = new JPanel(new GridLayout(2, 1,5,5));
            Border borderlcsouthPanel1 = BorderFactory.createLineBorder(Color.LIGHT_GRAY);
            lcsouthPanel1.setBorder(borderLcsouthPanel);
            lcsouthPanel1.add(new JLabel("Horizontal"));
            lcsouthPanel1.add(new JLabel("Vertical"));
          
            JPanel lcsouthPanel2 = new JPanel(new GridLayout(2, 1,5,5));
            Border borderlcsouthPanel2 = BorderFactory.createLineBorder(Color.LIGHT_GRAY);
            lcsouthPanel2.setBorder(borderlcsouthPanel2);
            JPanel lcsouthPanel21 = new JPanel(new FlowLayout(FlowLayout.CENTER, 20, 15));
                JPanel lcsouthPanel22 = new JPanel();
                combo1 = new JComboBox(horizotal);
                combo2 = new JComboBox(vertical);
                lcsouthPanel21.add(combo1);
                lcsouthPanel22.add(combo2);
                lcsouthPanel2.add(lcsouthPanel21);
                lcsouthPanel2.add(lcsouthPanel22);
                combo1.addItemListener(this);
                combo2.addItemListener(this);
                combo1.setSelectedIndex(0);
                combo2.setSelectedIndex(0);
          
            lcsouthPanel.add(lcsouthPanel1);
            lcsouthPanel.add(lcsouthPanel2);
      
        JPanel rcsouthPanel = new JPanel(new GridLayout(1,2,3,3));
        Border borderRcsouthPanel = BorderFactory.createLineBorder(Color.LIGHT_GRAY);
        TitledBorder TitleBorderRcsouthPanel = new TitledBorder(borderRcsouthPanel, "Text Position");
        rcsouthPanel.setBorder(TitleBorderRcsouthPanel);
            JPanel rcsouthPanel1 = new JPanel(new GridLayout(2, 1,5,5));
            Border borderRcsouthPanel1 = BorderFactory.createLineBorder(Color.LIGHT_GRAY);
            rcsouthPanel1.setBorder(borderRcsouthPanel);
            rcsouthPanel1.add(new JLabel("Horizontal"));
            rcsouthPanel1.add(new JLabel("Vertical"));
          
            JPanel rcsouthPanel2 = new JPanel(new GridLayout(2, 1,5,5));
            Border borderRcsouthPanel2 = BorderFactory.createLineBorder(Color.LIGHT_GRAY);
            rcsouthPanel2.setBorder(borderRcsouthPanel2);
                JPanel rcsouthPanel21 = new JPanel(new FlowLayout(FlowLayout.CENTER, 20, 15));
                JPanel rcsouthPanel22 = new JPanel();
                combo3 = new JComboBox(horizotal);
                combo4 = new JComboBox(vertical);
                rcsouthPanel21.add(combo3);
                rcsouthPanel22.add(combo4);
                rcsouthPanel2.add(rcsouthPanel21);
                rcsouthPanel2.add(rcsouthPanel22);
                combo3.addItemListener(this);
                combo4.addItemListener(this);
                combo3.setSelectedIndex(0);
                combo4.setSelectedIndex(0);
              
                rcsouthPanel.add(rcsouthPanel1);
                rcsouthPanel.add(rcsouthPanel2);
      
        csouthPanel.add(lcsouthPanel);
        csouthPanel.add(rcsouthPanel);
  
        JSeparator vien = new JSeparator();
        vien.setForeground(Color.LIGHT_GRAY);
        southPanel.add(vien,BorderLayout.NORTH);
        southPanel.add(csouthPanel,BorderLayout.CENTER);
      
    frame.add(centerPanel);
    frame.add(southPanel);
    frame.setVisible(true);
    }
    public static void main(String[] args) {
        CAU3 cau3 = new CAU3();
    }

    @Override
    public void itemStateChanged(ItemEvent e) {
        if(e.getStateChange()==ItemEvent.SELECTED){
            if(e.getSource()==combo1){
                int index = combo1.getSelectedIndex();
                if(index==0) kq.setHorizontalAlignment(SwingConstants.CENTER);
                if(index==1) kq.setHorizontalAlignment(SwingConstants.LEFT);
                if(index==2) kq.setHorizontalAlignment(SwingConstants.RIGHT);
            }
            if(e.getSource()==combo2){
                int index = combo2.getSelectedIndex();
                if(index==0) kq.setVerticalAlignment(SwingConstants.CENTER);
                if(index==1) kq.setVerticalAlignment(SwingConstants.TOP);
                if(index==2) kq.setVerticalAlignment(SwingConstants.BOTTOM);
            }
            if(e.getSource()==combo3){
                int index = combo3.getSelectedIndex();
                if(index==0) kq.setHorizontalTextPosition(SwingConstants.CENTER);
                if(index==1) kq.setHorizontalTextPosition(SwingConstants.LEFT);
                if(index==2) kq.setHorizontalTextPosition(SwingConstants.RIGHT);
            }
            if(e.getSource()==combo4){
                int index = combo4.getSelectedIndex();
                if(index==0) kq.setVerticalTextPosition(SwingConstants.CENTER);
                if(index==1) kq.setVerticalTextPosition(SwingConstants.TOP);
                if(index==2) kq.setVerticalTextPosition(SwingConstants.BOTTOM);
            }
        }
    }
}

THÔNG TIN CODE

Thể loại:
CODE BÀI TẬP JAVA
Trang chủ:
www.vn-full.com
Nguồn:
www.vn-full.com
Cập nhật:
09:09 08/11/2012
Dung lượng:
0MB
Sử dụng:
Miễn Phí Free
Download:
0000
Người viết:
Konodo

Không có nhận xét nào:

Đăng nhận xét

Girls Generation - Korean