1

在我们的应用程序中,我们使用金属 L&F。我们正在使用一个可浮动的 JToolBar;碰巧在执行拖动行为时,它会出现在 Windows L&F 中。

谁能告诉我在拖动 JToolBar 时如何保持 Metal L&F?

谢谢

PD 我们的 JToolBar 位于用户 BorderLayout 布局管理器的 JPanel 容器中。

也许我解释得不好我的问题。因此,我发布了一个取自 The Java Tutorials 的示例,让任何人了解我的应用程序会发生什么。

如果您执行以下代码,主 JFrame 将出现以海洋主题装饰;但是当我拖动 JToolBar 时,它的装饰不是海洋。我能做些什么??。

提前谢谢了

package components;

/*
 * ToolBarDemo.java requires the following addditional files:
 * images/Back24.gif
 * images/Forward24.gif
 * images/Up24.gif
 */

 import java.awt.BorderLayout;
 import java.awt.Dimension;
 import java.awt.event.ActionEvent;
 import java.awt.event.ActionListener;
 import java.net.URL;
 import javax.swing.ImageIcon;
 import javax.swing.JButton;
 import javax.swing.JFrame;
 import javax.swing.JPanel;
 import javax.swing.JScrollPane;
 import javax.swing.JTextArea;
 import javax.swing.JToolBar;
 import javax.swing.SwingUtilities;
 import javax.swing.UIManager;
 import javax.swing.plaf.metal.MetalLookAndFeel;
 import javax.swing.plaf.metal.OceanTheme;

 public class ToolBarDemo extends JPanel
                          implements ActionListener {
         protected JTextArea textArea;
         protected String newline = "\n";
static final private String PREVIOUS = "previous";
static final private String UP = "up";
static final private String NEXT = "next";

public ToolBarDemo() {
    super(new BorderLayout());

    //Create the toolbar.
    JToolBar toolBar = new JToolBar("Still draggable");
    addButtons(toolBar);

    //Create the text area used for output.  Request
    //enough space for 5 rows and 30 columns.
    textArea = new JTextArea(5, 30);
    textArea.setEditable(false);
    JScrollPane scrollPane = new JScrollPane(textArea);

    //Lay out the main panel.
    setPreferredSize(new Dimension(450, 130));
    add(toolBar, BorderLayout.PAGE_START);
    add(scrollPane, BorderLayout.CENTER);
}

protected void addButtons(JToolBar toolBar) {
    JButton button = null;

    //first button
    button = makeNavigationButton("Back24", PREVIOUS,
                                  "Back to previous something-or-other",
                                  "Previous");
    toolBar.add(button);

    //second button
    button = makeNavigationButton("Up24", UP,
                                  "Up to something-or-other",
                                  "Up");
    toolBar.add(button);

    //third button
    button = makeNavigationButton("Forward24", NEXT,
                                  "Forward to something-or-other",
                                  "Next");
    toolBar.add(button);
}

protected JButton makeNavigationButton(String imageName,
                                       String actionCommand,
                                       String toolTipText,
                                       String altText) {
    //Look for the image.
    String imgLocation = "images/"
                         + imageName
                         + ".gif";
    URL imageURL = ToolBarDemo.class.getResource(imgLocation);

    //Create and initialize the button.
    JButton button = new JButton();
    button.setActionCommand(actionCommand);
    button.setToolTipText(toolTipText);
    button.addActionListener(this);

    if (imageURL != null) {                      //image found
        button.setIcon(new ImageIcon(imageURL, altText));
    } else {                                     //no image found
        button.setText(altText);
        System.err.println("Resource not found: "
                           + imgLocation);
    }

    return button;
}

public void actionPerformed(ActionEvent e) {
    String cmd = e.getActionCommand();
    String description = null;

    // Handle each button.
    if (PREVIOUS.equals(cmd)) { //first button clicked
        description = "taken you to the previous <something>.";
    } else if (UP.equals(cmd)) { // second button clicked
        description = "taken you up one level to <something>.";
    } else if (NEXT.equals(cmd)) { // third button clicked
        description = "taken you to the next <something>.";
    }

    displayResult("If this were a real app, it would have "
                    + description);
}

protected void displayResult(String actionDescription) {
    textArea.append(actionDescription + newline);
    textArea.setCaretPosition(textArea.getDocument().getLength());
}

/**
 * Create the GUI and show it.  For thread safety,
 * this method should be invoked from the
 * event dispatch thread.
 */
private static void createAndShowGUI() {
    //Create and set up the window.
    JFrame frame = new JFrame("ToolBarDemo");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    //Add content to the window.
    frame.add(new ToolBarDemo());

    //Display the window.
    frame.pack();
    frame.setVisible(true);
}

public static void main(String[] args) {
    //Schedule a job for the event dispatch thread:
    //creating and showing this application's GUI.
    SwingUtilities.invokeLater(new Runnable() {
        public void run() {
            setsLF();
        createAndShowGUI();
        }
    });
}

    /**
 *
 */
private static void setsLF() {
    try {
        UIManager.setLookAndFeel(UIManager.getCrossPlatformLookAndFeelClassName());
        MetalLookAndFeel.setCurrentTheme(new OceanTheme());
        UIManager.setLookAndFeel(new MetalLookAndFeel());
    } catch (ClassNotFoundException ex) {
        java.util.logging.Logger.getLogger(ToolBarDemo.class.getName()).log (java.util.logging.Level.SEVERE, null, ex);
    } catch (InstantiationException ex) {
        java.util.logging.Logger.getLogger(ToolBarDemo.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
    } catch (IllegalAccessException ex) {
        java.util.logging.Logger.getLogger(ToolBarDemo.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
    } catch (javax.swing.UnsupportedLookAndFeelException ex) {
        java.util.logging.Logger.getLogger(ToolBarDemo.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
    }
    JFrame.setDefaultLookAndFeelDecorated(Boolean.TRUE);
    return;
}

}

4

2 回答 2

1

正如你所描述的,我做了一个小项目:

public class LafTest
{
    public static void main(String[] args)
    {
        JFrame frame = new JFrame();
        frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);

        frame.setSize(500, 500);

        JPanel panel = new JPanel(new BorderLayout());
        JToolBar toolbar = new JToolBar();

        toolbar.add(new JButton("button1"));
        toolbar.add(new JButton("button2"));

        panel.add(toolbar, BorderLayout.PAGE_START);

        frame.add(panel);

        frame.setVisible(true);
    }
}

对我来说效果很好,JToolBar 一直都有 Metal-LAF​​。(操作系统:Windows 7 x64,java 版本“1.7.0_09”)

请将您的代码与此代码段进行比较。可能您在某处使用了 UIManager 类。如果您仍然无法解决此问题,您应该发布一些使用过的代码,也许还有一些关于您的操作系统和使用的 Java 版本的更多详细信息。

于 2012-11-23T00:11:49.917 回答
1

看起来现在工具栏的顶层容器是 JDialog 类型的,所以你也可以设置 lafDecoration :

JFrame.setDefaultLookAndFeelDecorated(Boolean.TRUE);
JDialog.setDefaultLookAndFeelDecorated(true);

适用于 jdk7 和 vista,未测试其他环境。

于 2012-11-27T16:51:34.583 回答