java中JMenuBar的这个功能怎么实现?

java中JMenuBar的这个功能怎么实现?,第1张

你可以在图片和JMenuItem的设置背景颜色。具体的给你看一个例子:(设定方法,见注)

进口的java.awt.BorderLayout 

进口java.awt.Color中 

导入java.awt中。 event.ActionEvent 

进口java.awt.event.ActionListener 

进口javax.swing.ImageIcon中 

进口javax.swing.JButton中 

进口的javax。 swing.JFrame 

进口javax.swing.JMenu中 

进口javax.swing.JMenuBar中 

进口javax.swing.JMenuItem中 

进口javax.swing.JSeparator 

公共E类扩展JFrame中实现的ActionListener {

 JButton的紧密= NULL 

 JMenuItem的mopen = NULL 

 JMenuItem的mclose = NULL 

 JMenuItem的MEXIT = NULL 

 E(){

紧密=新的JButton(“退出”) 

添加(接近,BorderLayout.SOUTH) 

 close.addActionListener(本) 

的JMenuBar JMB =新的JMenuBar() 

 JMenu的文件=新的JMenu(“文件”) 

 mopen =新的JMenuItem(“打开”) 

的ImageIcon logoing =新的ImageIcon(“black_bishop.gif”) / /这里定义的图标图片

 mopen.setIcon(logoing) / /这里设置图标的图片到菜单项

 mopen.setBackground(颜色发黄。)/ /在哪里设置背景色为黄色的MenuItem 

 mclose =新的JMenuItem(“关闭”) 

 MEXIT =新的JMenuItem(“退出”) 

了JSeparator分隔符=新了JSeparator() 

file.add(mopen) 

 file.add(mclose) 

 file.add(分离器) 

文件。添加(MEXIT) 

 jmb.add(文件) 

 setJMenuBar(JMB) 

 mclose.addActionListener(本) 

 mexit.addActionListener(本) 

的SetSize(300,300) 

其setVisible(真) 

 setDefaultCloseOperation(DISPOSE_ON_CLOSE) 

 公共静态无效的主要(字串[] args){

新的E() 

 公共无效的actionPerformed(ActionEvent的AE){

如果(ae.getSource()==接近){

处置() 

}否则,如果(ae.getSource()== mclose){

的dispose() 

}否则,如果(ae.getSource()== MEXIT){

的dispose() 

 }

JMenuBar menuBar = new JMenuBar()

JMenu file = new JMenu("文件"),

Icon new= new ImageIcon("res/new.png")

Icon save= new ImageIcon("res/save.png")

JMenuItem newItem = new JMenuItem("查看结果",new),

saveItem = new JMenuItem("随机数据",save)

这样 就行了 ,很简单。

setVerticalAlignment

public void setVerticalAlignment(int alignment)

设置图标和文本的垂直对齐方式


欢迎分享,转载请注明来源:内存溢出

原文地址: http://www.outofmemory.cn/bake/11416660.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-05-15
下一篇 2023-05-15

发表评论

登录后才能评论

评论列表(0条)

保存