2 nishuihanllj nishuihanllj 于 2014.03.07 21:22 提问

JavaJFrame窗口中组件显示问题

我在JFrame中添加了菜单栏并为菜单项添加事件(在窗口中添加一个JComponent),但是窗口中不显示!
代码如下:
package frame;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.GridLayout;
import java.awt.event.*;
import java.awt.geom.Ellipse2D;
import java.awt.geom.Line2D;
import java.awt.geom.Rectangle2D;

import javax.swing.*;
import javax.swing.event.*;

public class Main
{

public static void main(String[] args) {
    // TODO Auto-generated method stub
    EventQueue.invokeLater(new Runnable()
    {
        public void run()
        {
            MenuFrame frame = new MenuFrame();
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            frame.setVisible(true);
        }
    });
}

}

class MenuFrame extends JFrame{

public MenuFrame()
{
    setTitle("TraPre");
    setBounds(200, 90, DEFAULT_WIDTH, DEFAULT_HEIGHT);

// setLayout(null);

    JMenu fileMenu = new JMenu("File");

    // demonstrates accelerators
    JMenuItem openItem = fileMenu.add(new TestAction("Open"));
    openItem.setAccelerator(KeyStroke.getKeyStroke("ctrl O"));

    fileMenu.addSeparator();

    JMenu SaveMenu = new JMenu("Save ");
    final JMenuItem SaveTraItem = new JMenuItem("Save Tra");
    SaveTraItem.setAccelerator(KeyStroke.getKeyStroke("ctrl T"));
    SaveMenu.add(SaveTraItem);
    final JMenuItem SaveEnItem = new JMenuItem("Save En");
    SaveEnItem.setAccelerator(KeyStroke.getKeyStroke("ctrl E"));
    SaveMenu.add(SaveEnItem);
    fileMenu.add(SaveMenu);

    fileMenu.add(new AbstractAction("Exit")
    {
        public void actionPerformed(ActionEvent event)
        {
            System.exit(0);
        }
    });

    fileMenu.addMenuListener(new MenuListener()
    {
        public void menuSelected(MenuEvent event)
        {
            SaveTraItem.setEnabled(false);
            SaveEnItem.setEnabled(false);
        }

        public void menuDeselected(MenuEvent event)
        {}

        public void menuCanceled(MenuEvent event)
        {}
    });

    // demonstrate icon
    Action cutAction = new TestAction("Cut");
    cutAction.putValue(Action.SMALL_ICON, new ImageIcon("cn.gif"));
    Action copyAction = new TestAction("Copy");
    copyAction.putValue(Action.SMALL_ICON, new ImageIcon("nz.gif"));
    Action pasteAction = new TestAction("Paste");
    pasteAction.putValue(Action.SMALL_ICON, new ImageIcon("gi.gif"));

    JMenu DrawMenu = new JMenu("画网格");
    DrawMenu.add(new DrawGridAction("Input Para"));

// JMenuItem ParaItem = new JMenuItem("输入参数");
// ParaItem.addActionListener(new ActionListener()
// {
// public void actionPerformed(ActionEvent event)
// {
// JComponent drawgrid = new DrawComponent();
// panel1.add(drawgrid);
// JButton button = new JButton();
// add(button, BorderLayout.NORTH);
// validate();
// }
// });
// ParaItem.addActionListener(new DrawGridAction());
// DrawMenu.add(ParaItem);

    JMenu ImportDataMenu = new JMenu("导入数据");
    JMenuItem SqlTraItem = new JMenuItem("从SQL导入原始记录");
    JMenuItem TxtEnItem = new JMenuItem("从TXT导入轨迹序列");
    ImportDataMenu.add(SqlTraItem);
    ImportDataMenu.add(TxtEnItem);

    JMenu GenerateTraMenu = new JMenu("生成轨迹");

    JMenu ComputEnMenu = new JMenu("熵值计算");
    JMenuItem AllTraItem = new JMenuItem("计算所有轨迹熵值");
    JMenuItem OneTraItem = new JMenuItem("计算一条轨迹的不同时间段的熵值");
    JMenuItem SomeTimeItem = new JMenuItem("计算所有轨迹不同时间段的熵值");
    ComputEnMenu.add(AllTraItem);
    ComputEnMenu.add(SomeTimeItem);
    ComputEnMenu.add(OneTraItem);

    JMenu TraPreMenu = new JMenu("轨迹预测");

    // demonstrate mnemonics
    JMenu helpMenu = new JMenu("Help");
    helpMenu.setMnemonic('H');

    JMenuItem indexItem = new JMenuItem("Index");
    indexItem.setMnemonic('I');
    helpMenu.add(indexItem);

    // you can also add the mnemonic key to an action
    Action aboutAction = new TestAction("About");
    aboutAction.putValue(Action.MNEMONIC_KEY, new Integer('A'));
    helpMenu.add(aboutAction);

    // add all top-level menus to menu bar
    JMenuBar menuBar = new JMenuBar();
    setJMenuBar(menuBar);

    menuBar.add(fileMenu);
    menuBar.add(DrawMenu);
    menuBar.add(ImportDataMenu);
    menuBar.add(GenerateTraMenu);
    menuBar.add(ComputEnMenu);
    menuBar.add(TraPreMenu);
    menuBar.add(helpMenu);

    // demonstrate pop-ups
    popup = new JPopupMenu();
    popup.add(cutAction);
    popup.add(copyAction);
    popup.add(pasteAction);

    JPanel panel = new JPanel();
    panel.setComponentPopupMenu(popup);
    add(panel);

    // the following line is a workaround for bug 4966109
    panel.addMouseListener(new MouseAdapter(){});

// DrawComponent drawgrid = new DrawComponent();
// add(drawgrid);
}

public static final int DEFAULT_WIDTH = 900;
public static final int DEFAULT_HEIGHT = 600;

private JPopupMenu popup;

// private DrawComponent drawgrid;

class TestAction extends AbstractAction
{
    public TestAction(String name)
    {
        super(name);
    }

    public void actionPerformed(ActionEvent event)
    {
        System.out.println(getValue(Action.NAME) + " selected.");
    }
}

class DrawGridAction extends AbstractAction{

    public DrawGridAction(String name)
    {
        super(name);
    }

    public void actionPerformed(ActionEvent event)
    {
        JButton button = new JButton();
        add(button, BorderLayout.SOUTH);
        JComponent drawgrid = new DrawComponent();
        getContentPane().add(drawgrid);
        validate();
        setVisible(true);
        System.out.println(getValue(Action.NAME) + " selected.");
    }
}

}

Csdn user default icon
上传中...
上传图片
插入图片
准确详细的回答,更有利于被提问者采纳,从而获得C币。复制、灌水、广告等回答会被删除,是时候展现真正的技术了!