Java,关于菜单栏的代码,不用监听
1个回答
展开全部
import java.awt.Dimension;
import java.awt.Toolkit;
import javax.swing.ButtonGroup;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JRadioButtonMenuItem;
/**
* @author xx
*
*/
public class MenuTest extends JFrame {
/**
* @param args
*/
public static void main(String[] args) {
JFrame f = new MenuTest("MenuTest");
f.setSize(new Dimension(300,230));
Toolkit tk = Toolkit.getDefaultToolkit();
Dimension d=tk.getScreenSize();
f.setLocation((d.width-f.getWidth())/2, (d.height-f.getHeight())/2);
f.setVisible(true);
}
public MenuTest(){}
public MenuTest(String title){
super(title);
}
private JMenuBar menubar = new JMenuBar();
private JMenu fileMenu = new JMenu("File");
private JMenuItem newMenuItem = new JMenuItem("new");
private JMenuItem openMenuItem = new JMenuItem("open");
private JMenuItem saveMenuItem = new JMenuItem("save");
private JMenuItem saveAsMenuItem = new JMenuItem("save as...");
private JMenuItem exitMenuItem = new JMenuItem("exit");
private JMenu configMenu = new JMenu("config");
private JMenuItem backgroudMusicMenuItem=new JMenuItem("back music");
private JMenu backColorMenu = new JMenu("back color");
private JRadioButtonMenuItem redItem = new JRadioButtonMenuItem("red");
private JRadioButtonMenuItem yellowItem = new JRadioButtonMenuItem("yellow");
private JRadioButtonMenuItem blueItem = new JRadioButtonMenuItem("blue");
{
this.setJMenuBar(menubar);
menubar.add(fileMenu);
menubar.add(configMenu);
fileMenu.add(newMenuItem);
fileMenu.add(openMenuItem);
fileMenu.add(saveMenuItem);
fileMenu.add(saveAsMenuItem);
fileMenu.addSeparator();
fileMenu.add(exitMenuItem);
configMenu.add(backgroudMusicMenuItem);
configMenu.add(backColorMenu);
backColorMenu.add(redItem);
backColorMenu.add(yellowItem);
backColorMenu.add(blueItem);
ButtonGroup colorGroup = new ButtonGroup();
colorGroup.add(redItem);
colorGroup.add(yellowItem);
colorGroup.add(blueItem);
}
}
import java.awt.Toolkit;
import javax.swing.ButtonGroup;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JRadioButtonMenuItem;
/**
* @author xx
*
*/
public class MenuTest extends JFrame {
/**
* @param args
*/
public static void main(String[] args) {
JFrame f = new MenuTest("MenuTest");
f.setSize(new Dimension(300,230));
Toolkit tk = Toolkit.getDefaultToolkit();
Dimension d=tk.getScreenSize();
f.setLocation((d.width-f.getWidth())/2, (d.height-f.getHeight())/2);
f.setVisible(true);
}
public MenuTest(){}
public MenuTest(String title){
super(title);
}
private JMenuBar menubar = new JMenuBar();
private JMenu fileMenu = new JMenu("File");
private JMenuItem newMenuItem = new JMenuItem("new");
private JMenuItem openMenuItem = new JMenuItem("open");
private JMenuItem saveMenuItem = new JMenuItem("save");
private JMenuItem saveAsMenuItem = new JMenuItem("save as...");
private JMenuItem exitMenuItem = new JMenuItem("exit");
private JMenu configMenu = new JMenu("config");
private JMenuItem backgroudMusicMenuItem=new JMenuItem("back music");
private JMenu backColorMenu = new JMenu("back color");
private JRadioButtonMenuItem redItem = new JRadioButtonMenuItem("red");
private JRadioButtonMenuItem yellowItem = new JRadioButtonMenuItem("yellow");
private JRadioButtonMenuItem blueItem = new JRadioButtonMenuItem("blue");
{
this.setJMenuBar(menubar);
menubar.add(fileMenu);
menubar.add(configMenu);
fileMenu.add(newMenuItem);
fileMenu.add(openMenuItem);
fileMenu.add(saveMenuItem);
fileMenu.add(saveAsMenuItem);
fileMenu.addSeparator();
fileMenu.add(exitMenuItem);
configMenu.add(backgroudMusicMenuItem);
configMenu.add(backColorMenu);
backColorMenu.add(redItem);
backColorMenu.add(yellowItem);
backColorMenu.add(blueItem);
ButtonGroup colorGroup = new ButtonGroup();
colorGroup.add(redItem);
colorGroup.add(yellowItem);
colorGroup.add(blueItem);
}
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询