1 Bài 3 - Buổi3 Mon Nov 05, 2012 7:50 pm
Admin
Admin
- Code:
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.Image;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.BorderFactory;
import javax.swing.GroupLayout.Alignment;
import javax.swing.ImageIcon;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SwingConstants;
public class Bai3 {
JFrame frame;
JLabel lb,ha1,ha2,tp1,tp2,lb1,lb2,lb3,lb4;
JPanel control,ha,tp,l1,l2,r1,r2;
JComboBox t,cb1,cb2,cb3,cb4;
private final String[] H = {"CENTER","LEFT","RIGHT"};
private final String[] V = {"CENTER","TOP","BOTTOM"};
Bai3(){
frame = new JFrame();
frame.setLayout(new BorderLayout());
frame.setTitle("Bai3");
frame.setLocation(300, 300);
frame.setSize(320, 270);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// Create Lable
lb = new JLabel();
lb.setIcon(new ImageIcon("username.png"));
lb.setText("Username");
lb.setPreferredSize(new Dimension(300, 150));
lb.setBorder(BorderFactory.createLineBorder(Color.gray));
//Default Label
lb.setHorizontalAlignment(SwingConstants.CENTER);
lb.setVerticalAlignment(SwingConstants.CENTER);
lb.setHorizontalTextPosition(SwingConstants.CENTER);
lb.setVerticalTextPosition(SwingConstants.CENTER);
//Create Control
control = new JPanel(new GridLayout(1, 2));
ha = new JPanel(new BorderLayout());
//Horizontal Alignment
l1 = new JPanel(new BorderLayout());
//Create label 1
lb1 = new JLabel("Horizontal ");
lb2 = new JLabel("Vertical ");
//Add to panel l1
l1.add(lb1,BorderLayout.NORTH);
l1.add(lb2,BorderLayout.SOUTH);
//Create panel l2
l2 = new JPanel(new BorderLayout());
//Create cobobox 1
cb1 = new JComboBox(H);
cb1.setSelectedIndex(0);
cb1.setPreferredSize(new Dimension(80, 22));
//cb1 Listener
cb1.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
JComboBox temp = (JComboBox) e.getSource();
int pos = temp.getSelectedIndex();
if (pos==0){
lb.setHorizontalAlignment(SwingConstants.CENTER);
}
else
if (pos==1){
lb.setHorizontalAlignment(SwingConstants.LEFT);
}
else if (pos==2){
lb.setHorizontalAlignment(SwingConstants.RIGHT);
}
}
});
//End listenr;
cb2 = new JComboBox(V);
cb2.setSelectedIndex(0);
cb2.setPreferredSize(new Dimension(80, 22));
cb2.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
JComboBox temp = (JComboBox) e.getSource();
int pos = temp.getSelectedIndex();
if (pos==0){
lb.setVerticalAlignment(SwingConstants.CENTER);
}
else
if (pos==1){
lb.setVerticalAlignment(SwingConstants.TOP);
}
else
if (pos==2){
lb.setVerticalAlignment(SwingConstants.BOTTOM);
}
}
});
//Add combobox to panel l2
l2.add(cb1,BorderLayout.NORTH);
l2.add(cb2,BorderLayout.SOUTH);
//Edit
l1.setBackground(Color.white);
l2.setBackground(Color.white);
//Add panel l1 to panel ha (Horizontal Alignment)
ha.add(l1,BorderLayout.WEST);
ha.add(l2,BorderLayout.EAST);
ha.setAlignmentX(Component.CENTER_ALIGNMENT);
ha.setAlignmentY(Component.CENTER_ALIGNMENT);
ha.setBorder(BorderFactory.createLineBorder(Color.gray));
ha.setBorder(BorderFactory.createTitledBorder("Horizontal Alignment"));
ha.setBackground(Color.white);
//Text Position (tp)
tp = new JPanel(new BorderLayout());
r1 = new JPanel(new BorderLayout());
lb3 = new JLabel("Horizontal ");
lb4 = new JLabel("Vertical ");
r1.add(lb3,BorderLayout.NORTH);
r1.add(lb4,BorderLayout.SOUTH);
r2 = new JPanel(new BorderLayout());
cb3 = new JComboBox(H);
cb3.setSelectedIndex(0);
cb3.setPreferredSize(new Dimension(80, 22));
//Create Listener
cb3.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
JComboBox temp = (JComboBox) e.getSource();
int pos = temp.getSelectedIndex();
if (pos==0){
lb.setHorizontalTextPosition(SwingConstants.CENTER);
}
else
if (pos==1){
lb.setHorizontalTextPosition(SwingConstants.LEFT);
}
else if(pos==2){
lb.setHorizontalTextPosition(SwingConstants.RIGHT);
}
}
});
cb4 = new JComboBox(V);
cb4.setSelectedIndex(0);
cb4.setPreferredSize(new Dimension(80, 22));
//Create Listener
cb4.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
JComboBox temp = (JComboBox) e.getSource();
int pos = temp.getSelectedIndex();
if (pos==0){
lb.setVerticalTextPosition(SwingConstants.CENTER);
}
else
if (pos==1){
lb.setVerticalTextPosition(SwingConstants.TOP);
}
else if(pos==2){
lb.setVerticalTextPosition(SwingConstants.BOTTOM);
}
}
});
r2.add(cb3,BorderLayout.NORTH);
r2.add(cb4,BorderLayout.SOUTH);
r1.setBackground(Color.white);
r2.setBackground(Color.white);
tp.add(r1,BorderLayout.WEST);
tp.add(r2,BorderLayout.EAST);
tp.setAlignmentX(Component.CENTER_ALIGNMENT);
tp.setAlignmentY(Component.CENTER_ALIGNMENT);
tp.setBorder(BorderFactory.createLineBorder(Color.gray));
tp.setBorder(BorderFactory.createTitledBorder("Text Position"));
tp.setBackground(Color.white);
control = new JPanel(new BorderLayout());
control.add(ha,BorderLayout.WEST);
control.add(tp,BorderLayout.EAST);
control.setBackground(Color.white);
control.setPreferredSize(new Dimension(500, 80));
frame.add(lb,BorderLayout.NORTH);
frame.add(control,BorderLayout.SOUTH);
frame.setVisible(true);
}
public static void main(String[] args) {
Bai3 obj = new Bai3();
}
}