回 帖 发 新 帖 刷新版面

主题:求一道JAVA题的答案

import java.awt.*;
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import java.awt.geom.*;
public class c1_0 extends JApplet implements ActionListener
{


  Container cp=getContentPane();
  JButton bt1=new JButton();
   JButton bt2=new JButton();
   JButton bt3=new JButton();
 
public void init()
 {   bt1.setRolloverEnabled(true);  
bt2.setRolloverEnabled(true);  
bt3.setRolloverEnabled(true);

bt1.setText("画圆");
   bt2.setText("画直线");
    bt3.setText("画椭圆");
  
  bt1.setHorizontalTextPosition(JLabel.CENTER);
  bt1.setVerticalTextPosition(JLabel.BOTTOM);
 bt2.setHorizontalTextPosition(JLabel.CENTER);
  bt2.setVerticalTextPosition(JLabel.BOTTOM);

 bt3.setHorizontalTextPosition(JLabel.CENTER);
  bt3.setVerticalTextPosition(JLabel.BOTTOM);
cp.add(bt1,BorderLayout.SOUTH);
cp.add(bt2,BorderLayout.CENTER);
cp.add(bt3,BorderLayout.NORTH);
bt1.addActionListener(this);
bt2.addActionListener(this);
bt3.addActionListener(this);
  }
public  void paint(Graphics g )
{
   Graphics2D g2=(Graphics2D)g;
Ellipse2D.Double e1=new Ellipse2D.Double(100,100,50,50); 
Line2D.Double l1=new Line2D.Double(100,100,200,200);
Ellipse2D.Double e2=new Ellipse2D.Double(300,300,60,40); 
g2.draw(e1);
g2.draw(l1);
g2.draw(e2);
}
public void actionPerformed(ActionEvent e)
 {  
    if(e.getSource()==bt1) repaint();
      
if(e.getSource()==bt2)  repaint();
      

if(e.getSource()==bt3)repaint();
      

}

 
 
}











回复列表 (共7个回复)

沙发

怎么事先点击哪一个按钮就画哪个图形的功能
请好心人帮助以下

板凳

把paint函数分开成几个写就行了啊,或者在paint里面作判断点了哪个按钮,具体作什么动作

3 楼

paint 方法不是只能写一次吗?怎样分开写具体不太懂请打下说情一点最好给俺把代码写出。谢了

4 楼

我测试过可以用了~不过......(本人初学者)



import java.awt.*;
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import java.awt.geom.*;

public class c1_0 extends JApplet implements ActionListener
{

       Container cp=getContentPane();
       JButton bt1=new JButton("画圆");
       JButton bt2=new JButton("画直线");
       JButton bt3=new JButton("画椭圆");
       String shape;
     
    public void init()
     {   
        
        bt1.setHorizontalTextPosition(JLabel.CENTER);
        bt1.setVerticalTextPosition(JLabel.BOTTOM);
        
        bt2.setHorizontalTextPosition(JLabel.CENTER);
        bt2.setVerticalTextPosition(JLabel.BOTTOM);
        
        bt3.setHorizontalTextPosition(JLabel.CENTER);
        bt3.setVerticalTextPosition(JLabel.BOTTOM);
        cp.add(bt1,BorderLayout.SOUTH);
        
        cp.add(bt2,BorderLayout.CENTER);
        cp.add(bt3,BorderLayout.NORTH);
        
        bt1.addActionListener(this);
        bt2.addActionListener(this);
        bt3.addActionListener(this);
     }
    
    public  void paint(Graphics g )
    {
        Graphics2D g2=(Graphics2D)g;
        
        Ellipse2D.Double e1=new Ellipse2D.Double(100,100,50,50); 
        
        Line2D.Double l1=new Line2D.Double(100,100,200,200);
        
        Ellipse2D.Double e2=new Ellipse2D.Double(300,300,60,40); 
        
        if(shape.equals("画圆"))
             g2.draw(e1);
        else if(shape.equals("画直线"))
             g2.draw(l1);
        else
             g2.draw(e2);
    }
    
    public void actionPerformed(ActionEvent e)
    {  
        if(e.getSource()==bt1) 
            shape = "画圆";
        
        else if(e.getSource()==bt2)
            shape = "画直线";
        
        else
            shape = "画椭圆";
            
        repaint();
        

    }

}

5 楼

厉害的确能运行谢谢了!!!!!!!!!!

6 楼

if(shape.equals("画圆"))
             g2.draw(e1);
        else if(shape.equals("画直线"))
             g2.draw(l1);
        else
             g2.draw(e2);
不太懂shape.equals()是什么方法阿?拜托在给解释一下,我们教材上没有。

7 楼

我懂了不用劳烦专家回复了

我来回复

您尚未登录,请登录后再回复。点此登录或注册