我在Java中编写了一个漂亮的东西,其中包含一个GUI,它包含一个相当小的JDialog,然后使用pack()来容纳后来编程的东西.所有这一切都在JDialog渲染之前进行.
然后,令我惊讶的是,大约80%的时间,当我运行它时,窗口无法调整大小.它似乎完全是随机的,因为它理论上每次都做同样的事情.为什么在地球上它会在五秒钟后在同一台机器上使用相同的代码执行不同的操作?
顺便说一句,当我为此GUI启用本机Windows外观时,会弹出此问题.
我正在尝试在 JDialog 上制作 JButton,但是该按钮将覆盖整个 JDialog,对此有什么帮助吗?它看起来是这样的:

这就是我创建 JDialog 和 JButton 的方法:
class MenuStoreHandler implements ActionListener{
public void actionPerformed(ActionEvent e){
Dimension dim = Toolkit.getDefaultToolkit().getScreenSize();
int screenWidth = (int) dim.getWidth();
int screenHeight = (int) dim.getHeight();
JDialog g = new JDialog();
g.setTitle("The Store");
g.setSize(200, 200);
g.setLocation(screenWidth / 2 - 150, screenHeight / 2 - 150);
JButton b = new JButton("Buy");
b.addActionListener( new StoreItem1Handler() );
b.setVisible(true);
g.add(b);
g.setVisible(true);
}
}
Run Code Online (Sandbox Code Playgroud)
我将发布完整的 MrStan.class,如下:
package Progress;
public class MrStan extends JPanel{
private Timer timer = new Timer(); …Run Code Online (Sandbox Code Playgroud) 我尝试过这样的事情:
public static void main(final String[] args)
{
JDialog dialog = new JDialog();
dialog.add(new JButton("XXXXXXX"));
dialog.setVisible(true);
dialog.setSize(new Dimension(100,100));
dialog.setMaximumSize(new Dimension(100,100));
}
Run Code Online (Sandbox Code Playgroud)
但我仍然可以在我设置的100,100限制之上调整此对话框的大小.有什么建议?谢谢!
我在JDialog中有一个可调整大小的JScrollPane.JScrollPane有一个JPanel(称为容器)所有这些JScrollPane,JPanel和Jdialog都是可调整大小的.在运行时,另一个JPanel(称为子)被添加到之前的jPanel,它位于JDialog的JScrollPane内.我想获得'子'JPanel大小的大小.在将'child'JPanel添加到'container'JPanel之前和之后,'child'和'container'JPanels的大小显示为0 0.(child.getSize() , container.getSize()).
在查看JComponent之前,获取大小可能是不可能的.但就我而言,即使添加并查看了'child'JPanel,大小仍为0.
在添加到'容器'JPanel后,如何才能获得'child'JPanel的实际大小
当用户在字段中键入文本并按创建时。JFrame 标题更改为更新的用户标题。JFrame 标题变量在 --Window.java-- 中,用户输入在 --NewFileBox.java-- 中。我不知道您将如何从单独的文件中实现更改。
我在尝试从屏幕上单击的点向负方向绘制矩形时遇到问题。我有以下类来模拟 Gyazo 之类的屏幕捕获软件:
class DrawSquare extends JPanel implements MouseListener, MouseMotionListener {
// Components
public JDialog frame;
public Rectangle rectangle;
public BufferedImage bufferedImage;
public Point start, end;
// Variables
public String capturedImage;
public DrawSquare(JDialog frame) {
this.frame = frame;
// Read in crosshair image to replace mouse icon
Toolkit tool = Toolkit.getDefaultToolkit();
Image newImage = getToolkit().getImage("components/cursor.png");
Cursor cursor = tool.createCustomCursor(newImage, new Point (this.frame.getX(), this.frame.getY()), "img");
this.frame.setCursor(cursor);
this.frame.addMouseListener(this);
this.frame.addMouseMotionListener(this);
}
@Override
public void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2d = (Graphics2D) g.create(); …Run Code Online (Sandbox Code Playgroud) 如何JDialog在点击JButton两次或更多时停止重复打开?
我有一个应用程序,它使用JDialog从用户获取输入,然后搜索文件,而不是浏览对话框,但使用元数据更专业.
这一切都很好.唯一的问题是我希望能够让用户输入搜索值,按Ok,并接收这些值来执行搜索和其他一些操作(来自打开对话框的调用类)而不关闭对话框?
有必要从调用类中执行这些操作,因为这是编辑器中插件的一部分.
基本上,简而言之,它有点像查找对话框在任何编辑器中的工作方式 - 当您从一个找到的项目跳到下一个项目时,查找对话框保持打开状态...
好像我错过了一些简单的东西,但我看不出怎么做.
编辑:
我根据Nick Rippe建议的教程在一个简单的测试应用程序中尝试了这个,但我认为我在某种程度上误解了它,因为我无法让它工作.我添加了一个带有getter和setter的字段,然后尝试获取它:
主要课程:
public class TestJFrames {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
TestForm frame = new TestForm();
frame.setDefaultCloseOperation(frame.DO_NOTHING_ON_CLOSE);
frame.addPropertyChangeListener("fileSelected", new FileSelectedListener());
frame.setVisible(true);
}
}
class FileSelectedListener implements PropertyChangeListener {
@Override
public void propertyChange(PropertyChangeEvent evt) {
System.out.println("TEST");
}
}
Run Code Online (Sandbox Code Playgroud)
从表单类:
private String fileSelected;
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
this.setFileSelected("Test");
}
public String getFileSelected() {
return fileSelected;
}
public void setFileSelected(String fileSelected) {
this.fileSelected …Run Code Online (Sandbox Code Playgroud) 我想通过点击键盘上的"enter"键关闭我的JDialog.我怎样才能做到这一点?谢谢!注意:我想这样做,不涉及任何按钮.谢谢!
我试图通过点击按钮让我的JDialog弹出我的JFrame中心.我有JOptionPanel,超过父的JFrame正常弹出,但的JDialog相对于弹出到JFrame而不是在中心.
这些按钮在我的代码中实际上是JMenuItem,但我在这里将它们写成JButton以使事情变得更容易和更直接.
这是我的代码:
从我的父JFrame调用:
JButton about = new JButton("About");
about.addActionListener(new ActionListener() { //this one IS NOT in the center of MyJFrame
public void actionPerformed(ActionEvent e) {
new AboutDialog(MyJFrame.this);
}
});
JButton exit = new JButton("Exit");
exit.addActionListener(new ActionListener() { //this one IS in the center of MyJFrame
public void actionPerformed(ActionEvent e) {
if(JOptionPane.showConfirmDialog(MyJFrame.this, "Are you sure you want to exit ?","",JOptionPane.YES_NO_OPTION) == 0)
System.exit(0);
}
});
Run Code Online (Sandbox Code Playgroud)
关于对话类
public class AboutDialog extends JDialog{
public AboutDialog(JFrame parent) {
setLocationRelativeTo(parent);
setLayout(new BorderLayout());
...
Run Code Online (Sandbox Code Playgroud)


谢谢
java ×10
jdialog ×10
swing ×9
jbutton ×2
jframe ×2
joptionpane ×1
jscrollpane ×1
layout ×1
modal-dialog ×1
titlebar ×1