分享

Java学习——107、圆、矩形

 pythonjava学习 2021-04-06

上篇中,帖了Choose类,此类中包含有一个Toppane的对象,即本篇所帖上来的。

之所以要把Toppane单出来成一个类,原因是如果将这所有代码都放在Chooser类中,我会觉得那个类里面代码太多,看起来好像不太友好。

3、Toppane类

import java.awt.Color;

import java.awt.event.ActionEvent;

import java.awt.event.ActionListener;

import javax.swing.*;

public class Toppane implements ActionListener{

    private JComboBox type;

    private JRadioButton yes_fill;

    private JButton color_button,back_button;

    private Color c;

    private Color backc;

    private Tu_canvas vas;

    public JComboBox init_type(){

       String s[]={"","矩形"};

       type=new JComboBox();

       for(int i=0;i<s.length;i++){

           type.addItem(s[i]); 

       }

       type.addActionListener(this);

       return type;

    }

    public JRadioButton init_fill(){

       yes_fill=new JRadioButton("填充");

       return yes_fill;

    }

    public JButton init_button(){

       color_button=new JButton("前景色");

       color_button.addActionListener(this);

       returncolor_button;

    }

    public JButton init_backbutton(){

       back_button=new JButton("背景色");

       back_button.addActionListener(this);

       returnback_button;

    }

    public Tu_canvas set_vas(Tu_canvas vas){

       this.vas=vas;

       return vas;

    }

    public Color get_backcolor(){

       returnbackc;

    }

    public Color get_color(){

       returnc;

    }

    publicint get_type(){

       if(type.getSelectedIndex()==0)

           return 0;

       else if(type.getSelectedIndex()==1)

           return 1;

       else

           return 1;    

    }

    public int get_fill(){

       if(yes_fill.isSelected()==true)

           return 1;

       else

           return 0;

    }

    @Override

    public void actionPerformed(ActionEvent e) {

       // TODO Auto-generatedmethod stub

       if(e.getSource()==color_button)

       {

           c=JColorChooser.showDialog(color_button,"颜色选择", Color.cyan);

           vas.set_color(c);

       }

       if(e.getSource()==back_button){

           backc=JColorChooser.showDialog(color_button,"颜色选择", Color.red);

           vas.setBackground(backc);

           vas.repaint();

       }

       vas.set_fill(this.get_fill());

       vas.set_type(this.get_type());

       vas.repaint();

    }

}

    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多