سؤال

I am trying to make a popup over my JFrame in Swing. I have made it so that the popup will be layered over the old JFrame and disable the old one by passing in the JFrame and then .disable(). However, i am also trying to make the frame behind darken to show that it is disabled.

I found this: stackoverflow - Change brightness of JFrame

But how do i use it to lower the brightness of the JFrame that i have as a parameter just before i disable it? Something like darken(frame) and it lowers it using the function darken(JFrame frame). Thanks!

هل كانت مفيدة؟

المحلول

In fact, I'm going to make my comment an answer:

  • To show a window over another window, and disable the lower window, make the upper window a modal JDialog, and pass the lower window in as its parent.
  • One way to dim a top-level window is to get its glass pane, set it visible, and draw a semi-opaque grey color over it.

Here's my test of concept code:

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Dialog.ModalityType;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import javax.swing.*;

public class DimView {
   protected static final Color GP_COLOR = new Color(0, 0, 0, 30);

   private static void createAndShowGui() {
      final JFrame frame = new JFrame("DimView");
      final JPanel glassPanel = new JPanel() {
         protected void paintComponent(Graphics g) {
            super.paintComponent(g);
            g.setColor(GP_COLOR);
            g.fillRect(0, 0, getWidth(), getHeight());
         };
      };
      glassPanel.setOpaque(false);
      frame.setGlassPane(glassPanel);
      JPanel mainPanel = new JPanel();
      mainPanel.setPreferredSize(new Dimension(400, 400));
      mainPanel.setBackground(Color.pink);
      mainPanel.add(new JButton(new AbstractAction("Push Me") {

         @Override
         public void actionPerformed(ActionEvent evt) {
            glassPanel.setVisible(true);

            JDialog dialog = new JDialog(frame, "Dialog",
                  ModalityType.APPLICATION_MODAL);
            dialog.add(Box.createRigidArea(new Dimension(200, 200)));
            dialog.pack();
            dialog.setLocationRelativeTo(frame);
            dialog.setVisible(true);

            glassPanel.setVisible(false);
         }
      }));

      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();
         }
      });
   }
}
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top