为什么Mainframe JButton出现在JInternalFrame的前景?

时间:2012-10-12 11:10:39

标签: java swing jinternalframe jdesktoppane

我正在创建一个Swing应用程序。我有一个主要JFrame和一个JDesktopPane。我在主框架上添加了一个按钮和一个标签。但是如果我在主框架按钮上打开任何JInternalFrame并且标签覆盖内部框架。

JButton和JLabel显示为JInternalFrame的前景。如果我单击内部框架按钮,请转到后台。

你能帮忙解决这个问题吗?

import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.Frame;
import java.awt.Point;
import java.awt.Rectangle;
import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JDesktopPane;
import javax.swing.JFrame;
import javax.swing.JInternalFrame;
import javax.swing.JLabel;
import javax.swing.SwingConstants;
import javax.swing.UIManager;

public class MainFrame {

   JFrame frame1 ;
   JDesktopPane desktop ;

       public MainFrame () {
         frame1 = new JFrame("EMPLOYEE LEAVE TRACKER");
         frame1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
         frame1.setExtendedState(Frame.MAXIMIZED_BOTH);
         frame1.repaint();
         desktop = new JDesktopPane();        //Creates a new JDesktopPane.
         frame1.setContentPane(desktop);
         frame1.setSize(900,700);
         frame1.setVisible(true);
         desktop.setBackground(Color.DARK_GRAY );

    //Creates a JLabel on JDesktopPane.
    JLabel label1 = new JLabel("EMPLOYEE LEAVE TRACKER", SwingConstants.CENTER);
       label1.setFont(new Font("SansSerif",Font.ITALIC + Font.BOLD,54));
       label1.setBounds(new Rectangle(new Point(275, 100),label1.getPreferredSize()));
    //Creates a JButon on JDesktopPane.


   JButton Leave = new JButton("Leave Management");
     Leave.setHorizontalTextPosition(JButton.CENTER);
     Leave.setBounds(new Rectangle(new Point(700,200),Leave.getPreferredSize()));
     Leave.setSize(300, 300);
     Leave.addActionListener(new java.awt.event.ActionListener()      {
        @Override
        public void actionPerformed(java.awt.event.ActionEvent e)   {
        frame1.add(LeaveManagment()); 
        }
    });

  //Look and Feel

     try {
        UIManager.setLookAndFeel("javax.swing.plaf.nimbus.NimbusLookAndFeel");
        } catch(Exception e) {
          System.out.println("Error setting native LAF: " + e);
        }

    desktop.add(Leave);
    desktop.add(label1);

     }
   //Creating JInternalFrame

    public JInternalFrame LeaveManagment(){              
    final JInternalFrame employeeFrame = new JInternalFrame("LEAVE M" +
            "ANAGEMNT", true, true, true, true);
    employeeFrame.getContentPane().setBackground(Color.white);
    employeeFrame.setSize(900,700);
    employeeFrame.setVisible(true);
    employeeFrame.setMaximizable(true);
    employeeFrame.setResizable(true);
    JComponent c = (JComponent)
    employeeFrame.getContentPane();
    c.setLayout(new FlowLayout());

        return employeeFrame;
}

    public static void main(String[] args) {
    javax.swing.SwingUtilities.invokeLater(new Runnable() {
      public void run() {
      new MainFrame ();
      }
});
}}

2 个答案:

答案 0 :(得分:3)

从声音中,您已将按钮添加到JDesktopPane

桌面窗格是JLayeredPane的一种类型,这使您可以在我们的图层上放置组件。

虽然这是一个简洁的功能,恕我直言,只有JInternalFrames和偶尔的特殊“窗口”应出现在桌面上(虽然桌面管理器确实使用一种按钮来表示最小化的窗口)

就个人而言,我也将按钮放在桌面上,而不是桌面上。

查看How to Use Internal FramesHow to use Layered Pane

答案 1 :(得分:0)

MadProgrammer说得对。一种解决方案是将组件添加到适当的JLayeredPane层。即,

// Note name change of JButton to adhere to Java naming standards:
desktop.add(leaveManagementBtn, JLayeredPane.DEFAULT_LAYER);
desktop.add(label1, JLayeredPane.DEFAULT_LAYER); // note layer component being added to

public void actionPerformed(java.awt.event.ActionEvent e) {
  // again method name capitalization changed to adhere to standard
  // again, component added to appropriate layer
  frame1.add(leaveManagment(), JLayeredPane.PALETTE_LAYER);
}

再次,MadProgrammer是对的,按钮不应该在JDesktopFrame上,而是在桌面上的按钮栏上。我建议你接受他的回答。