我需要将 JOptionPaneMessageDialog 置于 e.getSource 的父元素的中心。问题是我有两个不同的类。一个监听器类和另一个使用该监听器的类 这是我需要更改的行
JOptionPane.showMessageDialog((Component)e.getSource(), "pulsado");
package excepciones;
import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
class ButtonListener implements ActionListener{
@Override
public void actionPerformed(ActionEvent e) {
JOptionPane.showMessageDialog((Component)e.getSource(), "pulsado");
}
}
public class UseActionListener {
public static void main(String[] args) {
JFrame frame = new JFrame("Popup JComboBox");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JButton source = new JButton("Ring the bell!");
source.addActionListener(new ButtonListener());
frame.add(source, BorderLayout.SOUTH);
source.addActionListener(new ButtonListener());
frame.setSize(300, 200);
frame.setVisible(true);
}
}
最佳答案
JOptionPane.showMessageDialog(((Component) e.getSource()).getParent(), "pulsado")
;
关于java - 在生成事件的源元素的父元素中居中 JoptionPaneMessageDialog,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30747855/