在JPopupmenu中对齐图标和JCheckbox

时间:2012-05-05 20:30:06

标签: java swing jcheckbox imageicon jmenuitem

我在JPopupMenu中遇到了一些图标和复选框问题。复选框和图标未对齐

项目的创建方式如下:

JMenuItem deleteAttributeMenuItem = new JMenuItem(locale.getString("TREE_AttrDelete"), iconDelete);

JMenuItem primaryKeyAttributeMenuItem = new JCheckBoxMenuItem(locale.getString("TREE_AttrChkBoxPK"));

请看一下图片:

enter image description here

任何提示?

2 个答案:

答案 0 :(得分:2)

现在我找不到如何重新布局JCheckBoxMenuItem的正确方法,

但你是否同意使用(默认)Metal Look and Feel ???来摆脱Swing的标准输出,只是为了通过使用另一种外观来避免任何误解,因为API之间存在一些差异Swing的标准外观和感觉

enter image description here

来自教程代码(修改并删除了balasts和noises)

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class MenuLookDemo {

    private Icon errorIcon = UIManager.getIcon("OptionPane.errorIcon");
    private Icon infoIcon = UIManager.getIcon("OptionPane.informationIcon");
    private Icon warnIcon = UIManager.getIcon("OptionPane.warningIcon");
    private JTextArea output;
    private JScrollPane scrollPane;
    private JMenuBar menuBar;
    private JMenu menu;
    private JMenuItem menuItem;
    private JRadioButtonMenuItem rbMenuItem;
    private JCheckBoxMenuItem cbMenuItem;

    public JMenuBar createMenuBar() {
        menuBar = new JMenuBar();
        menu = new JMenu("A Menu");
        menu.getAccessibleContext().setAccessibleDescription("The only menu in this program that has menu items");
        menuBar.add(menu);
        menuItem = new JMenuItem("A text-only menu item", KeyEvent.VK_T);
        menuItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_1, ActionEvent.ALT_MASK));
        menuItem.getAccessibleContext().setAccessibleDescription("This doesn't really do anything");
        menu.add(menuItem);
        menuItem = new JMenuItem("Both text and icon", errorIcon);
        menu.add(menuItem);
        menu.addSeparator();
        ButtonGroup group = new ButtonGroup();
        rbMenuItem = new JRadioButtonMenuItem("A radio button menu item");
        rbMenuItem.setSelected(true);
        group.add(rbMenuItem);
        menu.add(rbMenuItem);
        rbMenuItem = new JRadioButtonMenuItem("Another one", infoIcon);
        group.add(rbMenuItem);
        menu.add(rbMenuItem);
        menu.addSeparator();
        cbMenuItem = new JCheckBoxMenuItem("A check box menu item", warnIcon);
        menu.add(cbMenuItem);
        cbMenuItem = new JCheckBoxMenuItem("Another one");
        menu.add(cbMenuItem);
        menu.addSeparator();
        return menuBar;
    }

    public Container createContentPane() {
        JPanel contentPane = new JPanel(new BorderLayout());
        contentPane.setOpaque(true);
        output = new JTextArea(5, 30);
        output.setEditable(false);
        scrollPane = new JScrollPane(output);
        contentPane.add(scrollPane, BorderLayout.CENTER);
        return contentPane;
    }

    private static void createAndShowGUI() {
        JFrame frame = new JFrame("MenuLookDemo");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        MenuLookDemo demo = new MenuLookDemo();
        frame.setJMenuBar(demo.createMenuBar());
        frame.setContentPane(demo.createContentPane());
        frame.setSize(450, 260);
        frame.setVisible(true);
    }

    public static void main(String[] args) {
        javax.swing.SwingUtilities.invokeLater(new Runnable() {

            public void run() {
                createAndShowGUI();
            }
        });
    }
}

这些方法以像素谈论可能性

enter image description here

答案 1 :(得分:2)

看看这个,为了实现你想要的,我做到了,

JCheckBoxMenuItem cbmi = new JCheckBoxMenuItem("Check Me", null, true);
cbmi.setMargin(new java.awt.Insets(5, 25, 5, 5));
cbmi.setIconTextGap(15);
cbmi.setHorizontalTextPosition(javax.swing.SwingConstants.LEFT);
helpMenu.add(cbmi);

这就是所说的东西的输出:

enter image description here