【问题标题】:Adding JDesktopPane to JPanel [closed]将 JDesktopPane 添加到 JPanel [关闭]
【发布时间】:2014-05-17 11:26:20
【问题描述】:

是否可以将JDesktopPaneJInternalFrame 添加到JPanel 如果可以,我该怎么做。我尝试添加它,但它无法正常工作。

【问题讨论】:

标签: java swing jpanel jinternalframe jdesktoppane


【解决方案1】:

"是否可以将 JDesktopPane 和 JInternalFrame 添加到 JPanel"

当然可以

desktop = createDesktopPane();
JInternalFrame iFrame = createInternalFrame();
desktop.add(iFrame);
JPanel panel = new JPanel(new BorderLayout());
panel.setBorder(new TitledBorder("DesktopPane"));
panel.add(desktop);

import java.awt.BorderLayout;
import java.awt.Graphics;
import java.awt.Image;
import java.io.IOException;
import java.net.URL;

import javax.imageio.ImageIO;
import javax.swing.JDesktopPane;
import javax.swing.JFrame;
import javax.swing.JInternalFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import javax.swing.border.TitledBorder;

public class JDesktopPaneDemo1 {
    private static final String URL_ONE = "http://www.hdbackgroundspoint.com/wp-content/uploads/2013/12/16/345t34.jpeg";
    private static final String URL_TWO = "http://www.hdbackgroundspoint.com/wp-content/uploads/2013/09/hh.jpeg";
    private Image image;
    private JDesktopPane desktop;

    public JDesktopPaneDemo1() {
        desktop = createDesktopPane();
        JInternalFrame iFrame = createInternalFrame();
        desktop.add(iFrame);
        JPanel panel = new JPanel(new BorderLayout());
        panel.setBorder(new TitledBorder("DesktopPane"));
        panel.add(desktop);

        JFrame frame = new JFrame("Desktop Background");
        frame.setContentPane(panel);
        frame.setSize(500, 350);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
        iFrame.setVisible(true);
    }

    public JDesktopPane createDesktopPane() {

        JDesktopPane pane = new JDesktopPane() {
            private Image image;
            {
                try {
                    image = ImageIO.read(new URL(URL_ONE));
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }

            @Override
            protected void paintComponent(Graphics g) {
                super.paintComponent(g);
                g.drawImage(image, 0, 0, getWidth(), getHeight(), this);
            }
        };
        return pane;
    }

    private JInternalFrame createInternalFrame() {
        JInternalFrame frame = new JInternalFrame();
        frame.setSize(200, 200);
        return frame;
    }

    public static void main(String[] args) {
        for (UIManager.LookAndFeelInfo laf : UIManager
                .getInstalledLookAndFeels()) {
            if ("Nimbus".equals(laf.getName())) {
                try {
                    UIManager.setLookAndFeel(laf.getClassName());
                } catch (ClassNotFoundException | InstantiationException
                        | IllegalAccessException
                        | UnsupportedLookAndFeelException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
        }
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                new JDesktopPaneDemo1();
            }
        });
    }
}

【讨论】:

  • URL_TWO 是干什么用的?好像没用过。
  • @AndrewThompson image = ImageIO.read(new URL(URL_ONE)); 我做了几个不同的例子(different question),我想让自己更容易切换图像
猜你喜欢
  • 1970-01-01
  • 2014-10-28
  • 2011-11-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-12-18
相关资源
最近更新 更多