-2

我是Java编程新手。我需要编写一个小程序来显示字符串的反转。我应该显示每个字符都在飞行并以相反的顺序排列。有人可以指导我如何做到这一点。提前致谢

4

4 回答 4

3

把你的问题(作业?)分解成几个步骤:

  • 你知道如何制作小程序吗?如果没有,请搜索“java小程序教程”
  • 你知道如何显示文本吗?如果没有,请查看 JLabel 文档
  • 你知道如何随着时间的推移改变事物吗?如果不是,请查看 javax.swing.Timer 文档(注意有多个名为 Timer 的类,获取 Swing 一个)
  • 想要获得精美的动画吗?如果是,请搜索“java 自定义绘画教程”(或者查看 JavaFX,这应该会使动画更容易)
于 2012-06-13T10:03:16.803 回答
1
     import javax.swing.*;
     import java.awt.BorderLayout;
     import java.awt.event.*;

     public class ShowReverse extends JApplet implements ActionListener {
     private JLabel reverseLabel;
     private JTextField inputField;
      private JButton clickButton;

      // Kicks off applet
      public void init() {
      reverseLabel = new JLabel();
     inputField = new JTextField();
      clickButton = new JButton("Reverse");

     // Add event listener to button
      clickButton.addActionListener(this);

     // Add the input field at the top, label in the middle and button at bottom
      add(inputField, BorderLayout.NORTH);
      add(reverseLabel, BorderLayout.CENTER);
       add(clickButton, BorderLayout.SOUTH);
        }


       // When button is clicked, it performs this action.
       // Set the label to the result of our reverse function.
        public void actionPerformed(ActionEvent e) {
       if (e.getSource() == clickButton) {
         reverseLabel.setText( reverse(inputField.getText() ));

           }
       }


      // reverses a string by simply looping through the characters backwards
        // and builds the string.
          private String reverse(String text) {
             if (text.length() > 1) {
               String reversed = "";

                for (int i = text.length() - 1; i >= 0; i--) {
               reversed += Character.toString(text.charAt(i));
               }

            return reversed;

           }
           else { return text; }

         }
         }
于 2012-06-13T10:05:22.670 回答
0

I think if you need flying characters, you should use Graphics instead of just text. You represent each letter as image or set of figures and then swap due to reversing algorithm.

But, first of all you need to understand how to draw in applet. Hope this article helps http://www.wikihow.com/Use-Graphics-in-a-Java-Applet

于 2012-06-13T10:05:21.607 回答
0

String 类不能有 Reverse 方法,因为它是不可变的。所以我们可以使用 StringBuffer 来修改给定的 String。在这里我们可以将 t1(TextField) 的给定输入反转为 t2(TextField)。但是 textfield 方法 setText(String) 接受一个必须在 String 中的参数,然后我们可以使用 toString() 方法将 StringBuffer 值转换为 String 值。

显示这个例子:

    import java.awt.*;

    import java.applet.Applet;

    import java.awt.event.*;

    public class rev extends Applet implements ActionListener

    {

    Button b;
    TextField t,t1;

    public void init()
    {
        b=new Button("Reverse");

        Label l=new Label("enter a your Name");
        add(l);
        t=new TextField(" ",15);
        add(t);
        Label l1=new Label("The reverce ");
        add(l1);
        t1=new TextField(" ",15);
        add(t1);
        b.addActionListener(this);
        add(b);
        setLayout(new FlowLayout(FlowLayout.LEADING));
    }
    public void actionPerformed(ActionEvent e)
    {
        String sa=t.getText();
        StringBuffer a = new StringBuffer(sa);
        t1.setText(t.getText()+"  "+a.reverse().toString());

    }
   }
于 2017-03-21T18:32:55.117 回答