在 Java 中的內部窗格中顯示多個元件


首先,在 JDesktopPane 中設定一個內部窗格 -

JDesktopPane desktopPane = new JDesktopPane();
JInternalFrame intFrame = new JInternalFrame("Our Frame", true, true, true, true);
desktopPane.add(intFrame);

現在,設定內部窗格的邊界 -

intFrame.setBounds(50, 90, 200, 250);

建立兩個元件 -

JLabel label = new JLabel(intFrame.getTitle(), JLabel.CENTER);
JButton button = new JButton("Demo Button");

將兩個元件新增到內部窗格 -

intFrame.add(label, BorderLayout.CENTER);
intFrame.add(button, BorderLayout.WEST);

以下是在內部窗格中顯示多個元件的一個示例 -

package my;
import java.awt.BorderLayout;
import javax.swing.JButton;
import javax.swing.JDesktopPane;
import javax.swing.JFrame;
import javax.swing.JInternalFrame;
import javax.swing.JLabel;
public class SwingDemo {
   public static void main(final String[] args) {
      JFrame frame = new JFrame();
      frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      JDesktopPane desktopPane = new JDesktopPane();
      JInternalFrame intFrame = new JInternalFrame("Our Frame", true, true, true, true);
      desktopPane.add(intFrame);
      intFrame.setBounds(50, 90, 200, 250);
      JLabel label = new JLabel(intFrame.getTitle(), JLabel.CENTER);
      JButton button = new JButton("Demo Button");
      intFrame.add(label, BorderLayout.CENTER);
      intFrame.add(button, BorderLayout.WEST);
      intFrame.setVisible(true);
      frame.add(desktopPane, BorderLayout.CENTER);
      frame.setSize(600, 500);
      frame.setVisible(true);
   }
}

輸出

以下是在上面顯示的內部窗格上單擊最大化按鈕後的結果 -

更新於:30-Jul-2019

373 次瀏覽

開啟您的 職業生涯

完成課程並獲得認證

開始
廣告
© . All rights reserved.