2013-10-10 43 views
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); 
      } 
+0

当你的类代码太长以至于不能重新构建时,需要重新构造它! – RamonBoza

+0

你缺少6 x'}' – Neet

+1

你需要静止的静止。 – maksimov

回答

2

由于混乱的缩进,这不是很明显,你是里面嵌套彼此的嵌套类。 Action5嵌套在Action4中,嵌套在Action3中,嵌套在Action2中,嵌套在Action中,嵌套在Stocks中。

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

+0

我认为它是由一些GUI生成器生成的,'Action1'到'Action5'是拷贝,甚至在任何地方都没有使用。 – maksimov

+0

我将如何将它们完全嵌套到股票中?我需要视觉方面。我没有使用GUI生成器,我只使用代码。它被复制和粘贴,所以当用户点击A按钮时,它会打开一个新的GUI。 – Aaron

1

尝试使用CRLT + Shift + F键格式化你的代码

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修饰符的作用和含义,请不要使用它。