好吧,我在视频/学校的java学习中作为初学者编写了这段代码,我得到了一些问题.
2 =>如何使该按钮变小?当我调整它时,它会更大.
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);
}
}
}
谢谢!