编写如下图的菜单画面,选择“打开”选项后,出现图1的画面,按下选择“颜色”选项后出现图2的画面,选择“退出”选项后,结束程序。
(参考API的JColorChooser和JFileChooser)
就是一个类似记事本的菜单栏,要求点了选项之后出现一个调色板,点打开出现一个选择文件的对话框实在一点想法都没有。谢谢了。。

解决方案 »

  1.   

    好久不做swing了,不过并不难,
    借花献佛,给你个思路:
    import javax.swing.*;
    import java.awt.event.*;
    import java.awt.*;public class NoteBook extends JFrame {
    private JTextArea ta; private JMenuBar mb; private JMenu file; private JMenuItem open, color, exit; public NoteBook() {
    super("记事本");
    Container c = getContentPane();
    mb = new JMenuBar();
    open = new JMenuItem("打开");
    color=new JMenuItem("颜色");
    exit = new JMenuItem("退出");
    open.addActionListener(new ActionListener(){
    public void actionPerformed(ActionEvent e){
    JFileChooser fc=new JFileChooser();
    fc.setSize(300,300);
    fc.showOpenDialog(NoteBook.this);
    }
    });
    color.addActionListener(new ActionListener(){
    public void actionPerformed(ActionEvent e) {
    JColorChooser.showDialog(NoteBook.this, "请选择颜色", Color.RED);
    }
    });
    exit.addActionListener(new ActionListener(){
    public void actionPerformed(ActionEvent e) {
    System.exit(0);
    }
    });

    file=new JMenu("文件");
    mb.add(file);
    file.add(open);
    file.add(color);
    file.add(exit);
    setJMenuBar(mb);
    ta = new JTextArea();
    add(ta);
    setSize(800, 600);
    setVisible(true);
    } public static void main(String args[]) {
    NoteBook nb = new NoteBook();
    }


    }
      

  2.   

    建议到jdk目录看那些demo
    初学时,很有用的
      

  3.   

    public NoteBook() {
            super("记事本");
            Container c = getContentPane();
            mb = new JMenuBar();
            open = new JMenuItem("打开");
            color=new JMenuItem("颜色");
            exit = new JMenuItem("退出");
            open.addActionListener(new ActionListener(){
                public void actionPerformed(ActionEvent e){
                    JFileChooser fc=new JFileChooser();
                    fc.setSize(300,300);
                    fc.showOpenDialog(NoteBook.this);
                }
            });
    这个难道不是函数里面套函数吗?为什么编译能通过呢?
    运行时一片异常,是因为没有main里面的东西吗?
      

  4.   

     open.addActionListener(new ActionListener(){ 这个算什么东西啊?