第3个回答 2011-11-19
public class MyDesktopPane extends JFrame implements ActionListener {
final static JDesktopPane desktopPane = new JDesktopPane();
public MyDesktopPane() {
super("MyDesktopPane.java:DesktopPane测试");
JMenuBar menuBar = new JMenuBar();
JMenu menu = new JMenu("新增窗口");
JMenuItem menultem = new JMenuItem("内部框架窗口");
menu.add(menultem);
menuBar.add(menu);
setJMenuBar(menuBar);
getContentPane().add(desktopPane);
menultem.addActionListener(this);
setSize(350, 200);
setVisible(true);
}
public void actionPerformed(ActionEvent e) {
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);
inFrame.setVisible(true);
}
public static void main(String args[]) {
MyDesktopPane app = new MyDesktopPane();
app.addWindowListener(new MyWindowListener());
}
}
class CirclePanel extends JPanel {
private ImageIcon imglcon;
public CirclePanel() {
try {
imglcon = new ImageIcon(new URL(""));
} catch (MalformedURLException ex) {
Logger.getLogger(CirclePanel.class.getName()).log(Level.SEVERE, null, ex);
}
}
public void paint(Graphics g) {
g.drawImage(imglcon.getImage(), 0, 0, this);
}
public Dimension getImageWidthHeight() {
return new Dimension(imglcon.getIconWidth(), imglcon.getIconHeight());
}
}
class MyWindowListener extends WindowAdapter {
public void windowClosing(WindowEvent e) {
System.exit(1);
}
}