为什么JFrame透明?

时间:2014-12-16 17:16:01

标签: java swing jframe transparent

我有一个正在表演的JFrame。如果我只是将JFrame设置为visible,那么整个JFrame会出现,但是如果我在将JFrame设置为visible后尝试执行任何操作,那么JFrame将会出现,但是透明,只有标题和关闭选项可见。这种情况最近才发生,我不知道发生了什么......

可见JFrame

GUI frame = new GUI(); //GUI is a class that extends JFrame
frame.setVisible(true);

透明JFrame

GUI frame = new GUI(); //GUI is a class that extends JFrame
frame.setVisible(true);
frame.setVisible(false); //If I throw a breakpoint here, as soon as it goes from .setVisible(true) to this line, the GUI appears, but is transparent

代码

public class GUI extends JFrame {
private JPanel contentPane;

public static void main(String[] args) {
    EventQueue.invokeLater(new Runnable() {
        public void run() {
            try {
                GUI frame = new GUI();
                frame.setVisible(true);
                frame.setVisible(false);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    });
}

public GUI() {
    setTitle("Title");
    setResizable(false);
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setBounds(100, 100, 330, 250);
    contentPane = new JPanel();
    contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
    contentPane.setLayout(new BorderLayout(0, 0));
    setContentPane(contentPane);
}

}

图片 enter image description here

1 个答案:

答案 0 :(得分:5)

问题可能在这里:

frame.setVisible(false); // If I throw a breakpoint here, as soon as it goes from 
                         // .setVisible(true) to this line, the GUI appears, 
                         // but is transparent

通过在运行Swing代码中设置断点,可以阻止Swing事件线程,该线程阻止GUI绘制或与用户交互。如果您需要在GUI运行时调试GUI,可以尝试不同的方法,包括使用记录器在程序运行时记录该程序的状态。


修改
您在评论中说明:

  

我希望用户在应用程序前进之前完成选择设置。所以我实际上并没有使用一段时间(真实),而是一段时间(可见)。 visible是一个布尔值,当用户点击gui上的按钮时,该布尔值设置为false,这将突破循环。

然后解决方案完全不同且非常简单:不要使用第二个JFrame来显示此设置显示窗口,而是要使用模态 JDialog或JOptionPane(其中实际上只是一个专门的模态JDialog)。

为什么这有助于Swing有一个特殊的模态对话机制,它在设置对话框可见后立即从调用窗口中冻结代码流。因此,调用代码将始终知道对话框何时不再可见,因为只有当对话框不可见时,代码的程序流才会恢复。因此,您需要在设置对话框或JOptionPane可见的行之后的行中提取对话框窗口的数据。在您放弃JOptionPanes过于简单化之前,要了解他们的第二个参数,即Object类型的参数,可以将任何Swing GUI组件作为参数,包括一个包含非常大且复杂的GUI的JPanel,使这些工具非常有用。 / p>


编辑2
您在评论中说明:

  

我正在使用WindowsBuilder来处理与GUI相关的事情,以便让我更轻松。不幸的是,它没有提供使用JOptionPane的选项。也没有JDialogue。我想留在WindowsBuilder能够修改的路线上。

我不知道在我理解底层库之前避免使用代码生成工具的重要性,为什么它很重要...但我建议你检查一下是否WindowBuilder工具为您提供了创建扩展JPanel的类的选项。如果是这样,那么这样做,创建你的JPanel,然后在调用代码中,只需将你的JPanel填充到JOptionPane或模态JDialog中。


编辑3

例如,假设您有一个名为GetInfoPanel的JPanel,允许用户输入一些文本并选择一个JRadioButton:

class GetInfoPanel extends JPanel {
   public static final String[] COLOR_STRINGS = {"Red", "Green", "Blue", "Orange"};
   private JTextField infoField = new JTextField(10);
   private ButtonGroup buttonGroup = new ButtonGroup();

   public GetInfoPanel() {
      JPanel topPanel = new JPanel();
      topPanel.add(new JLabel("Information that you want to submit:"));
      topPanel.add(infoField);

      JPanel colorPanel = new JPanel(new GridLayout(1, 0, 5, 0));
      for (String colorString : COLOR_STRINGS) {
         JRadioButton radioButton = new JRadioButton(colorString);
         radioButton.setActionCommand(colorString);
         buttonGroup.add(radioButton);
         colorPanel.add(radioButton);
      }


      setLayout(new BoxLayout(this, BoxLayout.PAGE_AXIS));
      add(topPanel);
      add(colorPanel);
   }

   public String getInfo() {
      return infoField.getText();
   }

   public String getColorString() {
      String selection = "";
      ButtonModel model = buttonGroup.getSelection();
      if (model != null) {
         selection = model.getActionCommand();
      }

      return selection;
   }
}

然后一个调用类可以创建上面的一个实例和包,并在JOptionPane中显示它,然后提取它包含的信息:

  public void actionPerformed(ActionEvent e) {
     // create our GetInfoPanel
     GetInfoPanel getInfoPanel = new GetInfoPanel();

     // display it in a JOptionPane which is a modal JDialog
     JOptionPane.showMessageDialog(TestGetInfo.this, getInfoPanel,
           "Enter Information Please", JOptionPane.PLAIN_MESSAGE);

     // this code will not be called until the dialog above is no longer visible
     // extract the text from the getInfoPanel's text field 
     String info = getInfoPanel.getInfo();
     infoDisplayField.setText(info);

     // extract the the radiobutton selection from the getInfoPanel
     String colorString = getInfoPanel.getColorString();
     colorStringField.setText(colorString);
  }

你可以在这个程序中自己测试一下:

import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import javax.swing.*;

@SuppressWarnings("serial")
public class TestGetInfo extends JPanel {
   private JTextField infoDisplayField = new JTextField(10);
   private JTextField colorStringField = new JTextField(10);
   private JButton displayGetInfoBtn = new JButton(new DisplayGetInfoAction(
         "Get Info"));

   public TestGetInfo() {
      infoDisplayField.setFocusable(false);
      colorStringField.setFocusable(false);

      add(new JLabel("Info:"));
      add(infoDisplayField);
      add(Box.createHorizontalStrut(10));
      add(new JLabel("Color:"));
      add(colorStringField);
      add(Box.createHorizontalStrut(10));
      add(displayGetInfoBtn);
   }

   private class DisplayGetInfoAction extends AbstractAction {
      public DisplayGetInfoAction(String name) {
         // this will be the button's text:
         super(name);
      }

      @Override
      public void actionPerformed(ActionEvent e) {
         // create our GetInfoPanel
         GetInfoPanel getInfoPanel = new GetInfoPanel();

         // display it in a JOptionPane which is a modal JDialog
         JOptionPane.showMessageDialog(TestGetInfo.this, getInfoPanel,
               "Enter Information Please", JOptionPane.PLAIN_MESSAGE);

         // this code will not be called until the dialog above is no longer visible
         // extract the text from the getInfoPanel's text field 
         String info = getInfoPanel.getInfo();
         infoDisplayField.setText(info);

         // extract the the radiobutton selection from the getInfoPanel
         String colorString = getInfoPanel.getColorString();
         colorStringField.setText(colorString);
      }
   }

   private static void createAndShowGui() {
      TestGetInfo mainPanel = new TestGetInfo();

      JFrame frame = new JFrame("TestGetInfo");
      frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
      frame.getContentPane().add(mainPanel);
      frame.pack();
      frame.setLocationByPlatform(true);
      frame.setVisible(true);
   }

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

@SuppressWarnings("serial")
class GetInfoPanel extends JPanel {
   public static final String[] COLOR_STRINGS = {"Red", "Green", "Blue", "Orange"};
   private JTextField infoField = new JTextField(10);
   private ButtonGroup buttonGroup = new ButtonGroup();

   public GetInfoPanel() {
      JPanel topPanel = new JPanel();
      topPanel.add(new JLabel("Information that you want to submit:"));
      topPanel.add(infoField);

      JPanel colorPanel = new JPanel(new GridLayout(1, 0, 5, 0));
      for (String colorString : COLOR_STRINGS) {
         JRadioButton radioButton = new JRadioButton(colorString);
         radioButton.setActionCommand(colorString);
         buttonGroup.add(radioButton);
         colorPanel.add(radioButton);
      }


      setLayout(new BoxLayout(this, BoxLayout.PAGE_AXIS));
      add(topPanel);
      add(colorPanel);
   }

   public String getInfo() {
      return infoField.getText();
   }

   public String getColorString() {
      String selection = "";
      ButtonModel model = buttonGroup.getSelection();
      if (model != null) {
         selection = model.getActionCommand();
      }

      return selection;
   }
}