我已经计算了花括号,无法弄清楚为什么类体不完整。 每当我尝试修课时,它都会让整个班级变得混乱。 问题是代码中的最后一个类。最后一个花括号是给我带来麻烦的课程。我正在使用Eclipse来编写它。
以下是整个计划的代码:
import java.awt.BorderLayout;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
public class Stocks {
public static void main(String [] args) {
JFrame frame = new JFrame ("Java Stocks");
frame.setSize(700,700);
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel panel = new JPanel (new GridBagLayout());
frame.add(panel);
frame.getContentPane().add(panel, BorderLayout.WEST);
GridBagConstraints c = new GridBagConstraints ();
JButton button1 = new JButton("Profile");
c.gridx = 0;
c.gridy = 0;
c.insets = new Insets(40, 40, 40, 40);
panel.add(button1, c);
button1.addActionListener(new Action());
}
static class Action implements ActionListener {
public void actionPerformed (ActionEvent e) {
JFrame frame2 = new JFrame("Your Stocks");
frame2.setVisible(true);
frame2.setSize(600,600);
JLabel label = new JLabel("Your Personal Stocks");
JPanel panel = new JPanel();
frame2.add(panel);
panel.add(label);
}
public static void main(String [] args) {
GridBagConstraints c = new GridBagConstraints ();
JButton button2 = new JButton("Market");
c.gridx = 0;
c.gridy = 1;
c.insets = new Insets(40, 40, 40, 40);
button2.addActionListener(new Action());
}
static class Action2 implements ActionListener {
public void actionPerformed (ActionEvent e) {
JFrame frame2 = new JFrame("Your Stocks");
frame2.setVisible(true);
frame2.setSize(600,600);
JLabel label = new JLabel("Your Personal Stocks");
JPanel panel = new JPanel();
frame2.add(panel);
panel.add(label);
}
public static void main(String [] args) {
GridBagConstraints c = new GridBagConstraints ();
JButton button3 = new JButton("Users");
c.gridx = 0;
c.gridy = 2;
c.insets = new Insets(40, 40, 40, 40);
button3.addActionListener(new Action());
}
static class Action3 implements ActionListener {
public void actionPerformed (ActionEvent e) {
JFrame frame2 = new JFrame("Your Stocks");
frame2.setVisible(true);
frame2.setSize(600,600);
JLabel label = new JLabel("Your Personal Stocks");
JPanel panel = new JPanel();
frame2.add(panel);
panel.add(label);
}
public static void main(String [] args) {
GridBagConstraints c = new GridBagConstraints ();
JButton button4 = new JButton("Notes");
c.gridx = 0;
c.gridy = 3;
c.insets = new Insets(40, 40, 40, 40);
button4.addActionListener(new Action());
}
static class Action4 implements ActionListener {
public void actionPerformed (ActionEvent e) {
JFrame frame2 = new JFrame("Your Stocks");
frame2.setVisible(true);
frame2.setSize(600,600);
JLabel label = new JLabel("Your Personal Stocks");
JPanel panel = new JPanel();
frame2.add(panel);
panel.add(label);
}
public static void main(String [] args) {
GridBagConstraints c = new GridBagConstraints ();
JButton button5 = new JButton("Information");
c.gridx = 0;
c.gridy = 4;
c.insets = new Insets(40, 40, 40, 40);
button5.addActionListener(new Action());
}
static class Action5 implements ActionListener {
public void actionPerformed (ActionEvent e) {
JFrame frame2 = new JFrame("Your Stocks");
frame2.setVisible(true);
frame2.setSize(600,600);
JLabel label = new JLabel("Your Personal Stocks");
JPanel panel = new JPanel();
frame2.add(panel);
panel.add(label);
}
答案 0 :(得分:2)
由于缩进令人困惑,您将嵌套类嵌套在彼此之内并不明显。 Action5
嵌套在Action4
中,嵌套在Action3
中,嵌套在Action2
中,嵌套在Action
中,嵌套在Stocks
中1}}。
要么在文件的最末端放置5个大括号,要关闭所有类,要么更好,将它们全部嵌套在Stocks
中,而不是彼此嵌入。
答案 1 :(得分:1)
尝试使用ctrl + shift + f格式化代码
答案 2 :(得分:0)
当你清理它时,你的代码应该是这样的:
import java.awt.BorderLayout;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
class Action implements ActionListener {
public void actionPerformed (ActionEvent e) {
JFrame frame2 = new JFrame("Your Stocks");
frame2.setVisible(true);
frame2.setSize(600,600);
JLabel label = new JLabel("Your Personal Stocks");
JPanel panel = new JPanel();
frame2.add(panel);
panel.add(label);
}
}
public class Stocks {
public static void main(String [] args) {
JFrame frame = new JFrame ("Java Stocks");
frame.setSize(700,700);
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel panel = new JPanel (new GridBagLayout());
frame.add(panel);
frame.getContentPane().add(panel, BorderLayout.WEST);
GridBagConstraints c = new GridBagConstraints ();
JButton button1 = new JButton("Profile");
c.gridx = 0;
c.gridy = 0;
c.insets = new Insets(40, 40, 40, 40);
panel.add(button1, c);
button1.addActionListener(new Action());
}
}
我的建议#1:首先学会用键盘而不是鼠标进行编码。
我的建议#2:如果您不确定static
修饰符的含义以及含义是什么,请不要使用它。