package com.woaixuchen;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import javax.swing.ButtonGroup;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
public class Gui2 extends JFrame implements ActionListener,MouseListener{
private JLabel jlabel;
private JButton button;
private JPanel jpa,jpa2;
private JComboBox comboBox;
private ButtonGroup group;
JRadioButton button1,button2,button3;
int x,y,width,height;
Color b;
Gui2(){
jpa=new JPanel();
jpa2=new JPanel();
jpa2.setLayout(null);
button=new JButton("重置");
group=new ButtonGroup();
button1=new JRadioButton("大");
button1.setSelected(true);
button2=new JRadioButton("中");
button3=new JRadioButton("小");
group.add(button1);
group.add(button2);
group.add(button3);
jlabel=new JLabel("颜色");
String sign[] = { "红", "黄", "蓝"};
comboBox=new JComboBox(sign);
jpa.add(button);
jpa.add(button1);
jpa.add(button2);
jpa.add(button3);
jpa.add(jlabel);
jpa.add(comboBox);
add(jpa,BorderLayout.SOUTH);
add(jpa2,BorderLayout.CENTER);
setBounds(200,50,600,600);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
button.addActionListener(this);
addMouseListener(this);
}
private void init(){
sizeSelect();
corlorSelect();
}
private void sizeSelect(){
if(button1.isSelected()){
width=80;
height=60;
}
if(button2.isSelected()){
width=60;
height=40;
}
if(button3.isSelected()){
width=40;
height=20;
}
}
private Color corlorSelect(){
int i = comboBox.getSelectedIndex();
if(i==0)
b=Color.red;
if(i==1)
b=Color.yellow;
if(i==2)
b=Color.blue;
return b;
}
public void actionPerformed(ActionEvent e) {
button1.setSelected(true);
}
public void mouseClicked(MouseEvent e) {
if(e.getButton() == MouseEvent.BUTTON1&&e.getClickCount()==1){
x=e.getX();
y=e.getY();
init();
JPanel s=new JPanel();
s.setSize(width,height);
s.setLocation(x,y);
s.setBackground(b);
s.setOpaque(true);
jpa2.add(s);
validate();
}
}
public void mouseReleased(MouseEvent e) {
}
public void mouseEntered(MouseEvent e){
}
public void mouseExited(MouseEvent e){
}
public void mousePressed(MouseEvent e){
}
public static void main(String[] args) {
new Gui2();
}
}