【发布时间】:2012-01-03 02:43:54
【问题描述】:
好的,我作为一个从视频/学校学习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);
}
}
}
谢谢!
【问题讨论】:
-
有关子菜单的信息请参见How to Use Menus。
标签: java swing jframe jbutton actionlistener