2

我的代码:

import java.awt.Dimension;
import java.awt.Toolkit;
import javax.swing.*;

public class TAFrame {

    private JFrame mainFrame;
    private JPanel mainPanel;
    private JButton button;
    private JTextArea textArea; //eclipse say Syntax error } expected


    mainFrame = new JFrame("mainFrame");
    mainPanel = new JPanel();
    button = new JButton("click me");
    area = new JTextArea(10, 15); 


}

找不到解决方案,但我认为这很容易尴尬:/

4

4 回答 4

5

我相信您想将一些代码放在构造函数中,如下所示:

public class TAFrame {

    private JFrame mainFrame;
    private JPanel mainPanel;
    private JButton button;
    private JTextArea textArea;

    public TAFrame() {
       mainFrame = new JFrame("mainFrame");
       mainPanel = new JPanel();
       button = new JButton("click me");
       area = new JTextArea(10, 15); 
    }
}

问题是您试图在任何方法之外执行任意代码。声明字段后,您需要通过方法访问它。只能在同一行初始化它,因此您可以执行以下操作:

public class TAFrame {

    private JFrame mainFrame = new JFrame("mainFrame");
    private JPanel mainPanel = new JPanel();
    private JButton button = new JButton("click me");
    private JTextArea textArea = new JTextArea(10, 15); 
}

在这种情况下,我推荐使用构造函数方法,但无论哪种方式,您都最需要构造函数,因为您可能希望向按钮添加一个动作监听器(例如)。

于 2013-09-16T20:01:30.113 回答
3

这里的大多数答案都正确指出您可以使用初始值进行初始化,并且可以使用构造函数。然而,Java 教程Initializing Fields实际上描述了两种初始化字段的非构造方法:(i)初始值;(ii) 初始化块。

以下代码演示了所有三种方法(并显示了实例和静态初始化块):

public class InitializationExample {
    private int field1 = 1; // in-line initializer
    private int field2a;
    private static int field2b;
    private int field3;

    { 
        field2a = 3; // instance initializer
    }

    static {
        field2b = 3; // static initializer
    }

    public InitializationExample( final int field3 ) {
        this.field3 = field3;
    }
}

使用初始化块,您可以对代码进行非常小的更改并使其编译:

import java.awt.Dimension;
import java.awt.Toolkit;
import javax.swing.*;

public class TAFrame {
    private JFrame mainFrame;
    private JPanel mainPanel;
    private JButton button;
    private JTextArea textArea;

    {    
      mainFrame = new JFrame("mainFrame");
      mainPanel = new JPanel();
      button = new JButton("click me");
      area = new JTextArea(10, 15); 
    }
}

尽管这是可能的,但它并不是非常普遍,所以除非你有特别好的理由使用它,或者它在你正在处理的代码库中已经很常见,否则初始值或构造函数可能更具可读性和可维护性选项。同样重要的是要注意,根据12.5。根据 Java 语言规范创建新类实例,在构造函数代码运行之前处理实例初始化代码(和初始值) 。

与基于构造函数的方法相比,初始化块方法有一个可能的好处。如果您确实将其设为构造函数,则按照

    public TAFrame() {    
      mainFrame = new JFrame("mainFrame");
      mainPanel = new JPanel();
      button = new JButton("click me");
      area = new JTextArea(10, 15); 
    }

然后稍后引入另一个带有一些参数的构造函数,您要么需要从该构造函数显式调用零参数构造函数(构造函数链接),要么在该构造函数中包含初始化赋值。使用初始化块,您也不需要这样做。

当您创建匿名子类的实例时,初始化块也很方便,因为您可以将初始化代码直观地保留在类“内部”。您可以阅读双括号初始化以获取更多详细信息,但这里有一个简单的示例:

import java.util.HashMap;
import java.util.Map;

public class MapInitializationExample {
    public static void main(String[] args) {
        // initialization code for this Map is visually "inside" the map
        final Map<Integer,String> numberNames = new HashMap<Integer,String>() {{
            put(1,"one");
            put(2,"two");
            put(3,"three");
        }};
    }
}
于 2013-09-16T20:38:35.077 回答
1

Because you are putting code outside of a method that does not belong.

This block in particular:

mainFrame = new JFrame("mainFrame");
mainPanel = new JPanel();
button = new JButton("click me");
area = new JTextArea(10, 15); 

The error is a bit deceiving because it looks like private JTextArea textArea; needs a closing brace. But really, the problem is that the next line does not belong there. The next line, mainFrame = new JFrame("mainFrame"); suggests a method has started and the previous block never closed, thus the reference to a missing }.

You have two options:

  1. Instantiate the objects in-line with their declarations
  2. Instantiate the objects in a constructor

To Instantiate In-line

import java.awt.Dimension;
import java.awt.Toolkit;
import javax.swing.*;

public class TAFrame {
    private JFrame mainFrame = new JFrame("mainFrame");
    private JPanel mainPanel = new JPanel();
    private JButton button = new JButton("click me");
    private JTextArea textArea = new JTextArea(10, 15);
}

To Instantiate with a Constructor

import java.awt.Dimension;
import java.awt.Toolkit;
import javax.swing.*;

public class TAFrame {
    private JFrame mainFrame;
    private JPanel mainPanel;
    private JButton button;
    private JTextArea textArea;

    public TAFrame() {
        mainFrame = new JFrame("mainFrame");
        mainPanel = new JPanel();
        button = new JButton("click me");
        area = new JTextArea(10, 15); 
    }
}

I personally prefer the inline approach as it is far less verbose.

于 2013-09-16T20:06:58.780 回答
0

你应该做这样的事情。

 public class TAFrame {

  private JFrame mainFrame;
  private JPanel mainPanel;
  private JButton button;
  private JTextArea textArea;

  public void initComponents() {
    mainFrame = new JFrame("mainFrame");
    mainPanel = new JPanel();
    button = new JButton("click me");
    area = new JTextArea(10, 15); 
 }
}

或者您可以在那里创建和实例化控件本身。

 private JFrame mainFrame = new JFrame("mainFrame");
 private JPanel mainPanel = new JPanel();
 private JButton button = new JButton("click me");
 private JTextArea textArea = new JTextArea(10, 15);
于 2013-09-16T20:28:33.427 回答