2012-10-24 3 views
-1

내 문제는, "triangle.addActionListener (new ShapeAction());"내 코드에서 ShapeAction 기호를 찾을 수 없다고 말하는 오류가있다. 내가 잘못 여기 일을하고 누군가가 내 If 문에서 볼 수 있었다, 할의, 표준은 : 메뉴 항목을 클릭하면 그것은 다른 클래스자바, 청취자 조언, 스윙 컴프가 필요한 조언 : (

public class MyFrame extends javax.swing.JFrame { 
    public MyFrame() { 
     // Create the menu 
     JMenuBar topMenu = new JMenuBar(); 
     this.setJMenuBar(topMenu); 

     //create the menu button "shapes" 
     JMenu shapes = new JMenu("Shapes"); 
     topMenu.add(shapes); 
     //Create the 3 shapes for the menu 
     JMenuItem square = new JMenuItem("Square"); 
     square.addActionListener(new ShapeAction()); 

     JMenuItem circle = new JMenuItem("Circle");  
     circle.addActionListener(new ShapeAction()); 

     JMenuItem triangle = new JMenuItem("Triangle"); 
     triangle.addActionListener(new ShapeAction()); 

     //add shapes to menu 
     shapes.add(circle); 
     shapes.add(triangle); 
     shapes.add(square); 

     //add the menu 
     setJMenuBar(topMenu); 


     MyControlPanel pane = new MyControlPanel(); 
     getContentPane().add(pane); 

     this.add(pane); 
    } 

    /** 
    * @param args the command line arguments 
    */ 
    public static void main(String args[]) { 
     // <snip> 

     /* Create and display the form */ 
     java.awt.EventQueue.invokeLater(new Runnable() { 
       public void run() { 
       new MyFrame().setVisible(true); 
       } 
       }); 

     class ShapeAction implements ActionListener{ 
      public void actionPerformed(ActionEvent e){ 
       JMenuItem clickedMenu = (JMenuItem)e.getSource(); 

       if (clickedMenu.getText().equals("Square")){ 
        //implement abstract methods     
        MyShape aSquare = new ASquare(); 

       } 
       else if (clickedMenu.getText().equals("Circle")){ 
        //implement abstract methods 
        MyShape ACircle = new ACircle(); 

       } 
       else if (clickedMenu.getText().equals("Triangle")){ 
        //implement abstract methods 
        MyShape ATriangle = new ATriangle(); 

       } 
      }   
     } 
    } 

package assignment; 

//import java.awt.FlowLayout; 
import java.awt.BorderLayout; 
import java.awt.FlowLayout; 
import javax.swing.GroupLayout; 
import javax.swing.GroupLayout.Alignment; 
import javax.swing.JLabel; 
import javax.swing.JPanel; 
import javax.swing.JSlider; 
import javax.swing.JTextField; 
import javax.swing.event.ChangeEvent; 
import javax.swing.event.ChangeListener; 


public class MyControlPanel extends javax.swing.JPanel { 

JSlider slider; 
JLabel sliderLabel; 
JLabel sliderdimension; 
JLabel blank; 
JLabel dl; 
JLabel area1; 

/** 
* Creates new form MyControlPanel 
*/ 
public MyControlPanel() { 


    slider = new JSlider(); 
    slider.setValue(50); 
    slider.addChangeListener(new MyChangeAction()); 
    slider.setMajorTickSpacing(10); 
    slider.setPaintLabels(true); 
    slider.setPaintTicks(true); 
    slider.setBounds(300, 50, 100, 50); 

    sliderLabel = new JLabel("50"); 
    blank = new JLabel("  "); 
    sliderdimension = new JLabel("Shape Dimension:"); 

    JTextField boundary_length = new JTextField("Boundary Length"); 
    JTextField area = new JTextField("Area"); 

    dl = new JLabel("Boundary Length ="); 
    area1 = new JLabel("Area ="); 

    setLayout(new BorderLayout()); 


    JPanel sliderPanel = new JPanel(); 
    sliderPanel.setLayout(new FlowLayout(FlowLayout.CENTER, 10, 0)); 


    sliderPanel.add(sliderdimension); 
    sliderPanel.add(sliderLabel); 
    sliderPanel.add(slider); 
    sliderPanel.add(dl); 
    sliderPanel.add(boundary_length); 
    sliderPanel.add(area1); 
    sliderPanel.add(area); 
    this.add(sliderPanel, BorderLayout.PAGE_END); 



} 

/** 
* This method is called from within the constructor to initialize the form. 
* WARNING: Do NOT modify this code. The content of this method is always 
* regenerated by the Form Editor. 
*/ 
// <editor-fold defaultstate="collapsed" desc="Generated Code">       
private void initComponents() { 

    javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this); 
    this.setLayout(layout); 
    layout.setHorizontalGroup(
     layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) 
     .addGap(0, 400, Short.MAX_VALUE) 
    ); 
    layout.setVerticalGroup(
     layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) 
     .addGap(0, 300, Short.MAX_VALUE) 
    ); 
}// </editor-fold>       
// Variables declaration - do not modify      
// End of variables declaration     

public class MyChangeAction implements ChangeListener { 

    //complete code here 
    public void stateChanged(ChangeEvent ce) { 
     int value = slider.getValue(); 
     String str = Integer.toString(value); 
     sliderLabel.setText(str); 


    } 
} // end class 




    } 

에서 그 형태의 인스턴스를 생성
package assignment; 

public abstract class MyShape 
{ 

double thelength; 
double thearea; 



public abstract double computeBoundaryLength(double Length); 

public abstract double computeArea (double Length); 
} 

package assignment; 


public class ACircle extends MyShape { 

@Override 
public double computeBoundaryLength(double Length) 
{ 
    thelength=(2*Length*Math.PI); 
return thelength; 
} 

@Override 
public double computeArea(double Length) 
{ 
    thearea=(Length*Length*Math.PI); 
    return thearea; 
} 

} 

답변

3

당신은 MyFrame 클래스 내부의 main 방법 밖에 내부 클래스 ShapeAction을 소개한다. 그리고 로컬 클래스 대신 main 메서드 클래스를 만듭니다.

import java.awt.event.ActionEvent; 
import java.awt.event.ActionListener; 
import javax.swing.JMenu; 
import javax.swing.JMenuBar; 
import javax.swing.JMenuItem; 


public class MyFrame extends javax.swing.JFrame 
{ 
    public MyFrame() 
    { 
     // Create the menu 
     JMenuBar topMenu = new JMenuBar(); 
     this.setJMenuBar (topMenu); 

     //create the menu button "shapes" 
     JMenu shapes = new JMenu ("Shapes"); 
     topMenu.add (shapes); 
     //Create the 3 shapes for the menu 
     JMenuItem square = new JMenuItem ("Square"); 
     square.addActionListener (new ShapeAction()); 

     JMenuItem circle = new JMenuItem ("Circle"); 
     circle.addActionListener (new ShapeAction()); 

     JMenuItem triangle = new JMenuItem ("Triangle"); 
     triangle.addActionListener (new ShapeAction()); 

     //add shapes to menu 
     shapes.add (circle); 
     shapes.add (triangle); 
     shapes.add (square); 

     //add the menu 
     setJMenuBar (topMenu); 


     MyControlPanel pane = new MyControlPanel(); 
     getContentPane().add (pane); 

     this.add (pane); 
    } 


    @SuppressWarnings("unchecked") 
    // <editor-fold defaultstate="collapsed" desc="Generated Code"> 
    private void initComponents() 
    { 

     setDefaultCloseOperation (javax.swing.WindowConstants.EXIT_ON_CLOSE); 

     javax.swing.GroupLayout layout = new javax.swing.GroupLayout (getContentPane()); 
     getContentPane().setLayout (layout); 
     layout.setHorizontalGroup (
       layout.createParallelGroup (javax.swing.GroupLayout.Alignment.LEADING).addGap (0, 400, Short.MAX_VALUE)); 
     layout.setVerticalGroup (
       layout.createParallelGroup (javax.swing.GroupLayout.Alignment.LEADING).addGap (0, 300, Short.MAX_VALUE)); 

     pack(); 
    }// </editor-fold> 

    /** 
    * @param args the command line arguments 
    */ 
    public static void main (String args[]) 
    { 
     /* Set the Nimbus look and feel */ 
     //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) "> 
     /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel. 
     * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
     */ 
     try 
     { 
      for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) 
      { 
       if ("Nimbus".equals (info.getName())) 
       { 
        javax.swing.UIManager.setLookAndFeel (info.getClassName()); 
        break; 
       } 
      } 
     } 
     catch (ClassNotFoundException ex) 
     { 
      java.util.logging.Logger.getLogger (MyFrame.class.getName()).log (java.util.logging.Level.SEVERE, null, ex); 
     } 
     catch (InstantiationException ex) 
     { 
      java.util.logging.Logger.getLogger (MyFrame.class.getName()).log (java.util.logging.Level.SEVERE, null, ex); 
     } 
     catch (IllegalAccessException ex) 
     { 
      java.util.logging.Logger.getLogger (MyFrame.class.getName()).log (java.util.logging.Level.SEVERE, null, ex); 
     } 
     catch (javax.swing.UnsupportedLookAndFeelException ex) 
     { 
      java.util.logging.Logger.getLogger (MyFrame.class.getName()).log (java.util.logging.Level.SEVERE, null, ex); 
     } 
     //</editor-fold> 

     /* Create and display the form */ 
     java.awt.EventQueue.invokeLater (new Runnable() 
     { 
      public void run() 
      { 
       new MyFrame().setVisible (true); 
      } 
     }); 
    } 

    class ShapeAction implements ActionListener 
    { 
     public void actionPerformed (ActionEvent e) 
     { 
      JMenuItem clickedMenu = (JMenuItem) e.getSource(); 

      if (clickedMenu.getText().equals ("Square")) 
      { 
       //implement abstract methods 
       MyShape aSquare = new ASquare(); 

      } 
      else if (clickedMenu.getText().equals ("Circle")) 
      { 
       //implement abstract methods 
       MyShape ACircle = new ACircle(); 

      } 
      else if (clickedMenu.getText().equals ("Triangle")) 
      { 
       //implement abstract methods 
       MyShape ATriangle = new ATriangle(); 

      } 
     } 
    } 
} 

추신 :

단순히 main 방법 밖에 이동 그리고 코드 끝에 하나의 닫는 괄호를 잊어 버렸습니다. 예를 통해 그것을 추가했습니다 ...

+0

이렇게하는 것이 좋을까요? – john

+0

@StevenMcilhone 내가 추가 한 코드를 참조하십시오. –

+0

생명의 은인 :) 각 모양의 치수를 업데이트하기 위해 내 슬라이더를 얻는 방법에 대한 아이디어가 있습니까? 그런 다음 경계 길이와 영역 jtextfeild에 값을 출력 하시겠습니까? – john