题目:编写一个程序,以允许用户可以从一个JComboBox中选择一个形状,然后用paint方法在随机的位置用随机的尺寸绘制该形状20次。在第一次调用paint方法时,应将JComboBox中的第一个列表项作为默认形状来显示。
下面是我写的代码。问题有3个:
1、第一次调用paint方法绘制Line时点确认按钮没反应,必须是第二次选择Line后才可以绘制;
2、绘制一次后,第二次按确认按钮无法将前一次的绘制结果清除。
3.JLabel标签没有显示,当注释掉paint()方法后才会出现。
我是初学者,望各位强人不吝赐教,拜谢!import java.awt.*;
import java.awt.event.*;
import javax.swing.*;public class Heyu3 extends JFrame{
    private JComboBox shapeBox;
    private JButton yesButton;
    private JLabel shapeLabel, label;
    private String shapeNames[] = { "Line", "Rectangle", "Oval"};
    private String shape;
    
    
    public Heyu3(){
        super("绘制自选图形");
        
        Container container = getContentPane();
        container.setLayout( new FlowLayout() );
        
        shapeLabel = new JLabel("请选择您要绘制的图形:");
        
        shapeBox = new JComboBox( shapeNames );
        shapeBox.setMaximumRowCount( 3 );
        shapeBox.addItemListener(
                
                new ItemListener(){
                    
                    public void itemStateChanged( ItemEvent event ){
                        shape = shapeNames[ shapeBox.getSelectedIndex() ];
                        
                    }
                }
                
        );
        
        yesButton = new JButton("确定");
        yesButton.addActionListener( new YesButtonHandler() );
        
        container.add( shapeLabel );
        container.add( shapeBox );
        container.add( yesButton );
        
        setSize( 500, 500 );
        setVisible( true );
        
    }
    
    private class YesButtonHandler implements ActionListener{
        
        public void actionPerformed( ActionEvent event ){
            
            if( event.getSource() == yesButton)
                
                repaint();
            
        }
    }
    
    public void paint( Graphics g ){
          for(int count=0;count<20;count++){
              
              if(shape.equals("Line"))
                    g.drawLine((int)(Math.random()*100)+50, (int)(Math.random()*100)+150,(int)(Math.random()*100)*(int)(Math.random()*10)+20, (int)(Math.random()*10)*(int)(Math.random()*10)+20);
                
              else if(shape.equals("Rectangle"))
                    g.drawRect((int)(Math.random()*100)+50, (int)(Math.random()*100)+150,(int)(Math.random()*100)*(int)(Math.random()*10)+20, (int)(Math.random()*10)*(int)(Math.random()*10)+20);
                
              else
                    g.drawOval((int)(Math.random()*100)+50, (int)(Math.random()*100)+150,(int)(Math.random()*100)*(int)(Math.random()*10)+20, (int)(Math.random()*10)*(int)(Math.random()*10)+20);
            
          }
        
    }
    public static void main( String args[]){
        Heyu3 application = new Heyu3();
        application.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
    }}

解决方案 »

  1.   

    主要问题是:
    public void paint(Graphics g ){
    第一句话,请写: 
    super.paint(g);
      

  2.   

    Line 是JComboBox的三个选项的第一个,所以是默认显示的,你在绘制Line时应该没有对JComboBox的选项进行选择,由于你对JComboBox监听的是选项改变的事件,所以shapa没变还是null,点击确认按钮时,当然绘不出来了,第二次选择Line时,由于触发了itemStateChanged事件,所以shapa不为null,可以绘出来
      

  3.   


    嗯,这个很好,解决了第二个和第三个问题了,非常感谢!但是第一个问题还是没有解决到,一运行程序就会有:
    Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException的警告提示
      

  4.   


    受教了~但是当我在private String shape;中把shape赋值为Line,编译后,就没有出现第一个问题的情况,却会在没有按确定就自动生成Line图形,那应该如何修改才不这样呢?
      

  5.   


    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;public class Heyu3 extends JFrame {
    private JComboBox shapeBox;
    private JButton yesButton;
    private JLabel shapeLabel, label;
    private String shapeNames[] = {"Line","Rectangle","Oval"};
    private String shape="";//赋初值,以免报空指针遗产
    public Heyu3() {
    super("绘制自选图形");
    Container container = getContentPane();
    container.setLayout(new FlowLayout());
    shapeLabel = new JLabel("请选择您要绘制的图形:");
    shapeBox = new JComboBox(shapeNames);
    shapeBox.setMaximumRowCount(3);
    // shapeBox.addItemListener(这是为那般??我感觉没用,除非你想根据这个触发.
    // new ItemListener(){
    // public void itemStateChanged(ItemEvent event) {
    // shape = shapeNames[shapeBox.getSelectedIndex()];
    // }
    // });
    yesButton = new JButton("确定");
    yesButton.addActionListener(new YesButtonHandler()); container.add(shapeLabel);
    container.add(shapeBox);
    container.add(yesButton); setSize(500, 500);
    setVisible(true); } private class YesButtonHandler implements ActionListener {
    public void actionPerformed(ActionEvent event) {
    if (event.getSource() == yesButton)
    repaint();
    }
    }
    public void paint(Graphics g){
    super.paint(g);
    for (int count = 0; count < 20; count++) {
    shape = (String)shapeBox.getSelectedItem();
    if (shape.equals("Line"))
    g.drawLine((int) (Math.random() * 100) + 50, (int) (Math
    .random() * 100) + 150, (int) (Math.random() * 100)
    * (int) (Math.random() * 10) + 20,
    (int) (Math.random() * 10) * (int) (Math.random() * 10)
    + 20); else if (shape.equals("Rectangle"))
    g.drawRect((int) (Math.random() * 100) + 50, (int) (Math
    .random() * 100) + 150, (int) (Math.random() * 100)
    * (int) (Math.random() * 10) + 20,
    (int) (Math.random() * 10) * (int) (Math.random() * 10)
    + 20); else
    g.drawOval((int) (Math.random() * 100) + 50, (int) (Math
    .random() * 100) + 150, (int) (Math.random() * 100)
    * (int) (Math.random() * 10) + 20,
    (int) (Math.random() * 10) * (int) (Math.random() * 10)
    + 20);
    }
    }
    public static void main(String args[]) {
    Heyu3 application = new Heyu3();
    application.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }}
      

  6.   

    把这句话:
    shape = shapeNames[ shapeBox.getSelectedIndex() ];
    发在确认按钮的单击事件中试试……
     private class YesButtonHandler implements ActionListener{
        
      public void actionPerformed( ActionEvent event ){
        
      if( event.getSource() == yesButton){  shape = shapeNames[ shapeBox.getSelectedIndex() ];
      repaint();  }
      }
      }
      

  7.   


    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;public class Heyu3 extends JFrame {
        private JComboBox shapeBox;
        private JButton yesButton;
        private JLabel shapeLabel, label;
        private String shapeNames[] = {"Line","Rectangle","Oval"};
        private String shape="";//赋初值,以免报空指针异常
        public Heyu3() {
            super("绘制自选图形");
            Container container = getContentPane();
            container.setLayout(new FlowLayout());
            shapeLabel = new JLabel("请选择您要绘制的图形:");
            shapeBox = new JComboBox(shapeNames);
            shapeBox.setMaximumRowCount(3);
    //        shapeBox.addItemListener(这是为那般??我感觉没用,除非你想根据这个触发.
    //        new ItemListener(){
    //            public void itemStateChanged(ItemEvent event) {
    //                shape = shapeNames[shapeBox.getSelectedIndex()];
    //            }
    //        });
            yesButton = new JButton("确定");
            yesButton.addActionListener(new YesButtonHandler());        container.add(shapeLabel);
            container.add(shapeBox);
            container.add(yesButton);        setSize(500, 500);
            setVisible(true);    }    private class YesButtonHandler implements ActionListener {
            public void actionPerformed(ActionEvent event) {
                if (event.getSource() == yesButton)
                 shape = (String) shapeBox.getSelectedItem();//赋值整到这里来
                    repaint();
            }
        }
        public void paint(Graphics g){
            super.paint(g);
            for (int count = 0; count < 20; count++) {
                if (shape.equals("Line"))
                    g.drawLine((int) (Math.random() * 100) + 50, (int) (Math
                            .random() * 100) + 150, (int) (Math.random() * 100)
                            * (int) (Math.random() * 10) + 20,
                            (int) (Math.random() * 10) * (int) (Math.random() * 10)
                                    + 20);            else if (shape.equals("Rectangle"))
                    g.drawRect((int) (Math.random() * 100) + 50, (int) (Math
                            .random() * 100) + 150, (int) (Math.random() * 100)
                            * (int) (Math.random() * 10) + 20,
                            (int) (Math.random() * 10) * (int) (Math.random() * 10)
                                    + 20);            else if (shape.equals("Oval"))//是这里,别整个else
                    g.drawOval((int) (Math.random() * 100) + 50, (int) (Math
                            .random() * 100) + 150, (int) (Math.random() * 100)
                            * (int) (Math.random() * 10) + 20,
                            (int) (Math.random() * 10) * (int) (Math.random() * 10)
                                    + 20);
            }
        }
        public static void main(String args[]) {
            Heyu3 application = new Heyu3();
            
            application.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        }}