0

我已经数过了花括号,无法弄清楚为什么类体不完整。每次我试图修复班级时,都会把整个班级搞得一团糟。问题是代码中的最后一个类。最后一个花括号是给我上课带来麻烦的花括号。我正在使用 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);
            }
4

3 回答 3

2

由于令人困惑的缩进,您将嵌套类相互嵌套并不明显。 Action5嵌套在Action4,嵌套在Action3,嵌套在Action2,嵌套在Action,嵌套在Stocks

要么在文件的最后放置 5 个大括号,以关闭所有类,或者更好的是,将它们全部直接嵌套在 中Stocks,而不是相互嵌套。

于 2013-10-10T15:56:06.113 回答
1

尝试使用 crlt+shift + f 格式化您的代码

于 2013-10-10T15:56:18.653 回答
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修饰符的用途和含义,请不要使用它。

于 2013-10-10T16:14:00.517 回答