java - 在生成事件的源元素的父元素中居中 JoptionPaneMessageDialog

标签 java swing events java-8

我需要将 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/

相关文章:

Java 堆栈到 GUI

java - 组件支持哪些 SynthConstants?

javascript - FlexSlider:如何使用 JS 或 jQuery 在页面加载时获取图像高度?

javascript - 使用 shift 修饰符从键码中获取键字符(值)

java - 推断嵌套静态泛型函数的泛型类型

java - 使用 radioButtons 更新 ListView 适配器输入

java - 我可以在同一个 URI 上配置 HTML 和 Spring Data REST JSON 吗?

java - 错误: Column 'col_name' not found with createSQLQuery

java - Java内部有Thread.sleep时定时器不会停止

node.js - Node JS 如何处理缓慢的 http 请求?