gpt4 book ai didi

java - 为什么 JFrame 是透明的?

转载 作者:行者123 更新时间:2023-11-29 07:43:57 28 4
gpt4 key购买 nike

我有一个正在运行的 JFrame。如果我只是将 JFrame 设置为可见,则会显示整个 JFrame,但如果我在将 JFrame 设置为可见后尝试执行任何操作,则 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

最佳答案

问题可能出在这里:

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 运行时调试它,则可以尝试不同的方法,包括使用记录器记录程序运行时的状态。


编辑
您在评论中声明:

I want the user to finish selecting their settings before the applications proceeds forward. So I am not actually using a while(true), but a while(visible). visible is a boolean that is set to false when the user clicks on a button on the gui, which will break out of the loop.

那么解决方案就完全不同而且非常简单:不要使用第二个 JFrame 来显示此设置显示窗口,而是您需要使用 modal JDialog 或 JOptionPane(这实际上是只不过是一个专门的模态 JDialog。

之所以有帮助,是因为 Swing 有一种用于模态对话框的特殊机制,它在将对话框设置为可见后立即卡住调用窗口中的代码流。因此,调用代码将始终知道对话框何时不再可见,因为其代码的程序流仅在对话框不可见时才会恢复。因此,您可能希望在设置对话框或 JOptionPane 可见的行之后立即提取对话窗口的数据。在您因为过于简单而放弃 JOptionPanes 之前,请了解它们的第二个参数,即 Object 类型的参数可以将任何 Swing GUI 组件作为参数,包括一个拥有非常大且复杂的 GUI 的 JPanel,这使得这些工具非常有用。


编辑2
您在评论中声明:

I am using WindowsBuilder for my GUI related things to make things easier for me. Unfortunately, it doesn't offer the option to use JOptionPane. There are no JDialogue, too. I want to stay on the route that WindowsBuilder will be able to modify.

在您了解底层库之前,我不会大谈特谈为什么避免使用代码生成工具很重要,...但我确实建议您检查 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;
}
}

关于java - 为什么 JFrame 是透明的?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27510216/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com