2013-04-25 119 views
1

我是Java新手,现在正在学习初学者课程。非常好,我正在尝试各种各样的东西,但现在我卡住了。 这段代码不起作用。它应该以相反的顺序(通过文字)输出输入。静态和非静态故障

代码翻转它工作在一段代码,我写了没有GUI,现在我想要得到它的GUI工作,有固定按钮,标签等 对于我已经复制的例子从互联网,并试图改变它的方式,它会工作。但它似乎没有找到我在actionPerformed中使用的变量,并在AddComponentsToPane中设置。它必须做一些静态和非静态的,我似乎无法得到真正清除

任何帮助将不胜感激。

继承人的代码。

package flipit; 


import java.applet.Applet; 
import java.awt.*; 
import java.awt.event.*; 
import java.util.List; 
import java.util.*; //ArrayList; 
import javax.swing.*; 

public class FlipIt extends JFrame implements ActionListener { 

public static void addComponentsToPane(Container pane) { 

    pane.setLayout(null); 

    JLabel  greetingLabel = new JLabel("Enter your array "); 
    JTextField inField   = new JTextField(); 
    JTextField commentaryField = new JTextField(); 
    JTextField strLen   = new JTextField(); 
    JButton button   = new JButton("FlipIt"); 

    pane.add(greetingLabel); 
    pane.add(inField); 
    pane.add(commentaryField); 
    pane.add(button);   

    Insets insets = pane.getInsets(); 
    Dimension size = button.getPreferredSize(); 

    greetingLabel.setBounds ( 5 + insets.left, 35 + insets.top,size.width + 40, size.height); 
    inField.setBounds  (120 + insets.left, 33 + insets.top,size.width + 200, size.height); 
    //size = commentaryField.getPreferredSize(); 
    commentaryField.setBounds(120 + insets.left, 80 + insets.top,size.width + 200, size.height); 
    size = button.getPreferredSize(); 
    button.setBounds   ( 5 + insets.left, 80 + insets.top,size.width + 40, size.height); 

} 

private static void createAndShowGUI() { 
    //Create and set up the window. 

    JFrame frame = new JFrame("Reverse the input string."); 
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 

    //Set up the content pane. 
    addComponentsToPane(frame.getContentPane()); 

    //Size and display the window. 
    Insets insets = frame.getInsets(); 
    frame.setSize(500 + insets.left + insets.right, 
        425 + insets.top + insets.bottom); 
    frame.setVisible(true); 
} 

    public static void main(String[] args) { 
    Scanner input = new Scanner(System.in); 

    //Schedule a job for the event-dispatching thread: 
    //creating and showing this application's GUI. 
    javax.swing.SwingUtilities.invokeLater(new Runnable() { 
     public void run() { 
      createAndShowGUI(); 
     } 
    }); 
} 

public void actionPerformed(ActionEvent event) { 

    String InputStr = inField.getText(); 
    int length = InputStr.length(); 

    if (InputStr.compareTo("") == 0 || InputStr == null) { 
     commentaryField.setText("Please enter some data, this array is empty"); 
    } 
    else { 

     // Convert string variable to an ArrayList. 
     List<String> list = new ArrayList<String>(Arrays.asList(InputStr.split(" ")));    
     // Converting ArrayList to a String Array 
     String [] InputList = list.toArray(new String[list.size()]); 

     int i = 0 ; 
     String ReverseOrder = "" ; 

     // starting for loop to print the array in reversed order. 
     for (i=InputList.length-1;i>=0; i--) 
      {ReverseOrder = ReverseOrder + " " + InputList[i] ; 
      } 
     // print result. 
      commentaryField.setText(ReverseOrder); 
      strLen.setText("Lengte string : "+ length); 
    } 
    } 
} 

谢谢, Rob。

+3

你得到的错误是什么? – Abubakkar 2013-04-25 09:26:28

+0

不是一个真正的错误。我在NedBeans中进行编辑,并且在actionPerformed中使用的变量comnoteField,strLen,inField前面给出了这些红色气球。这些说“找不到符号”。 – user2318914 2013-04-25 09:29:11

+0

这意味着错误,请发布您看到的整个错误,如“找不到符号.....” – Abubakkar 2013-04-25 09:30:21

回答

3

主要问题是作用域问题。由于您在addComponentsToPane方法中声明了小部件,因此它们在方法外部不可见。

尝试移动你的widget声明的addComponentstoPane方法之外:

JLabel  greetingLabel = new JLabel("Enter your array "); 
JTextField inField   = new JTextField(); 
JTextField commentaryField = new JTextField(); 
JTextField strLen   = new JTextField(); 
JButton button   = new JButton("FlipIt"); 

public static void addComponentsToPane(Container pane) { 

    pane.setLayout(null); 

    pane.add(greetingLabel); 
    pane.add(inField); 
    pane.add(commentaryField); 
    pane.add(button);  
    // etc 
} 

正如您所指出的,虽然(对不起,是我不好!)你的静态方法将不再有机会获得小部件(自他们现在是类别实例的一部分)。

思考静态与非静态的简单方法是,如果声明某些内容为静态,则不需要需要一个类实例才能访问它。因此,在你的代码,为什么你可以这样做:有效

public void run() { 
    createAndShowGUI(); 
} 

,这将是一样的这样做:你没有创建FlipIt类的实例

public void run() { 
    FlipIt.createAndShowGUI(); 
} 

注;你不需要,因为createAndShowGUI方法是static。但是,如果它不静态,那么你就必须创建一个新的类的实例,如下所示:

public void createAndShowGUI() { 
    // do your thing - note NO LONGER STATIC 
} 

public void run() { 
    // Create instance 
    FlipIt flipper = new FlipIt(); 

    // Invoke method against class instance 
    flipper.createAndShowGUI(); 
} 

那么 - 为了得到你的代码的工作,最好的解决办法是让一切非静态(当然除了main方法,其中必须是是静态的)。

下面是整个代码示例全部放在一起 - 请注意,您可能需要使createAndShowGUI方法public - 但我不这么认为。我用java编码已经有一段时间了,所以我不能确定。

package flipit; 


import java.applet.Applet; 
import java.awt.*; 
import java.awt.event.*; 
import java.util.List; 
import java.util.*; //ArrayList; 
import javax.swing.*; 

public class FlipIt extends JFrame implements ActionListener { 
    JLabel  greetingLabel = new JLabel("Enter your array "); 
    JTextField inField   = new JTextField(); 
    JTextField commentaryField = new JTextField(); 
    JTextField strLen   = new JTextField(); 
    JButton button   = new JButton("FlipIt"); 

    public void addComponentsToPane(Container pane) { 
    pane.setLayout(null); 

    pane.add(greetingLabel); 
    pane.add(inField); 
    pane.add(commentaryField); 
    pane.add(button);   

    Insets insets = pane.getInsets(); 
    Dimension size = button.getPreferredSize(); 

    greetingLabel.setBounds ( 5 + insets.left, 35 + insets.top,size.width + 40, size.height); 
    inField.setBounds  (120 + insets.left, 33 + insets.top,size.width + 200, size.height); 
    //size = commentaryField.getPreferredSize(); 
    commentaryField.setBounds(120 + insets.left, 80 + insets.top,size.width + 200, size.height); 
    size = button.getPreferredSize(); 
    button.setBounds   ( 5 + insets.left, 80 + insets.top,size.width + 40, size.height); 
    } 

    private void createAndShowGUI() { 
    //Create and set up the window. 
    JFrame frame = new JFrame("Reverse the input string."); 
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 

    //Set up the content pane. 
    addComponentsToPane(frame.getContentPane()); 

    //Size and display the window. 
    Insets insets = frame.getInsets(); 
    frame.setSize(500 + insets.left + insets.right, 
        425 + insets.top + insets.bottom); 
    frame.setVisible(true); 
    } 

    public void actionPerformed(ActionEvent event) { 
    String InputStr = inField.getText(); 
    int length = InputStr.length(); 

    if (InputStr.compareTo("") == 0 || InputStr == null) { 
     commentaryField.setText("Please enter some data, this array is empty"); 

    } else { 

     // Convert string variable to an ArrayList. 
     List<String> list = new ArrayList<String>(Arrays.asList(InputStr.split(" ")));    
     // Converting ArrayList to a String Array 
     String [] InputList = list.toArray(new String[list.size()]); 

     int i = 0 ; 
     String ReverseOrder = "" ; 

     // starting for loop to print the array in reversed order. 
     for (i=InputList.length-1;i>=0; i--) { 
     ReverseOrder = ReverseOrder + " " + InputList[i] ; 
     } 

     // print result. 
     commentaryField.setText(ReverseOrder); 
     strLen.setText("Lengte string : "+ length); 
    } 
    } 

    public static void main(String[] args) { 
     Scanner input = new Scanner(System.in); 

     //Schedule a job for the event-dispatching thread: 
     //creating and showing this application's GUI. 
     javax.swing.SwingUtilities.invokeLater(new Runnable() { 
     public void run() { 
      FlipIt flipper = new FlipIt(); 
      flipper.createAndShowGUI(); 
     } 
     }); 
    } 
    } 
} 

希望有所帮助!

+0

感谢您的回复。试图但当然addComponentsPane不喜欢那样。试图从它删除静态,但然后一切都变成了红色 – user2318914 2013-04-25 12:30:00

+0

对不起 - 关于 - 我写了我的答案迟了。我已经修正它指出为什么它是错的;希望这会让你走。欢迎来到java :-) – gerrod 2013-04-25 21:01:03

+0

嗨Gerrod,非常感谢您的回答,摆脱了NedBeans中的所有错误消息。它运行,但没有做它应该尽的,但至少(这是我理解java的主要问题)我知道更多这种静态的东西。 – user2318914 2013-04-26 14:58:30