想在下列代码上添加上新建与保存但是都失败了.求救下
import java.awt.*; 
import java.awt.event.*; 
import javax.swing.*; public class Painter2 extends JFrame { 
    private int topX, topY, width, fontSize, height, bottomX, bottomY, shape; 
    private boolean clear, textOn, filled; 
    private Color drawingColor; 
    private String font; 
    private JTextField text; 
    private JPanel panel1, panel2, panel3; 
    private JRadioButton ovalBox, rectBox, lineBox; 
    private ButtonGroup shapeGroup; 
    private JCheckBox fillBox; 
    private JComboBox colorList, fontList, sizeList; 
    private JButton clearButton; 
    private String colorNames[] = { "Black", "Green", "Blue", "Red", "Cyan" }; 
    private Color colors[] = { Color.black, Color.green, Color.blue, Color.red, 
            Color.cyan };     private String fontNames[] = { "Serif", "SansSerif", "Monospaced"     };     private String szieNames[] = { "9", "10", "22", "72" }; 
    private int sizes[] = { 9, 10, 22, 72 }; 
    private final int OVAL = 1, LINE = 2, RECT = 3; 
    private ToolWindow tools;     public Painter2() { 
        addMouseListener(new MouseHandler()); 
         
        drawingColor = Color.black; 
        shape = OVAL; 
        font = "Serif"; 
        fontSize = 9; 
        setSize(300, 300); 
        setVisible(true); 
        tools = new ToolWindow(); 
    }     public void paint(Graphics g) { 
        g.setColor(drawingColor); 
        if (textOn) { 
            g.setFont(new Font(font, Font.PLAIN, fontSize)); 
            g.drawString(text.getText(), topX, topY); 
            textOn = false; 
            return;         } 
        
        if (shape != LINE) { 
            topX = Math.min(topX, bottomX); 
            topY = Math.min(topY, bottomY); 
        } 
        if (filled && shape != LINE) 
            switch (shape) { 
            case OVAL: 
                g.fillOval(topX, topY, width, height); 
                break; 
            case RECT: 
                g.fillRect(topX, topY, width, height); 
                break; 
            } 
        else 
            switch (shape) { 
            case OVAL: 
                g.drawOval(topX, topY, width, height); 
                break; 
            case LINE: 
                g.drawLine(topX, topY, width, height); 
                break; 
            case RECT: 
                g.drawRect(topX, topY, width, height); 
                break; 
            } 
        if (clear == true) { 
            g.setColor(Color.white); 
            g.fillRect(0, 0, getSize().width, getSize().height); 
            clear = false; 
        } 
    }     private class ToolWindow extends JFrame { 
        public ToolWindow() { 
            text = new JTextField("Text", 20); 
            text.addActionListener(new ActionListener() { 
                public void actionPerformed(ActionEvent event) { 
                    textOn = true; 
                    repaint();                 }             }             );             fontList = new JComboBox(fontNames); 
            fontList.setMaximumRowCount(3); 
            fontList.addItemListener(new ItemListener() { 
                public void itemStateChanged(ItemEvent event) { 
                    font = fontNames[fontList.getSelectedIndex()];                 }             }             );             sizeList = new JComboBox(szieNames);             sizeList.setMaximumRowCount(3); 
            sizeList.addItemListener(new ItemListener() { 
                public void itemStateChanged(ItemEvent event) { 
                    font = fontNames[sizeList.getSelectedIndex()];                 }             }             );             colorList = new JComboBox(colorNames);             colorList.setMaximumRowCount(3); 
            colorList.addItemListener(new ItemListener() { 
                public void itemStateChanged(ItemEvent event) { 
                    drawingColor = colors[colorList.getSelectedIndex()]; 
                } 
            }); 
            clearButton = new JButton("Clear"); 
            clearButton.addActionListener(new ClearButtonHandler()); 
            fillBox = new JCheckBox("filled"); 
          // FillBoxHandler fillHandle = new FillBoxHandler(); 
            fillBox.addItemListener(new FillBoxHander()); 
            ovalBox = new JRadioButton("Oval", true); 
            lineBox = new JRadioButton("Line", false); 
            rectBox = new JRadioButton("Rect", false); 
            RadioButtonHandler handler = new RadioButtonHandler(); 
            ovalBox.addItemListener(handler); 
            lineBox.addItemListener(handler); 
            rectBox.addItemListener(handler); 
            
            shapeGroup = new ButtonGroup(); 
            shapeGroup.add(ovalBox); 
            shapeGroup.add(lineBox); 
            shapeGroup.add(rectBox); 
            panel1 = new JPanel(); 
            panel2 = new JPanel(); 
            panel3 = new JPanel(); 
            panel1.setLayout(new GridLayout(1, 3)); 
            panel2.setLayout(new GridLayout(1, 2)); 
            panel3.setLayout(new GridLayout(1, 3));             panel1.add(ovalBox); 
panel1.add(rectBox); 
            panel1.add(lineBox); 
            panel2.add(fillBox); 
            panel2.add(clearButton); 
            panel3.add(new JScrollPane(colorList)); 
            panel3.add(new JScrollPane(fontList)); 
            panel3.add(new JScrollPane(sizeList)); 
            Container container = getContentPane(); 
            container.setLayout(new FlowLayout()); 
            container.add(panel1); 
            container.add(panel2); 
            container.add(panel3); 
            container.add(text);             setSize(350, 200); 
            setLocation(300, 0); 
            setVisible(true); 
        } 
    }     private class MouseHandler extends MouseAdapter { 
        public void mousePressed(MouseEvent event) { 
            topX = event.getX(); 
            topY = event.getY();         }         public void mouseReleased(MouseEvent event) { 
            bottomX = event.getX(); 
            bottomY = event.getY(); 
            width = Math.abs(topX - bottomX); 
            height = Math.abs(topY - bottomY); 
            repaint(); 
        }     }     private class ClearButtonHandler implements ActionListener { 
        public void actionPerformed(ActionEvent event) { 
            clear = true; 
            repaint();         } 
    }     private class RadioButtonHandler implements ItemListener { 
        public void itemStateChanged(ItemEvent event) { 
            if (event.getSource() == ovalBox) 
                shape = OVAL; 
            else if (event.getSource() == lineBox) 
                shape = LINE; 
            if (event.getSource() == rectBox) 
                shape = RECT;         }     }     private class FillBoxHander implements ItemListener { 
        public void itemStateChanged(ItemEvent event) { 
            if (event.getStateChange() == ItemEvent.SELECTED) 
                filled = true; 
            else 
                filled = false;         }     }     public static void main(String arg[]) { 
        Painter2 application = new Painter2(); 
        application.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);     } 

解决方案 »

  1.   

    上面的发错了..代码了.这个才是真的
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;public class MenuTest extends JFrame {
       private final Color colorValues[] = 
          { Color.black, Color.blue, Color.red, Color.green };   
       private JRadioButtonMenuItem colorItems[], fonts[];
       private JCheckBoxMenuItem styleItems[];
       private JLabel displayLabel;
       private ButtonGroup fontGroup, colorGroup;
       private int style;
       public MenuTest()
       {
          super( "Using JMenus" );          
          JMenu fileMenu = new JMenu( "File" );
          fileMenu.setMnemonic( 'F' );
          JMenuItem aboutItem = new JMenuItem( "About..." );
          aboutItem.setMnemonic( 'A' );
          fileMenu.add( aboutItem );
          aboutItem.addActionListener(         new ActionListener() {           
                public void actionPerformed( ActionEvent event )
                {
                   JOptionPane.showMessageDialog( MenuTest.this,
                      "快做死人了",
                      "About", JOptionPane.PLAIN_MESSAGE );
                }         }        ); 
     
         
          JMenuItem exitItem = new JMenuItem( "Exit" );
          exitItem.setMnemonic( 'x' );
          fileMenu.add( exitItem );
          exitItem.addActionListener(         new ActionListener() {          
                public void actionPerformed( ActionEvent event )
                {
                   System.exit( 0 );
                }         }       );      
          JMenuBar bar = new JMenuBar();  
          setJMenuBar( bar );  
          bar.add( fileMenu );         
          JMenu formatMenu = new JMenu( "Format" );  
          formatMenu.setMnemonic( 'r' );     
          String colors[] = { "Black", "Blue", "Red", "Green" };      JMenu colorMenu = new JMenu( "Color" );
          colorMenu.setMnemonic( 'C' );      colorItems = new JRadioButtonMenuItem[ colors.length ];
          colorGroup = new ButtonGroup();
          ItemHandler itemHandler = new ItemHandler();  
          for ( int count = 0; count < colors.length; count++ ) {
             colorItems[ count ] = 
                new JRadioButtonMenuItem( colors[ count ] );
             colorMenu.add( colorItems[ count ] );
             colorGroup.add( colorItems[ count ] );
             colorItems[ count ].addActionListener( itemHandler );
          }     
          colorItems[ 0 ].setSelected( true );       
          formatMenu.add( colorMenu );
          formatMenu.addSeparator();      String fontNames[] = { "Serif", "Monospaced", "SansSerif" };      JMenu fontMenu = new JMenu( "Font" );
          fontMenu.setMnemonic( 'n' );      fonts = new JRadioButtonMenuItem[ fontNames.length ];
          fontGroup = new ButtonGroup();
          for ( int count = 0; count < fonts.length; count++ ) {
             fonts[ count ] = new JRadioButtonMenuItem( fontNames[ count ] );
             fontMenu.add( fonts[ count ] );
             fontGroup.add( fonts[ count ] );
             fonts[ count ].addActionListener( itemHandler );
          }
          fonts[ 0 ].setSelected( true );      fontMenu.addSeparator();      
          String styleNames[] = { "Bold", "Italic" };      styleItems = new JCheckBoxMenuItem[ styleNames.length ];
          StyleHandler styleHandler = new StyleHandler();  
          for ( int count = 0; count < styleNames.length; count++ ) {
             styleItems[ count ] = 
                new JCheckBoxMenuItem( styleNames[ count ] );
             fontMenu.add( styleItems[ count ] );
             styleItems[ count ].addItemListener( styleHandler );
          }    
          formatMenu.add( fontMenu );  
          bar.add( formatMenu );  
         
          
          displayLabel = new JLabel( "Sample Text", SwingConstants.CENTER );
          displayLabel.setForeground( colorValues[ 0 ] );
          displayLabel.setFont( new Font( "Serif", Font.PLAIN, 72 ) );      getContentPane().setBackground( Color.CYAN );
          getContentPane().add( displayLabel, BorderLayout.CENTER );      setSize( 500, 200 );
          setVisible( true );   }   public static void main( String args[] )
       {
          MenuTest application = new MenuTest();
          application.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
       }  
       private class ItemHandler implements ActionListener {      
          public void actionPerformed( ActionEvent event )
          {
             
             for ( int count = 0; count < colorItems.length; count++ )
      
                if ( colorItems[ count ].isSelected() ) {
                   displayLabel.setForeground( colorValues[ count ] );
                   break;
                }
     
             
             for ( int count = 0; count < fonts.length; count++ )            if ( event.getSource() == fonts[ count ] ) {
                   displayLabel.setFont( 
                      new Font( fonts[ count ].getText(), style, 72 ) );
                   break;
                }         repaint();        }    }  
       private class StyleHandler implements ItemListener {     
          public void itemStateChanged( ItemEvent e )
          {
             style = 0; 
             if ( styleItems[ 0 ].isSelected() )
                style += Font.BOLD;     
             if ( styleItems[ 1 ].isSelected() )
                style += Font.ITALIC;         displayLabel.setFont( 
                new Font( displayLabel.getFont().getName(), style, 72 ) );         repaint();
          }   } 

      

  2.   

    public void paint(Graphics g) { 
    在这句话下面加上
    super.paintComponents(g);
    就可以了
      

  3.   

    看看我的记事本,新建,保存都有的
    http://download.csdn.net/source/255532