java学习——106.圆和矩形
本篇及接下来几篇,都是介绍一个小小的程序,其运行结果如下图所示:
通过选择,可决定画的图形及图形的部分属性。
如:
本例总共包含四个类。
1、第一个类,包含着main方法,程序从此处开始运行 。
public class Main {
private static Choose choose;
public static void main(String args[]){
choose=new Choose();
}
}
2、Choose类,其完整代码如下:
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
public class Choose extends JFrame implements ActionListener{
private Toppane top;
private JComboBox type;
private JRadioButton yes_fill;
public static JTextField test;
private Tu_canvas vas;
private JButton button,circlebutton,rectbutton;
private JTextField radius,chang,kuan;
private JPanel top_pane,middle_pane;
public Choose(){
super("简单画图");
this.setVisible(true);
int screenWidth=((int)java.awt.Toolkit.getDefaultToolkit().getScreenSize().width);
int screenHeight = ((int)java.awt.Toolkit.getDefaultToolkit().getScreenSize().height);
this.setBounds(0,0,screenWidth,screenHeight);
top=new Toppane();
top_pane=new JPanel();
type=top.init_type();//类型,圆还是矩形等
top_pane.add(type);
yes_fill=top.init_fill();
top_pane.add(yes_fill);//按钮,是否填充
top_pane.add(top.init_button());//前颜色
top_pane.add(top.init_backbutton());//背颜色
this.getContentPane().add(top_pane,"North");
button=new JButton("确定");
button.addActionListener(this);
top_pane.add(button);
//test=new JTextField(10);
//top_pane.add(test);
middle_pane=new JPanel();
middle_pane.setLayout(new BorderLayout());
vas=new Tu_canvas();
vas.setSize(600, 600);
middle_pane.add(vas);
this.getContentPane().add(middle_pane,"Center");
top.set_vas(vas);//传参
}
public void get_pane(int i){
if(i==0){
middle_pane.removeAll();
middle_pane.add(Circle_pane(),"North");
middle_pane.add(vas);
}
else if(i==1){
middle_pane.removeAll();
middle_pane.add(Rectangle_pane(),"North");
middle_pane.add(vas);
}
}
public JPanel Circle_pane(){
JPanel pane=new JPanel();
pane.setBackground(Color.lightGray);
pane.add(new JLabel("请输入圆的半径:"));
radius=new JTextField(10);
pane.add(radius);
circlebutton=new JButton("画圆");
circlebutton.addActionListener(this);
pane.add(circlebutton);
return pane;
}
public JPanel Rectangle_pane(){
JPanel panel=new JPanel();
panel.setBackground(Color.lightGray);
panel.add(new JLabel("请输入矩形的长:"));
chang=new JTextField(10);
panel.add(chang);
panel.add(new JLabel("请输入矩形的宽:"));
kuan=new JTextField(10);
panel.add(kuan);
rectbutton=new JButton("画矩形");
rectbutton.addActionListener(this);
panel.add(rectbutton);
return panel;
}
@Override
publicvoid actionPerformed(ActionEvent e) {
// TODO Auto-generatedmethod stub
if(e.getSource()==circlebutton){
if(radius.getText().equals(""))
radius.setText("请输入半径");
else{
int r=Integer.parseInt(radius.getText());
vas.set_r(r);
vas.set_fill(top.get_fill());
vas.set_type(top.get_type());
vas.repaint();
}
}
if(e.getSource()==rectbutton){
if(chang.getText().equals("")){
chang.setText("请输入矩形的长");
}
elseif(kuan.getText().equals("")){
kuan.setText("请输入矩形的宽");
}
else{
int width=Integer.parseInt(chang.getText());
int height=Integer.parseInt(kuan.getText());
vas.set_width(width);
vas.set_height(height);
vas.set_fill(top.get_fill());
vas.set_type(top.get_type());
vas.repaint();
}
}
if(e.getSource()==button){
int i=top.get_type();
this.get_pane(i);
}
}
}