我是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。
你得到的錯誤是什麼? – Abubakkar 2013-04-25 09:26:28
不是一個真正的錯誤。我在NedBeans中進行編輯,並且在actionPerformed中使用的變量comnoteField,strLen,inField前面給出了這些紅色氣球。這些說「找不到符號」。 – user2318914 2013-04-25 09:29:11
這意味着錯誤,請發佈您看到的整個錯誤,如「找不到符號.....」 – Abubakkar 2013-04-25 09:30:21