Java 在JPanel中显示JDesktopPane
我很难将JDesktopPane(包含JInternalFrame)添加到JPanel中。正确的方法是什么?我做错了什么 以下是我的简单示例:Java 在JPanel中显示JDesktopPane,java,swing,jpanel,jdesktoppane,Java,Swing,Jpanel,Jdesktoppane,我很难将JDesktopPane(包含JInternalFrame)添加到JPanel中。正确的方法是什么?我做错了什么 以下是我的简单示例: import javax.swing.*; import java.awt.*; public class MainPanel extends JPanel { JDesktopPane jDesktopPane = new JDesktopPane(); JInternalFrame jInternalFrame = new JIn
import javax.swing.*;
import java.awt.*;
public class MainPanel extends JPanel {
JDesktopPane jDesktopPane = new JDesktopPane();
JInternalFrame jInternalFrame = new JInternalFrame();
public MainPanel() {
jDesktopPane.add(jInternalFrame);
add(jDesktopPane);
setSize(400,400);
setVisible(true);
}
private static void createAndShowGui() {
JFrame frame = new JFrame("This isn't working...");
MainPanel mainPanel = new MainPanel();
frame.setLayout(new BorderLayout());
frame.add(mainPanel, BorderLayout.CENTER);
frame.setContentPane(mainPanel);
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
frame.setLocationByPlatform(false);
frame.setSize(500, 500);
frame.setVisible(true);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGui();
}
});
}
}
不使用布局管理器,因此它的默认/首选大小为JDesktop
0x0
默认情况下使用JPanel
,在布局子组件时,它会尊重子组件的FlowLayout
preferredSize
BorderLayout
public MainPanel() {
setLayout(new BorderLayout());
jDesktopPane.add(jInternalFrame);
add(jDesktopPane);
// pointless
//setSize(400,400);
// pointless
//setVisible(true);
}
现在,你应该提供你自己的,因为实际上没有任何东西定义一个首选的尺寸
public Dimension getPreferredSize() {
return new Dimension(400, 400);
}
然后,当您创建UI时,您可以简单地打包框架
private static void createAndShowGui() {
JFrame frame = new JFrame("This should be working now...");
MainPanel mainPanel = new MainPanel();
frame.setLayout(new BorderLayout());
// pointless considering the setContentPane call
//frame.add(mainPanel, BorderLayout.CENTER);
frame.setContentPane(mainPanel);
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
frame.pack();
frame.setLocationByPlatform(false);
//frame.setSize(500, 500);
frame.setVisible(true);
}
现在,由于JDesktopPane
不使用任何布局管理器,因此您需要负责确保添加到其中的任何内容都已定位和大小
jInternalFrame.setBounds(10, 10, 200, 200);
// Just like any frame, it's not visible when it's first created
jInternalFrame.setVisible(true);
不使用布局管理器,因此它的默认/首选大小为JDesktop
0x0
默认情况下使用JPanel
,在布局子组件时,它会尊重子组件的FlowLayout
preferredSize
BorderLayout
public MainPanel() {
setLayout(new BorderLayout());
jDesktopPane.add(jInternalFrame);
add(jDesktopPane);
// pointless
//setSize(400,400);
// pointless
//setVisible(true);
}
现在,你应该提供你自己的,因为实际上没有任何东西定义一个首选的尺寸
public Dimension getPreferredSize() {
return new Dimension(400, 400);
}
然后,当您创建UI时,您可以简单地打包框架
private static void createAndShowGui() {
JFrame frame = new JFrame("This should be working now...");
MainPanel mainPanel = new MainPanel();
frame.setLayout(new BorderLayout());
// pointless considering the setContentPane call
//frame.add(mainPanel, BorderLayout.CENTER);
frame.setContentPane(mainPanel);
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
frame.pack();
frame.setLocationByPlatform(false);
//frame.setSize(500, 500);
frame.setVisible(true);
}
现在,由于JDesktopPane
不使用任何布局管理器,因此您需要负责确保添加到其中的任何内容都已定位和大小
jInternalFrame.setBounds(10, 10, 200, 200);
// Just like any frame, it's not visible when it's first created
jInternalFrame.setVisible(true);
不使用布局管理器,因此它的默认/首选大小为JDesktop
0x0
默认情况下使用JPanel
,在布局子组件时,它会尊重子组件的FlowLayout
preferredSize
BorderLayout
public MainPanel() {
setLayout(new BorderLayout());
jDesktopPane.add(jInternalFrame);
add(jDesktopPane);
// pointless
//setSize(400,400);
// pointless
//setVisible(true);
}
现在,你应该提供你自己的,因为实际上没有任何东西定义一个首选的尺寸
public Dimension getPreferredSize() {
return new Dimension(400, 400);
}
然后,当您创建UI时,您可以简单地打包框架
private static void createAndShowGui() {
JFrame frame = new JFrame("This should be working now...");
MainPanel mainPanel = new MainPanel();
frame.setLayout(new BorderLayout());
// pointless considering the setContentPane call
//frame.add(mainPanel, BorderLayout.CENTER);
frame.setContentPane(mainPanel);
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
frame.pack();
frame.setLocationByPlatform(false);
//frame.setSize(500, 500);
frame.setVisible(true);
}
现在,由于JDesktopPane
不使用任何布局管理器,因此您需要负责确保添加到其中的任何内容都已定位和大小
jInternalFrame.setBounds(10, 10, 200, 200);
// Just like any frame, it's not visible when it's first created
jInternalFrame.setVisible(true);
不使用布局管理器,因此它的默认/首选大小为JDesktop
0x0
默认情况下使用JPanel
,在布局子组件时,它会尊重子组件的FlowLayout
preferredSize
BorderLayout
public MainPanel() {
setLayout(new BorderLayout());
jDesktopPane.add(jInternalFrame);
add(jDesktopPane);
// pointless
//setSize(400,400);
// pointless
//setVisible(true);
}
现在,你应该提供你自己的,因为实际上没有任何东西定义一个首选的尺寸
public Dimension getPreferredSize() {
return new Dimension(400, 400);
}
然后,当您创建UI时,您可以简单地打包框架
private static void createAndShowGui() {
JFrame frame = new JFrame("This should be working now...");
MainPanel mainPanel = new MainPanel();
frame.setLayout(new BorderLayout());
// pointless considering the setContentPane call
//frame.add(mainPanel, BorderLayout.CENTER);
frame.setContentPane(mainPanel);
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
frame.pack();
frame.setLocationByPlatform(false);
//frame.setSize(500, 500);
frame.setVisible(true);
}
现在,由于JDesktopPane
不使用任何布局管理器,因此您需要负责确保添加到其中的任何内容都已定位和大小
jInternalFrame.setBounds(10, 10, 200, 200);
// Just like any frame, it's not visible when it's first created
jInternalFrame.setVisible(true);
做了一个很好的改变;)很高兴它能帮你做出一个很好的改变;)很高兴它能帮你做出一个很好的改变;)很高兴它能帮你做出一个很好的改变;)很高兴它能帮上忙你为什么不读一下Swing教程???我过去给你的Swing教程链接是有原因的!!!对于工作示例来说,它总是一个很好的起点。为什么要先创建JPanel?为什么不将桌面窗格添加到框架中?@camickr我需要将其添加到面板中,以便将面板添加到使用changeListener创建的GUI中。同样,为什么您认为需要先将其添加到面板中?桌面窗格是一个组件,就像JPanel一样。它可以添加到任何可以添加面板的位置。在任何情况下,您为什么不阅读Swing教程???它向您展示了如何将内部框架添加到桌面。为什么您的示例看起来不像教程代码?当您从教程中看到工作代码时,为什么希望有人填写详细信息。如果你不理解教程中的某些内容,请提出具体问题。顺便说一下,框架的内容窗格是一个面板。为什么不阅读Swing教程???我过去给你的Swing教程链接是有原因的!!!对于工作示例来说,它总是一个很好的起点。为什么要先创建JPanel?为什么不将桌面窗格添加到框架中?@camickr我需要将其添加到面板中,以便将面板添加到使用changeListener创建的GUI中。同样,为什么您认为需要先将其添加到面板中?桌面窗格是一个组件,就像JPanel一样。它可以添加到任何可以添加面板的位置。在任何情况下,您为什么不阅读Swing教程???它向您展示了如何将内部框架添加到桌面。为什么您的示例看起来不像教程代码?当您从教程中看到工作代码时,为什么希望有人填写详细信息。如果你不理解教程中的某些内容,请提出具体问题。顺便说一下,框架的内容窗格是一个面板。为什么不阅读Swing教程???我过去给你的Swing教程链接是有原因的!!!对于工作示例来说,它总是一个很好的起点。为什么要先创建JPanel?为什么不将桌面窗格添加到框架中?@camickr我需要将其添加到面板中,以便将面板添加到使用changeListener创建的GUI中。同样,为什么您认为需要先将其添加到面板中?桌面窗格是一个组件,就像JPanel一样。它可以添加到任何可以添加面板的位置。在任何情况下,您为什么不阅读Swing教程???它向您展示了如何将内部框架添加到桌面。为什么您的示例看起来不像教程a