gpt4 book ai didi

java - 将 JDialog 置于 JTabbedPane 上的 JPanel 之上

转载 作者:行者123 更新时间:2023-12-02 18:08:52 25 4
gpt4 key购买 nike

我已经尝试了在这里和其他网站上找到的所有建议。

我似乎无法让这个 JDialog 位于 JTabbedPane 面板的中心。

请注意,我必须禁用关闭按钮,因此我无法使用标准 JOptionPane.showDialogXYZ() 方法。

有什么想法吗?

import java.awt.BorderLayout;
import java.awt.Dialog.ModalityType;
import java.awt.Frame;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;

import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTabbedPane;
import javax.swing.SwingUtilities;

public class CenterDialog extends JFrame
{
public CenterDialog()
{
setResizable(false);

setName(getClass().getSimpleName());
setTitle("My Frame");
setSize(300, 300);

JTabbedPane tabbedPane = new JTabbedPane(JTabbedPane.TOP);

// Add the panel
tabbedPane.addTab("Button panel", new MyButtonPanel());

add(tabbedPane, BorderLayout.CENTER);

getContentPane().add(tabbedPane);
}

private class MyButtonPanel extends JPanel
{

public MyButtonPanel()
{
JButton btnShowDialog = new JButton("Show Dialog");
btnShowDialog.addActionListener(new BtnShowDialogActionListener());
add(btnShowDialog);
}

private class BtnShowDialogActionListener implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
// TODO: Figure out how to center this dialog box
final String YES = "Yup";
final String NO = "Nope";
final Object[] options = { YES, NO };

final JOptionPane optionPane = new JOptionPane("Is this centered.", JOptionPane.QUESTION_MESSAGE,
JOptionPane.YES_NO_OPTION, null, options, NO);

Frame f = JOptionPane.getFrameForComponent(((JButton) e.getSource()).getParent());
final JDialog dialog = new JDialog(f, "Question", ModalityType.APPLICATION_MODAL);

dialog.setContentPane(optionPane);
dialog.setDefaultCloseOperation(JDialog.DO_NOTHING_ON_CLOSE);

dialog.addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent we)
{
System.out.println("Ignoring close button");
}
});

optionPane.addPropertyChangeListener(new PropertyChangeListener()
{
public void propertyChange(PropertyChangeEvent e)
{
String prop = e.getPropertyName();

if (dialog.isVisible() && (e.getSource() == optionPane))
{
if (prop.equals(JOptionPane.VALUE_PROPERTY))
{
dialog.setVisible(false);
}
}
}
});

dialog.pack();
dialog.setVisible(true);
}
}
}

private static void createAndShowGUI()
{
// Create and set up the window.
CenterDialog frame = new CenterDialog();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}

public static void main(String[] args)
{
SwingUtilities.invokeLater(new Runnable()
{
public void run()
{
try
{
createAndShowGUI();
}
catch (Exception e)
{
e.printStackTrace();
System.exit(0);
}
}
});
}

}

}

最佳答案

将对话框相对于给定组件居中的方法(无需手动计算,在内部处理组件到屏幕的坐标映射):

  dialog.setLocationRelativeTo(someComponent);

根据您想要实现的具体目标选择组件:

 // center relative to the button
dialog.setLocationRelativeTo((Component) e.getSource());

// center relative to button's parent
dialog.setLocationRelativeTo(((Component) e.getSource()).getParent());

// center relative to the tabbedPane
JTabbedPane tabbed = // walk the parent chain until you reach it
dialog.setLocationRelativeTo(tabbed);

关于java - 将 JDialog 置于 JTabbedPane 上的 JPanel 之上,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8964558/

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