【问题标题】:issue about JComboBox itemListener in two level JMenu关于两级 JMenu 中的 JComboBox itemListener 的问题
【发布时间】:2018-02-02 17:57:03
【问题描述】:

我是新人,但我一直在寻找,现在是时候请教各位伙伴了。 我在 java 中有这个简单的应用程序,其中包括 JComboBox 的 itemListener。 我不知道为什么,但是它不听,但是当我将 JComboBox 放在层次结构的上层时,它可以工作,并且 itemListener 工作正常。任何想法为什么它在较低级别不起作用?

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

public class Notatnik extends JFrame {

JMenuBar menu;
JMenu tools, fontColor;
JComboBox<String> colors;

    public Notatnik() {
        this.setSize(500, 400);

        menu = new JMenuBar();
        tools = new JMenu("tools");
        fontColor = new JMenu("Font color");
            colors = new JComboBox<String>();
        colors.addItem("red");
        colors.addItem("green");
        colors.addItem("blue");

        colors.addItemListener(new ItemListener() {
            @Override
            public void itemStateChanged(ItemEvent e) {
                System.out.println(e.getItem().toString());
            }
        });

        fontColor.add(colors);
        tools.add(fontColor);
        menu.add(tools);
        this.setJMenuBar(menu);

        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        this.setVisible(true);
    }
}

【问题讨论】:

  • 等等,你是在向 JMenu 添加一个 JComboBox 吗?
  • 我的朋友补充说,我只是在想“嘿,这可能吗?”所以我检查并添加到 JMenu,然后添加到 JMenuBar,使用 JMenuBar 可以工作,而使用 JMenu 则不能。那么...是否可以添加到JMenu中,然后如何控制呢?
  • 不要认为这是可能的(无需对 UI 进行大量修改)。相反,我会: 1) 只为每种颜色显示 JRadioMenuItem。 2) 在添加到选项窗格的组合框中显示带有颜色的 JOptionPane。

标签: java swing jcombobox jmenubar itemlistener


【解决方案1】:

无法将JComboBox 添加到JMenu 组件(不破坏侦听器)。因此,我稍微改变了实现:

import java.awt.Color;
import java.awt.EventQueue;

import javax.swing.*;

public class Stackoverflow {

    // Initialize the color to your desired choice.
    private static Color color = Color.RED;

    public static void main(final String[] arguments) {
        final JFrame frame = new JFrame("Stackoverflow | Answer");
        EventQueue.invokeLater(() -> {
            final JMenuBar menuBar = new JMenuBar();
            final JMenu menu = new JMenu("Edit");
            // This is just for debugging.
            final JLabel currentColor = new JLabel(color.getRed() + "r " + color.getGreen() + "g " + color.getBlue() + "b");
            final JMenuItem item01 = new JMenuItem("Red");
            final JMenuItem item02 = new JMenuItem("Green");
            final JMenuItem item03 = new JMenuItem("Blue");
            item01.addActionListener((event -> {
                Stackoverflow.setColor(currentColor, Color.RED);
            }));
            item02.addActionListener((event -> {
                Stackoverflow.setColor(currentColor, Color.GREEN);
            }));
            item03.addActionListener((event -> {
                Stackoverflow.setColor(currentColor, Color.BLUE);
            }));
            final JMenu parent = new JMenu("Color");
            parent.add(currentColor);
            parent.add(item01);
            parent.add(item02);
            parent.add(item03);
            menu.add(parent);
            menuBar.add(menu);
            frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
            frame.setSize(500, 400);
            frame.setLocationRelativeTo(null);
            frame.setJMenuBar(menuBar);
            frame.setVisible(true);
        });
    }

    public static void setColor(final JLabel label, final Color color) {
        // Update the text of the specified JLabel. Edit this part to change the actual font color.
        label.setText(color.getRed() + "r " + color.getGreen() + "g " + color.getBlue() + "b");
        Stackoverflow.color = color;
    }
}

我知道代码不整洁也不高效,但它工作得很好。在我忘记之前,你应该明确地访问this关于何时继承一个类的文章。

【讨论】:

  • 非常感谢,满足了我的好奇心。 :)
  • @KanoidSan 没问题。 :)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-05-19
  • 1970-01-01
相关资源
最近更新 更多