2012-01-03 65 views
3

好吧,我寫了這個代碼片作爲一個初學者在java學習視頻/學校,我有一些問題。從菜單項/菜單欄和聲音播放器Java退出按鈕

1 =>爲什麼文件>退出按鈕不工作,有一個小箭頭,就好像有一些孩子的?大退出按鈕使用相同的功能。 我從這裏獲得靈感:http://www.youtube.com/watch?src_vid=FB_wJpIdA8k&feature=iv&annotation_id=annotation_40248&v=dwLkDGm5EBc

2 =>我該如何讓這個按鈕更小?調整大小時它會更大。

3 =>有誰知道一個簡單的聲音播放器庫?所以當我按下該按鈕來播放聲音?我嘗試了一些網絡示例,如http://www.developer.com/java/other/article.php/2173111/Java-Sound-Playing-Back-Audio-Files-using-Java.htm,不知道如何使它變得簡單,並在SoundPlay(sound.au)等各處使用。

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

import javax.swing.JButton; 
import javax.swing.JFrame; 
import javax.swing.JMenu; 
import javax.swing.JMenuBar; 
import javax.swing.JMenuItem; 

public class form4 
{ 
    public static void main(String[] args) 
    { 
     // Frame 
     JFrame frame = new JFrame("Menu"); 
     frame.setSize(300,300); 
     frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 

     // Just create menubar 
     JMenuBar menubar = new JMenuBar(); 
     frame.setJMenuBar(menubar); 

     // Add an JMenu 
     JMenu file = new JMenu("File"); 
     menubar.add(file); 
     // Add an JMenuItem 
     JMenuItem exit = new JMenu("Exit"); 
     file.add(exit); 
     exit.addActionListener(new exitApp()); 

     // Add an JMenu 
     JMenu help = new JMenu("Help"); 
     menubar.add(help); 
     // Add an JMenuItem 
     JMenuItem about = new JMenuItem("About"); 
     help.add(about); 

     // Add an JButton 
     JButton exitButton= new JButton("Exit!"); 
     frame.add(exitButton); 
     exitButton.addActionListener(new exitApp()); 
     exitButton.setSize(40,40); 

     frame.setVisible(true); 
    } 

    // Exit app 
    static class exitApp implements ActionListener 
    { 
     public void actionPerformed(ActionEvent e) 
     { 
      System.exit(0); 
     } 
    } 
} 

謝謝!

+3

參見[使用菜單](HTTP:// docs.oracle.com/javase/tutorial/uiswing/components/menu.html)有關子菜單。 – trashgod 2012-01-03 03:03:27

+2

另請參閱[初始線程](http://download.oracle.com/javase/tutorial/uiswing/concurrency/initial.html)。 – trashgod 2012-01-03 03:04:18

回答

11

要退出菜單的工作,使用的JMenuItem:

JMenuItem exit = new JMenuItem("Exit"); 
    exit.addActionListener(new exitApp()); 
    file.add(exit); 

關於你提到的其他問題,如何使按鈕「小」,你要明白,你要添加這個JButton的一個Jframe的contentPane,並且contentPane默認使用BorderLayout。因此,以這種方式添加按鈕將使其完全填滿容器。爲防止這種情況發生,您需要使用其他佈局。請有關如何使用Swing佈局的詳細信息,閱讀起來:A Visual Guide to the Layout Managers

+0

工程,這是我的錯誤,我必須更仔細地看,因爲我是新來的Java。謝謝:) – Master345 2012-01-03 03:19:01

+0

無論如何,有沒有一個可視化的按鈕界面,就像在C#中的java?我目前使用Eclipse ... – Master345 2012-01-03 03:20:26

+0

@RowMinds:解釋「可視化按鈕界面」,因爲這對我來說沒有任何意義。如果你問是否有一個類似於MS Visual Studio的可視化GUI設計器,那麼是的,但是我強烈建議你不要使用它,直到你學習Swing的雛形。 – 2012-01-03 04:51:02

3

下面是我如何在我的應用程序中實現它,希望它有助於:

menuBar = new JMenuBar(); 
    mainWindow.setJMenuBar(menuBar); 

    mnFile = new JMenu("File"); 
    menuBar.add(mnFile); 

    mntmClose = new JMenuItem("Close"); 
    mntmClose.setMnemonic(KeyEvent.VK_Q); 
    mntmClose.setAccelerator(KeyStroke.getKeyStroke(
      KeyEvent.VK_Q, ActionEvent.CTRL_MASK)); 
    mntmClose.setToolTipText("Exit application"); 
    mntmClose.addActionListener(new ActionListener() { 
     public void actionPerformed(ActionEvent event) { 
      System.exit(0); 
     } 

    }); 
    mnFile.add(mntmClose); 
+0

很好的答案,你把這個放在哪裏? – Master345 2013-11-12 18:27:40