2014-12-22 42 views
0

我正嘗試使用主框架上的按鈕打開菜單框架。我添加了一個事件的按鈕,我試圖調用其它類,但它一直給我的。「::預計令牌之後」用Java中的按鈕打開不同的框架

這是我的主框架

import java.awt.BorderLayout; 
import java.awt.EventQueue; 

import javax.swing.JFrame; 
import javax.swing.JPanel; 
import javax.swing.border.EmptyBorder; 
import javax.swing.JButton; 

import java.awt.event.ActionListener; 
import java.awt.event.ActionEvent; 


public class Main extends JFrame { 

public static JPanel mainPane; 
public final JButton menuButton = new JButton("New button"); 

/** 
* Launch the application. 
*/ 
public static void main(String[] args) { 
    EventQueue.invokeLater(new Runnable() { 
     public void run() { 
      try { 
       Main frame = new Main(); 
       frame.setVisible(true); 
      } catch (Exception e) { 
       e.printStackTrace(); 
      } 
     } 
    }); 
} 

/** 
* Create the frame. 
*/ 
public Main() { 
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
    setBounds(100, 100, 450, 300); 
    mainPane = new JPanel(); 
    mainPane.setBorder(new EmptyBorder(5, 5, 5, 5)); 
    setContentPane(mainPane); 
    mainPane.setLayout(null); 
    menuButton.addActionListener(new ActionListener() { 
     public void actionPerformed(ActionEvent arg0) { 
      Menu.main(String[] args); 
     } 
    }); 
    menuButton.setBounds(76, 89, 104, 32); 
    mainPane.add(menuButton); 
} 
} 

一個錯誤,這是我的菜單框

import java.awt.BorderLayout; 
import java.awt.EventQueue; 

import javax.swing.JFrame; 
import javax.swing.JPanel; 
import javax.swing.border.EmptyBorder; 
import javax.swing.JLabel; 


public class Menu extends JFrame { 

public static JPanel menuPane; 

/** 
* Launch the application. 
*/ 
public static void main(String[] args) { 
    EventQueue.invokeLater(new Runnable() { 
     public void run() { 
      try { 
       Menu frame = new Menu(); 
       frame.setVisible(true); 
      } catch (Exception e) { 
       e.printStackTrace(); 
      } 
     } 
    }); 
} 

/** 
* Create the frame. 
*/ 
public Menu() { 
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
    setBounds(100, 100, 450, 300); 
    menuPane = new JPanel(); 
    menuPane.setBorder(new EmptyBorder(5, 5, 5, 5)); 
    setContentPane(menuPane); 
    menuPane.setLayout(null); 

    JLabel menuTitle = new JLabel("Menu"); 
    menuTitle.setBounds(194, 11, 46, 14); 
    menuPane.add(menuTitle); 

} 
} 
+1

請張貼完整的錯誤輸出 – Kapparino

+0

哪裏錯誤? – FlyingPiMonster

+1

此問題至少討論過0次。 – nbro

回答

1

將您的操作事件更改爲this.no不需要調用main方法,而是創建一個Menu類的新實例。

menuButton.addActionListener(new ActionListener() { 
     public void actionPerformed(ActionEvent arg0) { 
      Menu menu = new Menu(); 
      menu.setVisible(true); 
     } 
    }); 

如果你relly要調用的主要方法,然後使用

menuButton.addActionListener(new ActionListener() { 
     public void actionPerformed(ActionEvent arg0) { 
      Menu.main(new String[0]); 
     } 
    }); 

的錯誤是在這裏

Menu.main(String[] args);//error 

這不是參數傳遞給一個methods.this的正確方法是參數列表的聲明。

您可以通過它改變向糾錯,

String args[] = null; 
Menu.main(args); //correct