gpt4 book ai didi

java - 根据父框架 Swing JDialog 大小

转载 作者:行者123 更新时间:2023-11-29 07:17:26 24 4
gpt4 key购买 nike

我正在创建一个对话框并将其大小设置为

setSize(m_appview.getSize());

其中 m_appview 是对话框将出现在上方的 JFrame。这个 JFrame 包含一个 JButton,当按下它时将显示对话框。现在,当我最大化框架并单击按钮时,对话框将打开,其宽度与框架的宽度相匹配,但高度小于 JFrame 的高度。

怎么办?

最佳答案

例如

enter image description hereenter image description here

来自代码

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.Point;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFrame;

public class JFrameAndJDialog {

private JFrame frame = new JFrame("PopupFactory Sample");
private JDialog dialog = new JDialog();
private JButton start1 = new JButton("Pick Me for Popup");
private JButton start2 = new JButton("Close this Popup");
private Point pnt = null;
private Dimension dim = null;

public JFrameAndJDialog() {
start2.addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent e) {
dialog.setVisible(false);
}
});
dialog.add(start2, BorderLayout.SOUTH);
dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
dialog.setUndecorated(true);
dialog.setVisible(false);
start1.addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent e) {
dim = frame.getSize();
pnt = frame.getLocationOnScreen();
int x = dim.width - 8;
int y = dim.height - 8;
dialog.setSize(x, y);
x = pnt.x + 4;
y = pnt.y + 4;
dialog.setLocation(x, y);
visibleForJDialog();
}
});
frame.add(start1, BorderLayout.SOUTH);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLocation(150, 150);
frame.setSize(300, 300);
frame.setVisible(true);
}

private void visibleForJDialog() {
java.awt.EventQueue.invokeLater(new Runnable() {

@Override
public void run() {
dialog.setVisible(true);
}
});
}

public static void main(final String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {

@Override
public void run() {
JFrameAndJDialog uPF = new JFrameAndJDialog();
}
});
}
}

关于java - 根据父框架 Swing JDialog 大小,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8367507/

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