1 top va buttom vuot qua gioi han Wed Nov 21, 2012 10:19 pm
1101817
Vịt mới lớn
cac pac xem dum minh code nay nhe cai button va top cua minh no vuot khoi cai frame hog nhin thay lun hog hieu tai sao mong cac pac giai thich dum minh thanks...
- Code:
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
public class TH03 {
private JFrame frame;
private JPanel jp1,control,ha,l1,l2,tp,r1,r2;
private final String[] H = {"CENTER","LEFT","RIGHT"};
private final String[] V = {"CENTER","TOP","BUTTON"};
private JLabel anh,lb1,lb2,lb3,lb4;
private JComboBox com1,com2,com3,com4;
public TH03(){
frame = new JFrame();
frame.setBounds(500,100,500, 300);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//create hinh anh
anh = new JLabel();
anh.setIcon(new ImageIcon("grapes.jpg"));
anh.setText("Grapes");
anh.setPreferredSize(new Dimension(200,150));
anh.setBorder(BorderFactory.createLineBorder(Color.RED));
anh.setHorizontalAlignment(SwingConstants.CENTER);
anh.setVerticalAlignment(SwingConstants.CENTER);
anh.setHorizontalTextPosition(SwingConstants.CENTER);
anh.setVerticalTextPosition(SwingConstants.CENTER);
frame.add(anh,BorderLayout.NORTH);
//
control = new JPanel(new GridLayout(1,2));
ha = new JPanel(new BorderLayout());
//Horizontal Alignment
l1 = new JPanel(new BorderLayout());
lb1 = new JLabel("Horizontal");
lb2 = new JLabel("Vertical");
l1.add(lb1,BorderLayout.NORTH);
l1.add(lb2,BorderLayout.SOUTH);
//
l2 = new JPanel(new BorderLayout());
com1 = new JComboBox(H);
com1.setSelectedIndex(0);
com1.setPreferredSize(new Dimension(80,22));
//
com2 = new JComboBox(V);
com2.setSelectedIndex(0);
com2.setPreferredSize(new Dimension(80,22));
l2.add(com1,BorderLayout.NORTH);
l2.add(com2,BorderLayout.SOUTH);
l1.setBackground(Color.WHITE);
l2.setBackground(Color.WHITE);
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.RED));
ha.setBorder(BorderFactory.createTitledBorder("Horizontal Alignment"));
ha.setBackground(Color.WHITE);
com1.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent ae) {
JComboBox tem = (JComboBox)ae.getSource();
int pos = tem.getSelectedIndex();
if(pos==0)
anh.setHorizontalAlignment(SwingConstants.CENTER);
else
if(pos==1)
anh.setHorizontalAlignment(SwingConstants.LEFT);
else
anh.setHorizontalAlignment(SwingConstants.RIGHT);
}
}
);
com2.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent ae) {
JComboBox tem = (JComboBox)ae.getSource();
int pos = tem.getSelectedIndex();
if(pos==0)
anh.setVerticalAlignment(SwingConstants.CENTER);
else
if(pos==1)
anh.setVerticalAlignment(SwingConstants.TOP);
else
anh.setVerticalAlignment(SwingConstants.BOTTOM);
}
});
//Text Position
tp = new JPanel(new BorderLayout());
r1 = new JPanel(new BorderLayout());
lb3 = new JLabel("Horizotal");
lb4 = new JLabel("Vertical");
r1.add(lb3,BorderLayout.NORTH);
r1.add(lb4,BorderLayout.SOUTH);
//
r2 = new JPanel(new BorderLayout());
com3 = new JComboBox(H);
com3.setSelectedIndex(0);
com3.setPreferredSize(new Dimension(80,22));
com3.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent ae) {
JComboBox tem = (JComboBox)ae.getSource();
int pos = tem.getSelectedIndex();
if(pos==0)
anh.setHorizontalTextPosition(SwingConstants.CENTER);
else
if(pos==1)
anh.setHorizontalTextPosition(SwingConstants.LEFT);
else
anh.setHorizontalTextPosition(SwingConstants.RIGHT);
}
}
);
//
com4 = new JComboBox(V);
com4.setSelectedIndex(0);
com4.setPreferredSize(new Dimension(80,22));
com4.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent ae) {
JComboBox tem = (JComboBox)ae.getSource();
int pos = tem.getSelectedIndex();
if(pos==0)
anh.setVerticalTextPosition(SwingConstants.CENTER);
else
if(pos==1)
anh.setVerticalTextPosition(SwingConstants.TOP);
else
anh.setVerticalTextPosition(SwingConstants.BOTTOM);
}
});
r2.add(com3,BorderLayout.NORTH);
r2.add(com4,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.RED));
tp.setBorder(BorderFactory.createTitledBorder("Text Position"));
tp.setBackground(Color.WHITE);
//control = new JPanel(new BorderLayout());
control.add(ha,BorderLayout.WEST);
control.add(tp,BorderLayout.SOUTH);
control.setBackground(Color.WHITE);
control.setPreferredSize(new Dimension(500,80));
frame.add(control,BorderLayout.SOUTH);
frame.setVisible(true);
}
public static void main(String[] args) {
TH03 t3 = new TH03();
}
}