是否可以將JDesktopPane
和JInternalFrame
添加到JPanel
如果是這樣,我該怎麼做。我嘗試添加它,但它無法工作。將JDesktopPane添加到JPanel
-2
A
回答
4
「是否有可能的JDesktopPane和JInternalFrame的添加到JPanel」
當然,你可以
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();
}
});
}
}
+0
什麼是URL_TWO?它似乎沒有被使用。 –
+0
@AndrewThompson'image = ImageIO.read(new URL(URL_ONE));'我正在製作一些不同的示例(對於[不同的問題](http://stackoverflow.com/a/22866831/2587435))和我想讓自己更容易切換圖像 –
相關問題
- 1. 將按鈕添加到JDesktopPane
- 2. 如何從ExplorerTopComponent將JDesktopPane添加到EditorTopComponent?
- 3. 將Jpanel添加到JFrame?
- 4. 將JPanel添加到JFrame中
- 5. 將JPanel添加到JList?
- 6. 將組件添加到JPanel
- 7. 將JFXPanel添加到JPanel
- 8. 將JPanel添加到Canvas
- 9. 將組件添加到JPanel
- 10. 將ButtonGroup添加到JPanel
- 11. 將按鈕添加到JPanel
- 12. 將JScrollPane添加到JPanel?
- 13. 將path2d添加到jpanel
- 14. 將JScrollPane添加到JPanel
- 15. 將JButton添加到JPanel
- 16. 將元素添加到JPanel
- 17. 將JLayeredPane添加到JPanel
- 18. 將jpanel添加到jframe
- 19. 將圖形添加到JPanel
- 20. 將JLabel添加到JPanel
- 21. 將JList添加到JPanel
- 22. 將JMenuBar添加到JPanel?
- 23. 將JPanel添加到JScrollPane
- 24. 將JMenu添加到JPanel
- 25. 將JApplet添加到JPanel
- 26. 將對象添加到Jpanel
- 27. 將actionlistener添加到jpanel
- 28. 將JList/JScrollPane添加到JPanel
- 29. 將JPanel添加到JFrame中
- 30. 將圖像添加到JPanel
正如在你最後陳述[關於這個問題的問題](http:// stackoverfl ow.com/questions/22874242/adding-jdesktoppane-to-jframe-using-gridbaglayout),除非你提供一個【示例演示】(https://stackoverflow.com/help/mcve)你的問題,有很少的人可以做。簡而言之,是的。 – MadProgrammer
儘管我同意MadProgrammer的說法,但還是猜測:你忘記調用'internalFrame.setVisible(true)'了嗎?無論如何,這應該是一個很好的起點:http://docs.oracle.com/javase/tutorial/uiswing/components/internalframe.html – Marco13