gpt4 book ai didi

java - 为什么我的简单 JFrame 显示很奇怪?

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

我是 Java Swing/AWT 的新手,我有以下代码用于一个简单的 PopUp 对话框,该对话框在单击任何 JButton 时关闭,但显示非常不稳定。有人对修复什么以及如何修复有建议吗?

import java.awt.Color;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextArea;
import javax.swing.Box.Filler;


public class UpgradePopupWindow extends JPanel implements ActionListener {
//public static UpgradePopupWindow mainWindow;

static final long serialVersionUID = 0;


final String upgrade = "Continue Upgrade";
final String restore = "Restore";

JPanel panels;
JButton flashMe;
JButton helpMe;
JTextArea Message;
JFrame newFrame;
FlasherThread flash;


protected JTextArea addText(String text, boolean visible, int fontStyle) {

JTextArea textArea = new JTextArea(text);

textArea.setFont(new Font("SansSerif", fontStyle, 12)); //$NON-NLS-1$

textArea.setLineWrap(true);
textArea.setWrapStyleWord(true);
textArea.setEditable(false);
textArea.setBackground(Color.DARK_GRAY);
textArea.setForeground(Color.WHITE);
textArea.setOpaque(false);
textArea.setVisible(visible);
textArea.setAlignmentX(Component.CENTER_ALIGNMENT);

add(textArea);

return textArea;
}

private UpgradePopupWindow(JFrame frame, Object ft) {

flash = (FlasherThread)ft;
String text = "An error occurred during the attempt to update your software. We recommend the following: (1) Restore your device to its previous version, (2) back up important data, and then (3) try updating your device again. If you continue with the current update, only your previously backed-up data will be available.";
addFiller(5);
addLabel(text, Font.PLAIN, 12);
//addText(text, true, Font.PLAIN);
addFiller(20);
newFrame = frame;
flashMe = new JButton(upgrade);

flashMe.setActionCommand("upgrade");
flashMe.addActionListener(this);
flashMe.setEnabled(true);
add(flashMe);


helpMe = new JButton(restore);
helpMe.setActionCommand("restore");
helpMe.addActionListener(this);
helpMe.setEnabled(true);
add(helpMe);
setOpaque(true);
newFrame.setContentPane(this);
}

protected JLabel addLabel(String text, int fontStyle, int size) {
JLabel label = new JLabel(text);
label.setFont(new Font("SansSerif", fontStyle, size));
label.setAlignmentX(Component.CENTER_ALIGNMENT);
label.setOpaque(false);
label.setVisible(true);
//label.setForeground(Color.BLUE);

add(label);
return label;
}

protected void addFiller(int size) {
/*
* create some space before the progress bar
*/
Dimension diminsion = new Dimension(size, size);
Filler filler = new Filler(diminsion, diminsion, diminsion);
filler.setAlignmentX(Component.CENTER_ALIGNMENT);

add(filler);
}

public static void createGUI(Object obj) {
//Create and set up the frame.
JFrame frame = new JFrame("PopUp Dialog");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setPreferredSize(new Dimension(400, 200));
//create and setup the content pane
UpgradePopupWindow popUpContentPane = new UpgradePopupWindow(frame, obj);

popUpContentPane.setOpaque(true);
frame.setContentPane(popUpContentPane);

frame.pack();
frame.setVisible(true);

}


public void actionPerformed(ActionEvent e) {

if("restore".equals(e.getActionCommand())) {
System.out.println("restore button selected");
flash.setUpgradeRestoreChoice("restore");
newFrame.dispose();
} else if ("upgrade".equals(e.getActionCommand())) {
System.out.println("upgrade button selected");
flash.setUpgradeRestoreChoice("upgrade");
newFrame.dispose();
}
}

}alt text

alt text

最佳答案

  1. 您应该使用比默认布局管理器更好的布局管理器
  2. 您应该使用JOptionPane 而不是制作您自己的选项对话框

关于java - 为什么我的简单 JFrame 显示很奇怪?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3121504/

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