Java 通过单击JMenu项选择颜色选择器

Java 通过单击JMenu项选择颜色选择器,java,oracle,swing,jmenuitem,jcolorchooser,Java,Oracle,Swing,Jmenuitem,Jcolorchooser,我试过不同网站的代码,还有一个是关于颜色的。如何让颜色选择器与jmenu项的按下一起工作 我查看了并使用以下代码实现了原始类: JMenuItem clr = new JMenuItem("Font Color"); clr.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent e) { ColorChooserDemo

我试过不同网站的代码,还有一个是关于颜色的。如何让颜色选择器与jmenu项的按下一起工作

我查看了并使用以下代码实现了原始类:

   JMenuItem clr = new JMenuItem("Font Color");
    clr.addActionListener(new ActionListener(){

        public void actionPerformed(ActionEvent e)
        {
            ColorChooserDemo ccd = new ColorChooserDemo();
            ccd.setVisible(true);
        }
    });
但当我按下菜单项时,这似乎什么也做不了

类代码来自oracle网页。这些是我使用的以下类(当然,缩短到手头的问题)。我正在做一个记事本程序,因为我要重新开始编程,并刷新我的记忆,了解如何用java做事情。目前的问题是,当我单击jmenuitem clr(字体颜色)时,我无法使颜色选择器出现。以下代码显示了我目前所拥有的:

颜色选择器类:

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

/* ColorChooserDemo.java requires no other files. */
@SuppressWarnings("serial")
public class ColorChooserDemo extends JPanel
                              implements ChangeListener {

    protected JColorChooser tcc;
    protected JLabel banner;

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

        //Set up color chooser for setting text color
        tcc = new JColorChooser();
        tcc.getSelectionModel().addChangeListener(this);
        tcc.setBorder(BorderFactory.createTitledBorder(
                                             "Choose Text Color"));

        add(tcc, BorderLayout.PAGE_END);
    }

    public void stateChanged(ChangeEvent e) {
        Color newColor = tcc.getColor();
        FirstWindow.ta1.setForeground(newColor);
    }

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

        //Create and set up the content pane.
        JComponent newContentPane = new ColorChooserDemo();
        newContentPane.setOpaque(true); //content panes must be opaque
        frame.setContentPane(newContentPane);

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

    public static void main(String[] args) {
        //Schedule a job for the event-dispatching thread:
        //creating and showing this application's GUI.
        javax.swing.SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                createAndShowGUI();
            }
        });
    }
}
主要类别:

导入java.awt.EventQueue

public class Main{
    protected static Object fw;

    public static void main(String[] args)
    {
        EventQueue.invokeLater(new Runnable(){

            @Override
            public void run() {
                // TODO Auto-generated method stub
                try
                {
                    FirstWindow fw = new FirstWindow();
                    fw.setVisible(true);
                } catch (Exception e)
                {
                    e.printStackTrace();
                }

            }

        });
    }
}
第一窗口类:

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


public class FirstWindow extends JFrame {
    private static final long serialVersionUID = 1L;
    protected JColorChooser tcc;
    protected static JTextArea ta1;

    public FirstWindow() {
        super("Note Pad");

        Font font = new Font("Verdana", Font.BOLD, 12);

        //Setting the size of the note pad
        setSize(650, 745);
        setDefaultCloseOperation(EXIT_ON_CLOSE);

        //Create the MenuBar
        JMenuBar mb = new JMenuBar();
        setJMenuBar(mb);

        //Create the panel to hold everything in
        JPanel p = new JPanel();

        //Create the Text Area
        final JTextArea ta1 = new JTextArea();
        ta1.setFont(font);
        ta1.setMargin(new Insets(5,5,5,5));
        ta1.setLineWrap(true);
        ta1.setWrapStyleWord(true);

        //Create the Scroll Pane to hold the Text Area 
        final JScrollPane sp = new JScrollPane(ta1);
        sp.setPreferredSize(new Dimension(625,675));
        sp.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED);

        //Create the menu's
        JMenu format = new JMenu("Format");

            //Create menu item for picking font color
        JMenuItem clr = new JMenuItem("Font Color");
        clr.addActionListener(new ActionListener(){

            public void actionPerformed(ActionEvent e)
            {
                ColorChooserDemo ccd = new ColorChooserDemo();
                ccd.setVisible(true);
            }
        });

        //adding the menu items to the file menu tab

        //adding menu items to the edit tab

        //adding menu items to the format tab
        format.add(clr);

        //adding the menus to the menu bar
        mb.add(format);


        //adding the scroll pane to the panel
        p.add(sp);
        add(p, BorderLayout.CENTER);

    }
}

显示颜色选择器的最简单方法可能是:

    clr.addActionListener(new ActionListener(){

        public void actionPerformed(ActionEvent e)
        {
            ColorChooserDemo.createAndShowGUI();
        }
    });
    clr.addActionListener(new ActionListener(){

        public void actionPerformed(ActionEvent e)
        {
            Color c = JColorChooser.showDialog(ta1, "ColorChooserDemo", null);
            ta1.setForeground(c);
        }
    });
在ColorChooserDemo类中,有方法
private static void createAndShowGUI()
,您应该将其声明为public

然后,将菜单项的ActionListener替换为以下内容:

    clr.addActionListener(new ActionListener(){

        public void actionPerformed(ActionEvent e)
        {
            ColorChooserDemo.createAndShowGUI();
        }
    });
    clr.addActionListener(new ActionListener(){

        public void actionPerformed(ActionEvent e)
        {
            Color c = JColorChooser.showDialog(ta1, "ColorChooserDemo", null);
            ta1.setForeground(c);
        }
    });
您的ColorChooserDemo类扩展了JPanel,而不是JFrame。首先需要一个JFrame,然后添加面板,然后显示JFrame。这就是
createAndShowGUI()
方法中发生的情况

编辑: 我知道您只想知道在选择菜单项时如何显示ColorChooserDemo

但是,要实际设置颜色,您可能希望跳过使用自己的ColorChooserDemo类,并用以下代码替换ActionListener代码:

    clr.addActionListener(new ActionListener(){

        public void actionPerformed(ActionEvent e)
        {
            ColorChooserDemo.createAndShowGUI();
        }
    });
    clr.addActionListener(new ActionListener(){

        public void actionPerformed(ActionEvent e)
        {
            Color c = JColorChooser.showDialog(ta1, "ColorChooserDemo", null);
            ta1.setForeground(c);
        }
    });
正如Andrew所建议的那样,a不仅可以让我们更容易地提供解决方案,还可以帮助您找出并理解该做什么。无论如何,这里有一个在按下
JMenuItem
后打开颜色选择器的快速示例:

item.addActionListener(new ActionListener() {
    @Override
    public void actionPerformed(ActionEvent e) {
        JColorChooser jColorChooser = new JColorChooser();

        JDialog jDialog = new JDialog();
        jDialog.setContentPane(jColorChooser);
        jDialog.pack();
        jDialog.setVisible(true);
    }
});

乐:(对不起,我是新来的颜色选择者)或者干脆用

更快地获得更好的帮助,发布一个。“我想知道,当您单击JMenuItem时,是否可以通过操作来实现它。”当然,这是可能的。顺便说一句,这个“问题”不包含“?”。我已经尝试过了,并在类和jColorChooser.getSelectionModel().addChangeListener(listener)的末尾添加了public void stateChanged(ChangeEvent){…};进入ActionListener,但不会起作用。在没有ActionListener的情况下,它会设置颜色,但只有在您单击菜单项两次之后,并且在单击JColorChooser中的颜色时不会更改颜色。我也尝试过这种方法,但由于它试图在FirstWindow类中设置JTextArea的颜色,所以出现了一个错误。