1

我在 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 片段。

<!-- ================================= -->
<!-- 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

为了更清楚,请看图片:

菜单布局 http://avengerpaintball.co.za/screen01.jpg

File 是一个 JMenu 以及 Manage Account。所以现在帐户菜单采用文件菜单的样式,这是行不通的,因为它们有不同的背景图像。

谢谢

4

1 回答 1

1

are you meaning ???

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);
    }
}

and from file

<?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>
于 2011-12-01T21:56:19.020 回答