- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有一个奇怪的问题,我不知道是什么原因造成的。我有一个对话框类,它只是在屏幕上显示错误/警告消息。有趣的是,当我设置窗口中心时,组件不会出现(除非我重新调整它的大小)。
但是,当我更改屏幕位置时,组件会很好地显示在 JPanel 上。
我基本上将这一行更改为int x = (dim.width - w)/3;
并且它有效。但当宽度除以 2 时就不会了。因此,对话框可以在除中心之外的任何地方工作。请让我知道我在这里缺少什么。
非常感谢任何帮助!
谢谢!
代码如下:
<小时/>MCVE:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
@SuppressWarnings("serial")
public class UKDialog extends JDialog {
private JButton button1;
private JLabel message;
public UKDialog(String message, String buttonText) {
this.button1 = new JButton(buttonText);
this.message = new JLabel(message);
setWarning();
}
public void setWarning() {
this.setModal(true);
this.setAlwaysOnTop(true);
if (this.message.getText().length() <= 30) {
this.setMaximumSize(new Dimension(650, 300));
this.setMinimumSize(new Dimension(650, 300));
this.setPreferredSize(new Dimension(650, 300));
} else if (this.message.getText().length() > 30) {
this.setMaximumSize(new Dimension(870, 300));
this.setMinimumSize(new Dimension(870, 300));
this.setPreferredSize(new Dimension(870, 300));
}
this.setLayout(new BorderLayout());
JPanel messagePanel = new JPanel();
messagePanel.setBorder(BorderFactory.createEtchedBorder());
JPanel buttonPanel = new JPanel(new FlowLayout(FlowLayout.CENTER));
buttonPanel.setBorder(BorderFactory.createEtchedBorder());
buttonPanel.setPreferredSize(new Dimension(600, 100));
this.message.setFont(new Font("Calibri", Font.BOLD, 40));
this.button1.setPreferredSize(new Dimension(230, 80));
this.button1.setFont(new Font("Calibri", Font.BOLD, 45));
this.button1.setBackground(new java.awt.Color(139, 71, 137));
this.button1.setForeground(java.awt.Color.white);
this.button1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
okActionPerformed(evt);
}
});
messagePanel.add(this.message, BorderLayout.CENTER);
buttonPanel.add(this.button1);
this.add(messagePanel, BorderLayout.CENTER);
this.add(buttonPanel, BorderLayout.SOUTH);
// calculate the new location of the window
Dimension dim = Toolkit.getDefaultToolkit().getScreenSize();
int w = this.getSize().width;
int h = this.getSize().height;
int x = (dim.width - w) / 2;
int y = (dim.height - h) / 5;
this.setLocation(x, y);
this.setVisible(true);
}
protected void okActionPerformed(ActionEvent evt) {
UKDialog.this.dispose();
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
final JFrame frame = new JFrame("Main GUI");
JButton button = new JButton(new AbstractAction("Push Me") {
@Override
public void actionPerformed(ActionEvent e) {
String message = "Message is Long Enough???";
String buttonText = "Button";
UKDialog ukDialog = new UKDialog(message, buttonText);
}
});
JPanel panel = new JPanel();
panel.setPreferredSize(new Dimension(500, 400));
panel.add(button);
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
frame.add(panel);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}
}
这是我的类使用 main 方法调用警告类。
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import javax.imageio.ImageIO;
import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextField;
import javax.swing.ScrollPaneLayout;
import javax.swing.WindowConstants;
public class PAndDeliveryNotification extends JDialog {
private JButton button1;
private JLabel messageTitle;
private JTextField text;
private boolean reply;
private String typedText;
private ArrayList<String[]> remainingInvoices;
private String stopNum = null;
private String currentStopNum, lastStopNum;
public PAndDeliveryNotification(ArrayList<String[]> rInvoices, String buttonText1) {
this.button1 = new JButton(buttonText1);
this.remainingInvoices = rInvoices;
}
public void setDelivStopNo() throws IOException {
BufferedImage image = null;
try {
image = ImageIO.read(new File("images/uk.png"));
} catch (IOException e) {
}
this.setIconImage(image);
//frame properties
this.setModal(true);
this.setAlwaysOnTop(true);
this.setDefaultCloseOperation(WindowConstants.DO_NOTHING_ON_CLOSE);
this.setLayout(new BorderLayout());
//frame size
this.setPreferredSize(new Dimension(900, 600));
this.setMaximumSize(new Dimension(900, 600));
this.setMinimumSize(new Dimension(900, 600));
JPanel upperPanel = new JPanel(new WrapLayout());
upperPanel.setBorder(BorderFactory.createEtchedBorder());
for (int i = 0; i < this.remainingInvoices.size(); i++) {
String[] invoice;
invoice = (String[]) this.remainingInvoices.get(i);
if (i == 0) {
this.currentStopNum = invoice[0];
}
if (i == this.remainingInvoices.size() - 1) {
this.lastStopNum = invoice[0];
}
StopItem item = new StopItem(invoice);
item.setPreferredSize(new Dimension(770, 120));
upperPanel.add(item);
}
upperPanel.repaint();
upperPanel.revalidate();
JScrollPane pane = new JScrollPane(upperPanel);
pane.setPreferredSize(new Dimension(770, 600));
pane.getVerticalScrollBar().setPreferredSize(new Dimension(50, 0));
pane.setLayout(new ScrollPaneLayout());
this.add(pane, BorderLayout.CENTER);
JPanel lowerPanel = new JPanel(new BorderLayout());
lowerPanel.setBorder(BorderFactory.createEtchedBorder());
lowerPanel.setPreferredSize(new Dimension(600, 100));
JPanel messagePanel = new JPanel(new FlowLayout());
messagePanel.setPreferredSize(new Dimension(600, 80));
JPanel buttonPanel = new JPanel();
buttonPanel.setPreferredSize(new Dimension(240, 80));
this.messageTitle = new JLabel("Enter Delivery Stop Number:");
this.messageTitle.setFont(new Font("Calibri", Font.BOLD, 40));
this.messageTitle.setPreferredSize(new Dimension(480, 80));
this.text = new JTextField(3);
this.text.setPreferredSize(new Dimension(100, 80));
this.text.setFont(new Font("Calibri", Font.BOLD, 40));
this.text.setHorizontalAlignment(JTextField.CENTER);
this.text.requestFocus();
Runtime.getRuntime().exec("cmd /c tabtip.exe");
this.button1.setPreferredSize(new Dimension(230, 80));
this.button1.setFont(new Font("Calibri", Font.BOLD, 45));
this.button1.setBackground(new java.awt.Color(139, 71, 137));
this.button1.setForeground(java.awt.Color.white);
this.button1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
saveStopNumActionPerformed(evt);
}
});
messagePanel.add(this.messageTitle);
messagePanel.add(this.text);
buttonPanel.add(this.button1);
lowerPanel.add(messagePanel, BorderLayout.WEST);
lowerPanel.add(buttonPanel, BorderLayout.EAST);
this.add(pane, BorderLayout.CENTER);
this.add(lowerPanel, BorderLayout.SOUTH);
Dimension dim = Toolkit.getDefaultToolkit().getScreenSize();
// calculate the new location of the window
int w = this.getSize().width;
int h = this.getSize().height;
int x = (dim.width - w) / 2;
int y = 0;
this.setLocation(x, y);
this.setVisible(true);
}
private void saveStopNumActionPerformed(ActionEvent evt) {
this.stopNum = this.text.getText();
try {
if (stopNum == null) {
new UKDialog("Fields cannot be empty!", "OK");
return;
} else if (Integer.parseInt(stopNum) < Integer.parseInt(this.currentStopNum)) {
new UKDialog("Delivery cannot be before pick up!", "OK");
return;
} else if (Integer.parseInt(stopNum) > Integer.parseInt(this.lastStopNum) + 1) {
new UKDialog("Enter stop number between " + this.currentStopNum + 1 + " and "
+ this.lastStopNum, "OK");
return;
}
} catch (NumberFormatException e) {
new UKDialog("Please enter numeric values!", "OK");
return;
}
System.out.println(this.stopNum);
this.setVisible(false);
this.dispose();
}
public String getStopNum() {
return this.stopNum;
}
private class StopItem extends JPanel {
private JPanel numPanel = new JPanel();
private JLabel itemNumber = new JLabel();
private JPanel leftPanel = new JPanel();
private JLabel invNoLbl = new JLabel();
private JLabel consigneeLbl = new JLabel();
private JLabel locationLbl = new JLabel();
private final String pUStopNo;
private final String invoiceNo;
private final String consignee;
private final String location;
public StopItem(String[] invoice) {
this.pUStopNo = invoice[0];
this.invoiceNo = invoice[1];
this.consignee = invoice[2];
this.location = invoice[3];
//Left and numberpanels
this.numPanel.setLayout(new BorderLayout());
this.numPanel.setPreferredSize(new Dimension(80, 80));
this.leftPanel.setLayout(new BorderLayout());
this.leftPanel.setPreferredSize(new Dimension(200, 90));
//number panel
this.itemNumber.setFont(new java.awt.Font("Lucida Grande", Font.BOLD, 45));
this.itemNumber.setText(String.valueOf(" " + pUStopNo));
//this.numPanel.setBorder(BorderFactory.createEtchedBorder());
this.numPanel.add(this.itemNumber);
//Main panel
this.setLayout(new BorderLayout());
this.setBorder(BorderFactory.createLineBorder(Color.black, 2));
this.invNoLbl.setText("Invoice #: " + this.invoiceNo);
this.invNoLbl.setFont(new java.awt.Font("Calibri", Font.PLAIN, 35));
this.consigneeLbl.setText("Consignee: " + this.consignee);
this.consigneeLbl.setFont(new java.awt.Font("Calibri", Font.PLAIN, 35));
this.locationLbl.setText("Location: " + this.location);
this.locationLbl.setFont(new java.awt.Font("Calibri", Font.PLAIN, 35));
this.leftPanel.add(this.invNoLbl, BorderLayout.NORTH);
this.leftPanel.add(this.consigneeLbl, BorderLayout.CENTER);
this.leftPanel.add(this.locationLbl, BorderLayout.SOUTH);
// add all the components to the frame
this.add(this.numPanel, BorderLayout.WEST);
this.add(this.leftPanel, BorderLayout.CENTER);
}
}
public static void main(String[] args) throws IOException {
String[] array1 = {"6", "444555", "Ali Murtaza Sumer", "Carson, Ca 90746"};
String[] array2 = {"7", "444666", "Hasan Huseyin Karabiber", "Los Angeles, Ca 90746"};
String[] array3 = {"8", "444777", "Mustafa Tinaztepe", "Macomb, IL 61455"};
String[] array4 = {"9", "444888", "Berkay Surmeli", "Chicago, IL 60625"};
String[] array5 = {"6", "444555", "Ali Murtaza Sumer", "Carson, Ca 90746"};
String[] array6 = {"7", "444666", "Hasan Huseyin Karabiber", "Los Angeles, Ca 90746"};
String[] array7 = {"8", "444777", "Mustafa Tinaztepe", "Macomb, IL 61455"};
String[] array8 = {"9", "444888", "Berkay Surmeli", "Chicago, IL 60625"};
ArrayList<String[]> aL = new ArrayList<>();
aL.add(array1);
aL.add(array2);
aL.add(array3);
aL.add(array4);
PAndDeliveryNotification question = new PAndDeliveryNotification(aL, "SAVE");
question.setDelivStopNo();
}
}
最佳答案
编辑:
您使用两个 JDialog 扩展类。它们都有 setAlwaysOnTop(true) 和 setModal(true) 因此它们都在竞争 View 。也许你应该使用 JFrame 和 JDialog
关于窗口居中时 Java 组件不出现,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31687649/
https://github.com/mattdiamond/Recorderjs/blob/master/recorder.js中的代码 我不明白 JavaScript 语法,比如 (functio
在 iOS 7 及更早版本中,如果我们想在应用程序中找到 topMostWindow,我们通常使用以下代码行 [[[UIApplication sharedApplication] windows]
我已经尝试解决这个问题很长一段时间了:我无法访问窗口的 url,因为它位于另一个域上..有一些解决方案吗? function login() { var cb = window.ope
是否可以将 FFMPEG 视频流传递到 C# 窗口?现在它在新窗口中作为新进程打开,我只是想将它传递给我自己的 SessionWindow。 此时我像这样执行ffplay: public void E
我有一个名为 x 的矩阵看起来像这样: pTime Close 1 1275087600 1.2268 2 1275264000 1.2264 3 1275264300 1.2
在编译时,发生搜索,grep搜索等,Emacs会在单独的窗口中创建一个新的缓冲区来显示结果,有没有自动跳转到那个窗口的方法?这很有用,因为我可以使用 n 和 p 而不是 M-g n 和 M-g p 移
我有一个启动 PowerShell 脚本的批处理文件。 批处理文件: START Powershell -executionpolicy RemoteSigned -noexit -file "MyS
我有一个基于菜单栏的应用程序,单击图标时会显示一个窗口。在 Mac OS X Lion 上一切正常,但由于某种原因,在 Snow Leopard 和早期版本的 Mac OS X 上会出现错误。任何时候
在 macOS 中,如何在 Xcode 和/或 Interface Builder 中创建带有“集成标题栏和工具栏”的窗口? 这是“宽标题栏”类型的窗口,已添加到 OS X 10.10 Yosemit
在浏览器 (Chrome) 中 JavaScript: var DataModler = { Data: { Something: 'value' }, Process: functi
我有 3 个 html 页面。第 1 页链接到第 2 页,第 2 页链接到第 3 页(为了简单起见)。 我希望页面 2 中的链接打开页面 3 并关闭页面 1(选项卡 1)。 据我了解,您无法使用 Ja
当点击“创建节点”按钮时,如何打开一个新的框架或窗口?我希望新框架包含一个文本字段和下拉菜单,以便用户可以选择一个选项。 Create node Search node
我有一个用户控件,用于编辑应用程序中的某些对象。 我最近遇到一个实例,我想弹出一个新的对话框(窗口)来托管此用户控件。 如何实例化新窗口并将需要设置的任何属性从窗口传递到用户控件? 感谢您的宝贵时间。
我有一个Observable,它发出许多对象,我想使用window或buffer操作对这些对象进行分组。但是,我不想指定count参数来确定窗口中应包含多少个对象,而是希望能够使用自定义条件。 例如,
我有以下代码,它打开一个新的 JavaFX 阶段(我们称之为窗口)。 openAlertBox.setOnAction(e -> { AlertBox alert = AlertBox
我要添加一个“在新窗口中打开”上下文菜单项,该菜单项将以新的UIScene打开我的应用程序文档之一。当然,我只想在实际上支持多个场景的设备上显示该菜单项。 目前,我只是在检查设备是否是使用旧设备的iP
我正在尝试创建一个 AIR 应用程序来记录应用程序的使用情况,使用 AIR 从系统获取信息的唯一简单方法是使用命令行工具和抓取 标准输出 . 我知道像 这样的工具顶部 和 ps 对于 OS X,但它们
所以我有这个简单的 turtle 螺旋制作器,我想知道是否有一种方法可以打印出由该程序创建的我的设计副本。 代码: import turtle x= float(input("Angle: ")) y
我正在编写一个 C# WPF 程序,它将文本消息发送到另一个程序的窗口。我有一个宏程序作为我的键盘驱动程序 (Logitech g15) 的一部分,它已经这样做了,尽管它不会将击键直接发送到进程,而是
我尝试使用以下代码通过 UDP 发送,但得到了奇怪的结果。 if((sendto(newSocket, sendBuf, totalLength, 0, (SOCKADDR *)&sendAd
我是一名优秀的程序员,十分优秀!