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("窗口首次可见");
}
} 展开
public void actionPerformed(ActionEvent e) {
System.out.println("点击子菜单");
JInternalFrame inframe = new JInternalFrame("内部框架:圆环", true, true, true, true);
Container c = inframe.getContentPane();
setLayout(new FlowLayout());
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);
////////////////////////////////增加代码
this.add(desktoppane);
this.add(c);
///////把 panel 放到 JFrame 中 因为 Panel 是不能单独存在的 必须放到 框架里面才能显示
setVisible(true);
}