java 怎样设置当JMenuItem被选择后的背景色
1个回答
展开全部
我真佩服我自己,这方法都能想出来,呵呵。。。
import javax.swing.*;
import java.awt.Color;
import java.awt.Graphics;
import javax.swing.plaf.basic.BasicMenuItemUI;
public class TestMenu extends JFrame {
JMenuBar mbrTest;
JMenu mnuTestGroup;
JMenuItem mnuTestItem1,mnuTestItem2;
Color oldColor;
public TestMenu(){
mnuTestItem1=new JMenuItem("测试菜单1");
mnuTestItem1.setUI(new MyMenuItemUI(Color.BLUE,Color.YELLOW));
mnuTestItem2=new JMenuItem("测试菜单2");
mnuTestItem2.setUI(new MyMenuItemUI(Color.RED,Color.WHITE));
mnuTestGroup=new JMenu("测试菜单");
mnuTestGroup.add(mnuTestItem1);
mnuTestGroup.add(mnuTestItem2);
mbrTest=new JMenuBar();
mbrTest.add(mnuTestGroup);
setJMenuBar(mbrTest);
setSize(800,600);
setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
setVisible(true);
}
public static void main(String[] args){
new TestMenu();
}
class MyMenuItemUI extends BasicMenuItemUI{
public MyMenuItemUI(Color bgColor,Color fgColor){
super.selectionBackground=bgColor;
super.selectionForeground=fgColor;
}
}
}
import javax.swing.*;
import java.awt.Color;
import java.awt.Graphics;
import javax.swing.plaf.basic.BasicMenuItemUI;
public class TestMenu extends JFrame {
JMenuBar mbrTest;
JMenu mnuTestGroup;
JMenuItem mnuTestItem1,mnuTestItem2;
Color oldColor;
public TestMenu(){
mnuTestItem1=new JMenuItem("测试菜单1");
mnuTestItem1.setUI(new MyMenuItemUI(Color.BLUE,Color.YELLOW));
mnuTestItem2=new JMenuItem("测试菜单2");
mnuTestItem2.setUI(new MyMenuItemUI(Color.RED,Color.WHITE));
mnuTestGroup=new JMenu("测试菜单");
mnuTestGroup.add(mnuTestItem1);
mnuTestGroup.add(mnuTestItem2);
mbrTest=new JMenuBar();
mbrTest.add(mnuTestGroup);
setJMenuBar(mbrTest);
setSize(800,600);
setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
setVisible(true);
}
public static void main(String[] args){
new TestMenu();
}
class MyMenuItemUI extends BasicMenuItemUI{
public MyMenuItemUI(Color bgColor,Color fgColor){
super.selectionBackground=bgColor;
super.selectionForeground=fgColor;
}
}
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询