有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java如何使JDialog处于非活动状态

我想使基于JDialog的窗口处于非活动状态,以便禁用所有控件(灰色)。setEnabled(false)只会使您无法单击任何控件,甚至无法关闭窗口。但没有什么会变成灰色。请帮忙

编辑:下面是示例代码

import javax.swing.JButton;
import javax.swing.JDialog;


public class Analyzer extends JDialog{

public Analyzer() {
    JButton but = new JButton("test");
    setLayout(null);
    but.setBounds(10,10,100,100);

    add(but);
    setSize( 200, 200);
    setVisible(true);
    setEnabled(false);
}

public static void main(String[] args) {
    new Analyzer();
}

}

共 (2) 个答案

  1. # 1 楼答案

    实现这一点的典型方法是使用glassPane,但Java7引入了JLayer,它也应该做到这一点

  2. # 2 楼答案

    我知道有两种方法可以做到这一点,一种是递归禁用对话框的组件,另一种是禁用整个对话框(包括拖动对话框的功能):

    import java.awt.Component;
    import java.awt.Container;
    import java.awt.Dimension;
    import java.awt.FlowLayout;
    import java.awt.Point;
    import java.awt.event.ActionEvent;
    
    import javax.swing.*;
    
    @SuppressWarnings("serial")
    public class DisableEg extends JPanel {
       public static final String DISABLE_DIALOG_COMPONENTS = "Disable Dialog Components";
       public static final String ENABLE_DIALOG_COMPONENTS = "Enable Dialog Components";
       public static final String DISABLE_DIALOG = "Disable Dialog";
       public static final String ENABLE_DIALOG = "Enable Dialog";
       private static final int LOC_SHIFT = 150;
       private Analyzer analyzer;
    
       public DisableEg(JFrame frame) {
          analyzer = new Analyzer(frame);
          analyzer.pack();
          analyzer.setLocationRelativeTo(frame);
          Point location = analyzer.getLocation();
          location = new Point(location.x - LOC_SHIFT, location.y - LOC_SHIFT);
          analyzer.setLocation(location);
          analyzer.setVisible(true);
    
          add(new JButton(new AbstractAction(DISABLE_DIALOG_COMPONENTS) {
    
             @Override
             public void actionPerformed(ActionEvent evt) {
                AbstractButton btn = (AbstractButton) evt.getSource();
                if (btn.getText().equals(DISABLE_DIALOG_COMPONENTS)) {
                   btn.setText(ENABLE_DIALOG_COMPONENTS);
                   analyzer.setComponentEnabled(false);
                } else {
                   btn.setText(DISABLE_DIALOG_COMPONENTS);
                   analyzer.setComponentEnabled(true);
                }
             }
          }));
          add(new JButton(new AbstractAction(DISABLE_DIALOG) {
    
             @Override
             public void actionPerformed(ActionEvent evt) {
                AbstractButton btn = (AbstractButton) evt.getSource();
                if (btn.getText().equals(DISABLE_DIALOG)) {
                   btn.setText(ENABLE_DIALOG);
                   analyzer.setEnabled(false);
                } else {
                   btn.setText(DISABLE_DIALOG);
                   analyzer.setEnabled(true);
                }
             }
          }));
       }
    
       private static void createAndShowGui() {
          JFrame frame = new JFrame("Disable Example");
          DisableEg mainPanel = new DisableEg(frame);
    
          frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
          frame.getContentPane().add(mainPanel);
          frame.pack();
          frame.setLocationRelativeTo(null);
          frame.setVisible(true);
       }
    
       public static void main(String[] args) {
          SwingUtilities.invokeLater(new Runnable() {
             public void run() {
                createAndShowGui();
             }
          });
       }
    }
    
    @SuppressWarnings("serial")
    class Analyzer extends JDialog {
    
       public Analyzer(JFrame frame) {
          super(frame, "Analyzer Dialog", false);
          JButton but = new JButton("test");
          setLayout(new FlowLayout());
    
          add(but);
          setPreferredSize(new Dimension(200, 200));
       }
    
       public void setComponentEnabled(boolean enabled) {
          setComponentEnabled(enabled, getContentPane());
    
          // !! if you have menus to disable, you may need instead
          // setComponentEnabled(enabled, this); // !!
       }
    
       private void setComponentEnabled(boolean enabled, Component component) {
          component.setEnabled(enabled);
          if (component instanceof Container) {
             Component[] components = ((Container) component).getComponents();
             if (components != null && components.length > 0) {
                for (Component heldComponent : components) {
                   setComponentEnabled(enabled, heldComponent);
                }
             }
          }
       }
    
    }