我遇到错误“语法错误,插入”}“以完成ClassBody” 我不知道该怎么办 这是我的代码:
import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JTextField;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import javax.swing.JButton;
public class Password {
private JFrame frame;
private JTextField PassField;
private JTextField txtSecretPasswords;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
Password window = new Password();
window.frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the application.
*/
public Password() {
initialize();
}/* Here*/
/**
* Initialize the contents of the frame.
*/
private void initialize() {
frame = new JFrame();
frame.setBounds(100, 100, 700, 650);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().setLayout(null);
PassField = new JTextField();
PassField.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
}
});
PassField.setBounds(0, 42, 150, 32);
frame.getContentPane().add(PassField);
PassField.setColumns(10);
txtSecretPasswords = new JTextField();
txtSecretPasswords.setEditable(false);
txtSecretPasswords.setText("Secret passwords");
txtSecretPasswords.setBounds(0, 11, 131, 20);
frame.getContentPane().add(txtSecretPasswords);
txtSecretPasswords.setColumns(10);
JButton btnEnter = new JButton("Enter");
btnEnter.addActionListener(new ActionListener() {
private JTextField txtMessage;
private JTextField txtMessage2;
private JTextField txtMessage3;
private JTextField txtPass2;
private JButton btnEnter2;
public void actionPerformed(ActionEvent e) {
int pass;
pass = Integer.parseInt(PassField.getText());
if (pass == 3333) {
txtMessage = new JTextField();
txtMessage.setEditable(false);
txtMessage.setText("Postal Code: 3333");
txtMessage.setBounds(0, 100, 130, 20);
frame.getContentPane().add(txtMessage);
txtMessage.setColumns(10);
txtMessage2 = new JTextField();
txtMessage2.setEditable(false);
txtMessage2 .setText("Email password: 3333");
txtMessage2.setBounds(0, 125, 150, 20);
frame.getContentPane().add(txtMessage2);
txtMessage2.setColumns(10);
txtMessage3 = new JTextField();
txtMessage3.setEditable(false);
txtMessage3.setText("Steam password: 3333");
txtMessage3.setBounds(0, 170, 200, 20);
frame.getContentPane().add(txtMessage3);
txtMessage3.setColumns(10);
btnEnter2 = new JButton();
btnEnter2.setText("Enter");
btnEnter2.setBounds(175, 250, 100, 15);
frame.getContentPane().add(btnEnter2);
btnEnter2.addActionListener(new ActionListener() {
public void actionPreformed(ActionEvent e) {
int pass2;
pass2 = Integer.parseInt(txtPass2.getText());
if (pass2 == 030303) {
txtMessage3 = new JTextField();
txtMessage3.setEditable(false);
txtMessage3.setText("Steam password: 3333");
txtMessage3.setBounds(0, 170, 200, 20);
frame.getContentPane().add(txtMessage3);
txtMessage3.setColumns(10);
}
else {
JOptionPane.showMessageDialog(null, "Wrong");
}
}/* And here*/);
txtPass2 = new JTextField();
txtPass2.setEditable(true);
txtPass2.setBounds(0, 250, 150, 20);
frame.getContentPane().add(txtPass2);
txtPass2.setColumns(10);
}
else {
JOptionPane.showMessageDialog(null, "Wrong");
}
}
});
btnEnter.setBounds(160, 47, 99, 23);
frame.getContentPane().add(btnEnter);
}
我知道它很长。我试图查看代码,但我不熟悉Java或编码。 我不知道为什么那里的错误或者怎么做如果你可以帮助它会受到初学者的赞赏!
答案 0 :(得分:5)
我建议使用一个好的编辑器或IDE。任何体面的编辑都应该允许"去匹配的括号"。例如,如果您使用vi,请将光标放在大括号上并键入"%"去匹配的。您可以使用它来验证括号是否符合您的预期。
Eclipse或IntelliJ等IDE允许您重新格式化代码,这也可以帮助您找到缺少结束括号的位置。在使用Java编程时,它们还提供其他宝贵的功能,因此我强烈建议花时间安装和学习。
这就是说,你在&#34之前错过了一个近距离支撑;而且这里"评价。
答案 1 :(得分:0)
如果您使用文本编辑器中的自动缩进,您会注意到最后一个'}'仍然缩进一个标签。
这意味着{
比}
更多。
使用一个好的IDE(例如Eclipse),可以更容易地找到它应该添加的位置(一次到最后一次,在最后一次/* And here */
注释之前)。请注意,在这种情况下,Eclipse显示的第一个错误不是正确的位置。
import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JTextField;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import javax.swing.JButton;
public class Password
{
private JFrame frame;
private JTextField PassField;
private JTextField txtSecretPasswords;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
Password window = new Password();
window.frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the application.
*/
public Password() {
initialize();
}/* Here */
/**
* Initialize the contents of the frame.
*/
private void initialize() {
frame = new JFrame();
frame.setBounds(100, 100, 700, 650);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().setLayout(null);
PassField = new JTextField();
PassField.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
}
});
PassField.setBounds(0, 42, 150, 32);
frame.getContentPane().add(PassField);
PassField.setColumns(10);
txtSecretPasswords = new JTextField();
txtSecretPasswords.setEditable(false);
txtSecretPasswords.setText("Secret passwords");
txtSecretPasswords.setBounds(0, 11, 131, 20);
frame.getContentPane().add(txtSecretPasswords);
txtSecretPasswords.setColumns(10);
JButton btnEnter = new JButton("Enter");
btnEnter.addActionListener(new ActionListener() {
private JTextField txtMessage;
private JTextField txtMessage2;
private JTextField txtMessage3;
private JTextField txtPass2;
private JButton btnEnter2;
public void actionPerformed(ActionEvent e) {
int pass;
pass = Integer.parseInt(PassField.getText());
if (pass == 5441) {
txtMessage = new JTextField();
txtMessage.setEditable(false);
txtMessage.setText("Postal Code: 42658");
txtMessage.setBounds(0, 100, 130, 20);
frame.getContentPane().add(txtMessage);
txtMessage.setColumns(10);
txtMessage2 = new JTextField();
txtMessage2.setEditable(false);
txtMessage2.setText("Email password: Vauxhal1");
txtMessage2.setBounds(0, 125, 150, 20);
frame.getContentPane().add(txtMessage2);
txtMessage2.setColumns(10);
txtMessage3 = new JTextField();
txtMessage3.setEditable(false);
txtMessage3.setText("Steam password: Vauxhal12");
txtMessage3.setBounds(0, 170, 200, 20);
frame.getContentPane().add(txtMessage3);
txtMessage3.setColumns(10);
btnEnter2 = new JButton();
btnEnter2.setText("Enter");
btnEnter2.setBounds(175, 250, 100, 15);
frame.getContentPane().add(btnEnter2);
btnEnter2.addActionListener(new ActionListener() {
public void actionPreformed(ActionEvent e) {
int pass2;
pass2 = Integer.parseInt(txtPass2.getText());
if (pass2 == 030303) {
txtMessage3 = new JTextField();
txtMessage3.setEditable(false);
txtMessage3.setText("Steam password: Vauxhal12");
txtMessage3.setBounds(0, 170, 200, 20);
frame.getContentPane().add(txtMessage3);
txtMessage3.setColumns(10);
} else {
JOptionPane.showMessageDialog(null, "Wrong");
}
}
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
}
}/* And here */);
txtPass2 = new JTextField();
txtPass2.setEditable(true);
txtPass2.setBounds(0, 250, 150, 20);
frame.getContentPane().add(txtPass2);
txtPass2.setColumns(10);
} else {
JOptionPane.showMessageDialog(null, "Wrong");
}
}
});
btnEnter.setBounds(160, 47, 99, 23);
frame.getContentPane().add(btnEnter);
}
}
PS:如果你只想使用基本的文本编辑器,Java就是错误的语言。使用Java +完整IDE(Netbeans或Eclipse),或者使用pluma / notepad2 / vim /.../ / p>的Ruby / Python
答案 2 :(得分:0)
在第122行的开头和代码末尾插入}
以完成classBody
。
但你应该考虑Roberto Attia的建议。
答案 3 :(得分:0)
您应该使用IDE,以便能够缩进代码并查看任何缺少花括号。
答案 4 :(得分:0)
正如许多其他人在此提出的那样,问题在于支撑平衡,这意味着每个{
打开一个代码块,因此需要匹配}
来关闭该代码块。
IDE确实可以让您更轻松地进行匹配,许多自由文本编辑器都可以为您进行匹配。
对代码进行一些更改:
此行}/* And here*/);
删除/* And here*/
并将其替换为}
。
接下来,主要课程public class Password
需要在}
的最后一行关闭。
最后,由于btnEnter2
需要覆盖addActionListener
抽象方法,因此您在第public void actionPreformed(ActionEvent e) {
行中输入拼写错误,actionPreformed
应拼写为actionPerformed
成功覆盖addActionListener
匿名方法。
最终代码应该类似于:
import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JTextField;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import javax.swing.JButton;
public class Password {
private JFrame frame;
private JTextField PassField;
private JTextField txtSecretPasswords;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
Password window = new Password();
window.frame.setVisible(true);
}
catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the application.
*/
public Password() {
initialize();
}/* Here*/
/**
* Initialize the contents of the frame.
*/
private void initialize() {
frame = new JFrame();
frame.setBounds(100, 100, 700, 650);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().setLayout(null);
PassField = new JTextField();
PassField.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
}
});
PassField.setBounds(0, 42, 150, 32);
frame.getContentPane().add(PassField);
PassField.setColumns(10);
txtSecretPasswords = new JTextField();
txtSecretPasswords.setEditable(false);
txtSecretPasswords.setText("Secret passwords");
txtSecretPasswords.setBounds(0, 11, 131, 20);
frame.getContentPane().add(txtSecretPasswords);
txtSecretPasswords.setColumns(10);
JButton btnEnter = new JButton("Enter");
btnEnter.addActionListener(new ActionListener() {
private JTextField txtMessage;
private JTextField txtMessage2;
private JTextField txtMessage3;
private JTextField txtPass2;
private JButton btnEnter2;
public void actionPerformed(ActionEvent e) {
int pass;
pass = Integer.parseInt(PassField.getText());
if (pass == 5441) {
txtMessage = new JTextField();
txtMessage.setEditable(false);
txtMessage.setText("Postal Code: 42658");
txtMessage.setBounds(0, 100, 130, 20);
frame.getContentPane().add(txtMessage);
txtMessage.setColumns(10);
txtMessage2 = new JTextField();
txtMessage2.setEditable(false);
txtMessage2 .setText("Email password: Vauxhal1");
txtMessage2.setBounds(0, 125, 150, 20);
frame.getContentPane().add(txtMessage2);
txtMessage2.setColumns(10);
txtMessage3 = new JTextField();
txtMessage3.setEditable(false);
txtMessage3.setText("Steam password: Vauxhal12");
txtMessage3.setBounds(0, 170, 200, 20);
frame.getContentPane().add(txtMessage3);
txtMessage3.setColumns(10);
btnEnter2 = new JButton();
btnEnter2.setText("Enter");
btnEnter2.setBounds(175, 250, 100, 15);
frame.getContentPane().add(btnEnter2);
btnEnter2.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
int pass2;
pass2 = Integer.parseInt(txtPass2.getText());
if (pass2 == 030303) {
txtMessage3 = new JTextField();
txtMessage3.setEditable(false);
txtMessage3.setText("Steam password: Vauxhal12");
txtMessage3.setBounds(0, 170, 200, 20);
frame.getContentPane().add(txtMessage3);
txtMessage3.setColumns(10);
}
else {
JOptionPane.showMessageDialog(null, "Wrong");
}
}
});
txtPass2 = new JTextField();
txtPass2.setEditable(true);
txtPass2.setBounds(0, 250, 150, 20);
frame.getContentPane().add(txtPass2);
txtPass2.setColumns(10);
}
else {
JOptionPane.showMessageDialog(null, "Wrong");
}
}
});
btnEnter.setBounds(160, 47, 99, 23);
frame.getContentPane().add(btnEnter);
}
}
希望这有帮助。