0

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

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 ();
      }
});
}}
4

2 回答 2

3

从它的声音来看,您已将按钮添加到JDesktopPane.

桌面窗格是一种类型JLayeredPane,它允许您将组件放置在图层上。

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

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

看看如何使用内部框架如何使用分层窗格

于 2012-10-12T11:51:00.187 回答
0

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

// 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 上,而应位于桌面之外的按钮栏上。我建议你接受他的回答。

于 2012-10-16T12:18:23.163 回答