java绘图问题

importjava.awt.*;importjavax.swing.*;importjava.awt.event.*;importjava.awt.event.Wind... import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import java.awt.event.WindowAdapter;

public class Draw1{

public static final int YES_NO_OPTION = 0;
public static void main(String[] args){
DrawFrame drawFrame=new DrawFrame();
drawFrame.addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e){
int result=JOptionPane.showConfirmDialog(null, "确认退出",null,YES_NO_OPTION);
if(result==JOptionPane.YES_OPTION)
System.exit(0);
else
return;
}
});
drawFrame.setVisible(true);
drawFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
class DrawFrame extends JFrame{

JPanel jp1=new DrawPanel();
public DrawFrame(){
setLayout(new BorderLayout());
setSize(200,300);
setTitle("123");
add(jp1);
}
}
class DrawPanel extends JPanel{

enum Flag{STRING,RECT,CIRCLE};
Flag flag;
private JPanel jp=new JPanel();
private JPanel controlPane=new JPanel();
private ButtonGroup buttonGroup=new ButtonGroup();
private JButton yellow=new JButton("yellow");
private JButton red=new JButton("red");

private JRadioButton a= new JRadioButton("String",false);
private JRadioButton b= new JRadioButton("Rect",false);
private JRadioButton c= new JRadioButton("circle",false);
public DrawPanel(){

//事件监听
ActionListener actionListener=new ActionListener(){
public void actionPerformed(ActionEvent e){
if((JRadioButton)e.getSource()==a)
flag=Flag.STRING;
if((JRadioButton)e.getSource()==b)
flag=Flag.RECT;
if((JRadioButton)e.getSource()==c)
flag=Flag.CIRCLE;
if((JButton)e.getSource()==yellow)
jp.setBackground(Color.YELLOW);
if((JButton)e.getSource()==red)
jp.setBackground(Color.RED);
}
};
controlPane.setLayout(new GridLayout(2,1));
controlPane.add(yellow);
yellow.addActionListener(actionListener);
controlPane.add(red);
red.addActionListener(actionListener);

controlPane.add(a);
controlPane.add(b);
controlPane.add(c);

buttonGroup.add(a);
a.addActionListener(actionListener);
buttonGroup.add(b);
b.addActionListener(actionListener);
buttonGroup.add(c);
c.addActionListener(actionListener);

add(jp);
add(controlPane,BorderLayout.NORTH);
}
public void paintComponent(Graphics g){
super.paintComponent(g);
if(flag==Flag.STRING)
g.drawString("abc",100,50);
if(flag==Flag.RECT)
g.fillRect(20,20,130,140);
if(flag==Flag.CIRCLE)
g.fillOval(20,20,150,210);

}
}
编译通过,运行时候事件监听出现问题
求大牛能调试一下
展开
 我来答
死后是好人
推荐于2016-01-23 · TA获得超过2638个赞
知道小有建树答主
回答量:628
采纳率:0%
帮助的人:966万
展开全部

你这个主要是布局管理器太过混乱,窗口大小定义不当

class DrawPanel extends JPanel{

你的public void paintComponent(Graphics g){

是在DrawPanel区域内执行的

结果你直接

 add(jp);    

    add(controlPane,BorderLayout.NORTH);

一、没有对DrawPanel进行布局管理器管理

二、直接调用add()方法,覆盖了你的作图区域

三、建议你引用ActionListener接口,继承只能继承一个,接口则无此限制

四、修改窗口大小,以配合程序

五、附赠你一段使窗口居中显示的代码

import java.awt.*;

import javax.swing.*;

import javax.swing.border.Border;

import java.awt.event.*;

public class Draw1{

  public static final int YES_NO_OPTION = 0;

  public static void main(String[] args){

    DrawFrame drawFrame=new DrawFrame();

    drawFrame.addWindowListener(new WindowAdapter(){

      public void windowClosing(WindowEvent e){

        int result=JOptionPane.showConfirmDialog(null, "确认退出",null,YES_NO_OPTION);

        if(result==JOptionPane.YES_OPTION)

       System.exit(0);

        else

          return;

      }

    });

    drawFrame.setVisible(true);

    drawFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

  }

}

class DrawFrame extends JFrame{

Dimension scrnsize;

Toolkit toolkit = Toolkit.getDefaultToolkit();

  JPanel jp1=new DrawPanel();

  public DrawFrame(){

    setLayout(new BorderLayout());

    setSize(300,400);

    scrnsize = toolkit.getScreenSize();

setLocation(scrnsize.width / 2 - getWidth() / 2,

scrnsize.height / 2 - getHeight() / 2);

    setTitle("123");

    add(jp1);  

  } 

}

class DrawPanel extends JPanel implements ActionListener{

  enum Flag{STRING,RECT,CIRCLE};

  Flag flag;

  private JPanel total=new JPanel();

  private JPanel jp=new JPanel();

  private JPanel controlPane=new JPanel();

  private ButtonGroup buttonGroup=new ButtonGroup();  

  private JButton yellow=new JButton("yellow");

  private JButton red=new JButton("red");

  private JRadioButton a= new JRadioButton("String",false);

  private JRadioButton b= new JRadioButton("Rect",false);

  private JRadioButton c= new JRadioButton("circle",false);

  public DrawPanel(){

         jp.setLayout(new GridLayout(1,2));

    jp.add(yellow);

    yellow.addActionListener(this);

    jp.add(red);

    red.addActionListener(this);

    controlPane.add(a);

    controlPane.add(b);

    controlPane.add(c);

    buttonGroup.add(a);

    a.addActionListener(this);

    buttonGroup.add(b);

    b.addActionListener(this);

    buttonGroup.add(c);

    c.addActionListener(this);

    this.setLayout( new BorderLayout());

    total.setLayout(new BorderLayout());

    total.add(jp,BorderLayout.NORTH);

    total.add(controlPane,BorderLayout.SOUTH);

    //add(jp,BorderLayout.SOUTH);    

    //add(controlPane,BorderLayout.SOUTH);

    this.add(total,BorderLayout.SOUTH);

   }

//事件监听

    public void actionPerformed(ActionEvent e){

       if(e.getSource()==a)

        flag=Flag.STRING;  

       if(e.getSource()==b)

        flag=Flag.RECT;

       if(e.getSource()==c)

         flag=Flag.CIRCLE;

       if(e.getSource()==yellow)

         this.setBackground(Color.YELLOW);

       if(e.getSource()==red)

         this.setBackground(Color.RED);

    }

   public void paintComponent(Graphics g){

      super.paintComponent(g);

      if(flag==Flag.STRING)

        g.drawString("abc",100,50); 

      if(flag==Flag.RECT)

        g.fillRect(20,20,130,140);

      if(flag==Flag.CIRCLE)

       g.fillOval(20,20,150,210);   

   }

}

百度网友7287170
2011-09-04 · 超过31用户采纳过TA的回答
知道答主
回答量:58
采纳率:0%
帮助的人:71.8万
展开全部
//事件监听 以下就是把强制转换类型去掉 不过貌似你其他地方有问题
ActionListener actionListener=new ActionListener(){
public void actionPerformed(ActionEvent e){
if(e.getSource()==a)
flag=Flag.STRING;
if(e.getSource()==b)
flag=Flag.RECT;
if(e.getSource()==c)
flag=Flag.CIRCLE;
if(e.getSource()==yellow)
jp.setBackground(Color.YELLOW);
if(e.getSource()==red)
jp.setBackground(Color.RED);
}
};
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式