我正在创建一个 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 ();
}
});
}}