ArrayList用于存储单击按钮时执行的操作

时间:2013-12-04 15:23:30

标签: java arraylist

我的arrayList遇到了很多困难。我有三个按钮(在我的jPanelBottom页面中),当我点击开始按钮时,我从那时开始做的每个动作(从菜单中选择选项或移动jSlider)都应该被记录下来。我点击停止按钮,它应该停止录制,一旦我点击播放按钮,我所做的每一个动作都应该输出到屏幕。我不知道该怎么做。

我会附上我的一些课程,以便你能够看到。

这是我的JPanelBottom页面,其中包含我创建的所有内容和我的更改事件(对于jSlider)

public class jPanelBottom extends javax.swing.JPanel {

private JTextField jtfBoundaryLength, jtfArea;
private JSlider jsShapes;
private JLabel jLabelBoundaryLength, jLabelArea, jLabelSlider;
private JButton jbStart, jbStop, jbPlay;
//private JPanel jpBottom;

public jPanelBottom() {
    initComponents();

    //JSlider jsShapes = new JSlider();
    jsShapes = new javax.swing.JSlider();
    jsShapes.setMajorTickSpacing(10);
    jsShapes.setPaintLabels(true);
    jsShapes.setPaintTicks(true);
    jsShapes.setSize(300, 100);
    this.add(jsShapes);
    jsShapes.addChangeListener(new event());

    jtfBoundaryLength = new JTextField();
    jtfBoundaryLength.setSize(100, 25);
    jtfBoundaryLength.setLocation(550, 30);
    this.add(jtfBoundaryLength);

    jLabelBoundaryLength = new JLabel();
    jLabelBoundaryLength.setText("Boundary Length: ");
    jLabelBoundaryLength.setSize(130, 25);
    jLabelBoundaryLength.setLocation(400, 30);
    this.add(jLabelBoundaryLength);

    jtfArea = new JTextField();
    jtfArea.setSize(100, 25);
    jtfArea.setLocation(550, 60);

    this.add(jtfArea);

    jLabelArea = new JLabel();
    jLabelArea.setText("Area: ");
    jLabelArea.setSize(100, 25);
    jLabelArea.setLocation(400, 60);
    this.add(jLabelArea);

    jLabelSlider = new JLabel();
    jLabelSlider.setText("Select the size please");
    jLabelSlider.setSize(150, 150);
    jLabelSlider.setLocation(85, 30);
    this.add(jLabelSlider);

    jbStart = new JButton();
    jbStart.setText("Start");
    jbStart.setSize(80, 25);
    jbStart.setLocation(400, 95);
    this.add(jbStart);

    jbStop = new JButton();
    jbStop.setText("Stop");
    jbStop.setSize(80, 25);
    jbStop.setLocation(500, 95);
    this.add(jbStop);

    jbPlay = new JButton();
    jbPlay.setText("Play");
    jbPlay.setSize(80, 25);
    jbPlay.setLocation(600, 95);
    this.add(jbPlay);
}

public class event implements ChangeListener {

    @Override
    public void stateChanged(ChangeEvent e) {
        try {
            if (MyFrame.shape1 == null) {
                jtfBoundaryLength.setText("shape not set");
            } else {
                 DecimalFormat g = new DecimalFormat("0.0");
                jtfBoundaryLength.setText("" + g.format(MyFrame.shape1.getBoundaryLength(jsShapes.getValue())));
                jtfArea.setText("" + g.format(MyFrame.shape1.getArea(jsShapes.getValue())));      
            }


            MyFrame.shape1.setSliderValue(jsShapes.getValue());
        } catch (Throwable ex) {}
        }

    }

    public int getSliderValue() {
        return this.jsShapes.getValue();
    }  

    public void actionPerformed(ActionEvent e) {
        System.out.println(MyFrame.shape1.getArrayList());
    }
}

这是我创建ArrayList

的Shapes页面的代码
public abstract class Shapes {

  ArrayList<String> arrayList = new ArrayList();

  protected double boundaryLength;
  protected double area;
  private int sliderValue;

  public int getSliderValue() {
    return sliderValue;
  }

  public void setSliderValue(int sliderValue) {
    this.sliderValue = sliderValue;
  }

  public double getBoundaryLength(double boundaryLength) 
  {
    return boundaryLength;
  }

  public double getArea(double area) 
  {
    return area;
  }

  public ArrayList<String> getArrayList() {
    return arrayList;
  }

  public void setArrayList(ArrayList arrayList) {
    this.arrayList = arrayList;
  }
}

这是我的MyFrame页面,它是创建菜单的地方。菜单中有三个选项(三角形,圆形和方形)。

public class MyFrame extends javax.swing.JFrame implements ActionListener, ChangeListener {


public static Shapes shape1;
private JMenuItem Square;
private JMenuItem Triangle;
private JMenuItem Circle;
private MyControlPanel controlPanelShapes;

private jPanelTop d1 = new jPanelTop();
private jPanelBottom d2 = new jPanelBottom();

public MyFrame() {
    initComponents();

    controlPanelShapes = new MyControlPanel();
    controlPanelShapes.setSize(1000, 1000);
    controlPanelShapes.setLocation(0, 20);
    add(controlPanelShapes);

    d1.setSize(550, 230);
    d1.setVisible(true);
    this.add(d1);

    JMenuBar jBarShape = new JMenuBar();
    JMenu Shape = new JMenu();
    Shape.setText("Shape");

    Square = new JMenuItem();
    Square.setText("Square");
    Shape.add(Square);
    Square.addActionListener(this);

    Triangle = new JMenuItem();
    Triangle.setText("Triangle");
    Shape.add(Triangle);
    Triangle.addActionListener(this);

    Circle = new JMenuItem();
    Circle.setText("Circle");
    Shape.add(Circle);
    Circle.addActionListener(this);

    jBarShape.add(Shape);
    setJMenuBar(jBarShape);

}


@Override
public void stateChanged(ChangeEvent e) {
    throw new UnsupportedOperationException("Not supported yet.");
}

@Override
public void actionPerformed(ActionEvent e) {

    if (e.getSource() == Circle) {

        shape1 = new Circle();
        d1.setCircle(true, false);
        shape1.setSliderValue(d2.getSliderValue());
        System.out.println(MyFrame.shape1.getArrayList());

    } else if (e.getSource() == Triangle) {

        shape1 = new Triangle();
        d1.setTriangle(true, false);
         shape1.setSliderValue(d2.getSliderValue());
         System.out.println(MyFrame.shape1.getArrayList());
    } else if (e.getSource() == Square) {

        shape1 = new Square();
        d1.setSquare(true, false);
        shape1.setSliderValue(d2.getSliderValue());
         System.out.println(MyFrame.shape1.getArrayList());
    }
}
}

基本上当我单击播放按钮时,无论何时单击菜单选项或移动滑块,都应该记录它,当我单击停止时它会停止录制。单击“播放”时,将其输出到屏幕。我相信这很简单,我只是不知道,我一直试图让它工作多年! 提前感谢您的帮助,非常感谢!

0 个答案:

没有答案