gpt4 book ai didi

java - 静态和非静态问题

转载 作者:行者123 更新时间:2023-11-29 05:44:48 27 4
gpt4 key购买 nike

我是 Java 的新手,现在正在学习初学者类(class)。非常好,我正在尝试各种方法,但现在我被卡住了。这段代码不起作用。它应该以相反的顺序(按单词)输出输入。

翻转它的代码在我编写的没有 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);
}
}
}

谢谢,罗布。

最佳答案

主要问题是范围界定问题。由于您是在addComponentsToPane 方法内部声明小部件,因此从方法外部看不到它们。

尝试将小部件声明移到 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();
}

实际上,这与这样做是一样的:

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

请注意,您还没有创建 FlipIt 类的实例;您不需要,因为 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();
}
});
}
}
}

希望对您有所帮助!

关于java - 静态和非静态问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16211034/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com