2013-10-10 42 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修飾符的作用和含義,請不要使用它。