java 点击菜单显示面板_java程序未得到预想的结果,在点击菜单栏后面板上没有输出。...

我从相关的java的编程书上抄写了一个程序MyDesktopPane.java如下:importjava.awt.*;importjava.awt.event.*;importjavax.swing.*;publicclassMyDesktopPaneextendsJFrameimplementsA...

我从相关的java的编程书上抄写了一个程序MyDesktopPane.java如下:

import java.awt.*;

import java.awt.event.*;

import javax.swing.*;

public class MyDesktopPane extends JFrame implements ActionListener

{

final static JDesktopPane desktoppane=new JDesktopPane();

public MyDesktopPane()

{

super("MyDesktopPane");

JMenuBar menuBar=new JMenuBar();

JMenu menu=new JMenu("新增窗口");

JMenuItem menuItem=new JMenuItem("内部框架窗口");

menu.add(menuItem);

menuBar.add(menu);

setJMenuBar(menuBar);

getContentPane().add(desktoppane);

menuItem.addActionListener(this);

setSize(400,200);

setVisible(true);

}

public void actionPerformed(ActionEvent e)

{

System.out.println("点击子菜单");

JInternalFrame inframe=new JInternalFrame("内部框架:圆环",true,true,true,true);

Container c=inframe.getContentPane();

CirclePanel circlepanel=new CirclePanel(); //这是自己定义的类

JLabel label=new JLabel("圆环");

c.add(circlepanel,BorderLayout.CENTER);

c.add(label,BorderLayout.WEST);

int w=circlepanel.getImageWidthHeight().width+150;

int h=circlepanel.getImageWidthHeight().height+50;

inframe.setSize(w,h);

inframe.reshape(100,50,w,h);

inframe.setOpaque(true);

desktoppane.add(inframe);

setVisible(true);

}

public static void main(String[] args)

{

MyDesktopPane f=new MyDesktopPane();

f.addWindowListener(new MyWindowListener());

}

}

class CirclePanel extends JPanel

{

private ImageIcon imageicon;

public CirclePanel()

{

imageicon=new ImageIcon("noleft.gif");

}

public void paintComponent(Graphics g)

{

imageicon.paintIcon(this,g,0,0);

}

public Dimension getImageWidthHeight()

{

return new Dimension(imageicon.getIconWidth(),imageicon.getIconHeight());

}

}

其中用到的MyWindowListener这个类如下:

import java.awt.event.*;

public class MyWindowListener implements WindowListener

{

public void windowActivated(WindowEvent e)

{

System.out.println("窗口为活动状态");

}

public void windowClosed(WindowEvent e)

{

System.out.println("窗口为关闭状态");

System.exit(0);

}

public void windowClosing(WindowEvent e)

{

System.out.println("窗口正在关闭");

System.exit(0);

}

public void windowDeactivated(WindowEvent e)

{

System.out.println("窗口不再活动");

}

public void windowDeiconified(WindowEvent e)

{

System.out.println("窗口由最小化变为正常");

}

public void windowIconified(WindowEvent e)

{

System.out.println("窗口为最小化");

}

public void windowOpened(WindowEvent e)

{

System.out.println("窗口首次可见");

}

}

展开


版权声明:本文为weixin_32567181原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。