Java Swing 工具栏

标签 java swing jtoolbar

我已经创建了 JToolBar (Java Swing)。我在包含 JToolBar 的框架上设置了背景图像。我希望我的 JToolBar 是透明的,以便保持在框架上的图像应该是可见的。我正在使用 setOpaque(false) 但它对我的工具栏没有任何影响。之前有人对此感到厌烦吗?我的代码如下

import javax.swing.ImageIcon;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

public class NewJFrame extends javax.swing.JFrame {
    static NewJFrame dialog = null;

    /** Creates new form NewJFrame */
    public NewJFrame() {
        initComponents();
        jToolBar1.setOpaque(false);
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">
    private void initComponents() {

        jToolBar1 = new javax.swing.JToolBar();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        jToolBar1.setRollover(true);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jToolBar1, javax.swing.GroupLayout.DEFAULT_SIZE, 400, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(jToolBar1, javax.swing.GroupLayout.PREFERRED_SIZE, 25, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(275, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>

    /**
    * @param args the command line arguments
    */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {



            public void run() {

                try {
                    try {
                        // Set cross-platform Java L&F (also called "Metal")
                        UIManager.setLookAndFeel(UIManager.
                                getSystemLookAndFeelClassName());

                    } catch (UnsupportedLookAndFeelException ex) {
                        ex.printStackTrace();
                    }
                } catch (ClassNotFoundException e) {
                    e.printStackTrace();
                } catch (InstantiationException e) {
                    e.printStackTrace();
                } catch (IllegalAccessException e) {
                    e.printStackTrace();
                }
                ImagePanel panel = new ImagePanel(new ImageIcon("image").getImage());
                dialog = new NewJFrame();
                dialog.getContentPane().add(panel);
                dialog.setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify
    private javax.swing.JToolBar jToolBar1;
    // End of variables declaration

}

最佳答案

JToolBar 创建 JButton 以包含您在其上设置的操作。默认情况下,JButton 是不透明的。要获得您描述的效果,您需要覆盖 JToolBar.createActionComponent。

例如:

    jToolBar1 = new javax.swing.JToolBar() {
        @Override
        protected JButton createActionComponent(Action a) {
            JButton jb = super.createActionComponent(a);
            jb.setOpaque(false);
            return jb;
        }
    };

注意:YMMV 取决于使用的 LAF。

关于Java Swing 工具栏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1886329/

相关文章:

java - 检查可点击的网络元素时出错

带有 ImageIcon 列的 Java JTable TableCellRenderer

java - 如何跟踪鼠标在桌面上的点击

java - 拖动JToolBar时如何保持Metal L&F

java - 使 jtoolbar 透明

java - 如何从 JPanel 加载/保存图像

java - 将restful ajax请求映射到spring

Swing JToolbar按钮按下

java - 从 android 发出 JSON 请求

java - 将 JFrame 传输到 JPanel