从Synth中排除某些Jmenu

时间:2011-12-01 21:26:30

标签: java swing jmenu jmenuitem synth

我在XML文件中使用Synth L& F,我到目前为止设置了所有内容,但是我没有嵌套的JMenu,我不希望嵌套的JMenu具有顶级JMenu的风格。

    JMenu accountMenu = new JMenu("Manage Account");
    JMenuItem editUsername = new JMenuItem("Change Username");
    JMenuItem editPassword = new JMenuItem("Change Password");
    accountMenu.add(editUsername);
    accountMenu.add(editPassword);
    fileMenu.add(accountMenu);

这只是从我的代码中获取并编辑以适应,并不代表实际代码。

然后这是我正在使用的Synth XML I代码段。

<!-- ================================= -->
<!-- MENU -->
<!-- ================================= -->
<style id="MenuStyle">
    <insets top="2" bottom="2" right="10" left="7" />
    <state>
        <font name="Calibre" size="14" style="BOLD" />
        <color value="#cccccc" type="TEXT_FOREGROUND" />
    </state>

    <state value="DISABLED">
        <color value="WHITE" type="TEXT_FOREGROUND" />
    </state>

    <state value="SELECTED">
        <imagePainter method="MenuBackground" path="Bin/Images/headerbarActive.jpg"
            sourceInsets="0 0 0 0" />
        <color value="WHITE" type="TEXT_FOREGROUND" />
    </state>
</style>
<bind style="MenuStyle" type="region" key="Menu" />

<!-- ================================= -->
<!-- MENU ITEM-->
<!-- ================================= -->
<style id="MenuItemStyle">
    <insets top="3" bottom="3" right="20" left="5" />
    <state>
        <imagePainter method="MenuItemBackground" path="Bin/Images/menuItem.jpg"
            sourceInsets="0 0 0 0" />
        <font name="Calibre" size="14" style="PLAIN" />
        <color value="#cccccc" type="TEXT_FOREGROUND" />
    </state>
    <state value="MOUSE_OVER">
        <imagePainter method="MenuItemBackground" path="Bin/Images/menuItemActive.jpg"
            sourceInsets="0 0 0 0" />
        <color value="#000000" type="TEXT_FOREGROUND" />
    </state>
    <state value="DISABLED">
        <color value="WHITE" type="TEXT_FOREGROUND" />
    </state>
</style>
<bind style="MenuItemStyle" type="region" key="MenuItem" />

所以现在我期待目标让我们只说AccountMenu JMenu并给它与JMenuItems相同的样式而不是JMenus

更清楚一点,请看图片:

Menu Layout http://avengerpaintball.co.za/screen01.jpg

文件是JMenu以及管理帐户。因此,现在“帐户菜单”采用“文件菜单样式”,这不起作用,因为它们具有不同的背景图像。

由于

1 个答案:

答案 0 :(得分:2)

你是什​​么意思???

import javax.swing.*;
import javax.swing.plaf.synth.SynthLookAndFeel;

public class ButtonRollover extends JFrame {

    private static final long serialVersionUID = 1L;

    public ButtonRollover() {
        JMenuBar fileMenu = new JMenuBar();
        setJMenuBar(fileMenu);
        JMenu accountMenu = new JMenu("Manage Account");
        JMenuItem editUsername = new JMenuItem("Change Username");
        JMenuItem editPassword = new JMenuItem("Change Password");
        accountMenu.add(editUsername);
        accountMenu.add(editPassword);
        fileMenu.add(accountMenu);
    }

    public static void main(String[] args) {
        try {
            SynthLookAndFeel laf = new SynthLookAndFeel();
            laf.load(ButtonRollover.class.getResourceAsStream("menusynt.xml"), ButtonRollover.class);
            UIManager.setLookAndFeel(laf);
        } catch (Exception e) {
            e.printStackTrace();
        }
        ButtonRollover frame = new ButtonRollover();
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setSize(400, 200);
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
    }
}

和文件

<?xml version="1.0" encoding="UTF-8"?>
<root>
<!-- ================================= -->
<!-- MENU menusynt.xml -->
<!-- ================================= -->
<style id="MenuStyle">
    <insets top="2" bottom="2" right="10" left="7" />
    <state>
        <font name="Calibre" size="14" style="BOLD" />
        <color value="#cccccc" type="TEXT_FOREGROUND" />
    </state>

    <state value="DISABLED">
        <color value="WHITE" type="TEXT_FOREGROUND" />
    </state>

    <state value="SELECTED">
        <!--<imagePainter method="MenuBackground" path="src/Paint/Images/failed.png"
            sourceInsets="0 0 0 0" />-->
        <color value="WHITE" type="TEXT_FOREGROUND" />
    </state>
</style>
<bind style="MenuStyle" type="region" key="Menu" />

<!-- ================================= -->
<!-- MENU ITEM-->
<!-- ================================= -->
<style id="MenuItemStyle">
    <insets top="3" bottom="3" right="20" left="5" />
    <state>
        <!-- <imagePainter method="MenuItemBackground" path="src/Paint/Images/passed.png"
            sourceInsets="0 0 0 0" />-->
        <font name="Calibre" size="14" style="PLAIN" />
        <color value="#cccccc" type="TEXT_FOREGROUND" />
    </state>
    <state value="MOUSE_OVER">
       <!--  <imagePainter method="MenuItemBackground" path="src/Paint/Images/failed.png"
            sourceInsets="0 0 0 0" />-->
        <color value="#000000" type="TEXT_FOREGROUND" />
    </state>
    <state value="DISABLED">
        <color value="WHITE" type="TEXT_FOREGROUND" />
    </state>
</style>
<bind style="MenuItemStyle" type="region" key="MenuItem" />

</root>