2012-10-24 53 views
0

即時通訊嘗試獲取我的jslider的值並輸入它,以便它是從我的第一段代碼中創建的if語句創建的每個形狀的邊界長度和區域的值,然後更新我的2個jtextfields的邊界長度和麪積的答案,關於如何做到這一點的任何提示?動作偵聽器建議需要

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

讓你的文本框類變量,並把這兩行你stateChanged方法:

boundary_length.setText(str); 
area.setText(str); 

此外,請確保使用固定數量的列創建文本框,以便它們不會根據顯示的文本更改長度。

UPDATE

我做了一些修改代碼以滑塊的值傳遞給您的自定義形狀:

  • MyControlPanel成爲一個內部類的MyFrame
  • 文本框變類變量MyControlPanel
  • 滑塊變爲MyFrame的類變量,因此它的值可以是訪問ShapeAction

示例代碼:

import java.awt.BorderLayout; 
import java.awt.FlowLayout; 
import java.awt.event.ActionEvent; 
import java.awt.event.ActionListener; 

import javax.swing.JLabel; 
import javax.swing.JMenu; 
import javax.swing.JMenuBar; 
import javax.swing.JMenuItem; 
import javax.swing.JPanel; 
import javax.swing.JSlider; 
import javax.swing.JTextField; 
import javax.swing.event.ChangeEvent; 
import javax.swing.event.ChangeListener; 

public class MyFrame extends javax.swing.JFrame { 

    JSlider slider; 

    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("Circle")){ 
        int value = slider.getValue(); 
        MyShape ACircle = new ACircle(value); 
       } 
      }   
     } 

     public class MyControlPanel extends javax.swing.JPanel { 

      JLabel sliderLabel; 
      JLabel sliderdimension; 
      JLabel blank; 
      JLabel dl; 
      JLabel area1; 
      JTextField boundary_length = new JTextField("Boundary Length"); 
      JTextField area = new JTextField("Area"); 

      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:"); 

       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); 
      } 

      public class MyChangeAction implements ChangeListener { 
       public void stateChanged(ChangeEvent ce) { 
        int value = slider.getValue(); 
        String str = Integer.toString(value); 
        sliderLabel.setText(str); 
        boundary_length.setText(str); 
        area.setText(str); 
       } 
      } 
     } 
} 

您還應該添加一個構造函數來你的形狀類(如ACircle),其作爲輸入滑塊的值(如你在其他方法要使用它)

public class ACircle extends MyShape { 

    int value; 

    public ACircle(int value) { 
     this.value = value; 
    } 

    // other methods 
} 
+0

那些形狀有一個超級類,我可以把它放在那裏嗎? – john

+0

如果你指的是抽象類MyShape,那麼你可以在該類中使用構造函數,但是你還需要其他具體形狀類的構造函數來使用'super'調用父構造函數並傳遞適當的值。這是基本的Java的東西,你應該閱讀並嘗試一點點。 – Rempelos