java j监听程序_java – 单击按钮的JDialog动作监听器

如果用户按下确认后对话框将消失:

>并且你希望对话框的行为类似于模态JDialog,那么它很简单,因为一旦用户完成对话,你就会知道你的程序在代码中的位置 – 它会在你调用setVisible之后立即知道(true)在对话框上.因此,只需在对话框中调用setVisible(true)后立即在对话框中查询对话框对象的状态.

>如果需要处理非模态对话框,则需要在对话框窗口变为不可见时向对话框添加WindowListener以进行通知.

如果在用户按下确认后对话框保持打开状态:

>那么您应该使用上面建议的PropertyChangeListener.或者给对话框对象一个公共方法,允许外部类能够将ActionListener添加到确认按钮.

有关详细信息,请向我们显示您的代码的相关位,甚至更好,sscce.

例如,要允许JDialog类接受外部侦听器,您可以为其提供JTextField和JButton:

class MyDialog extends JDialog {

private JTextField textfield = new JTextField(10);

private JButton confirmBtn = new JButton("Confirm");

和一个允许外部类将ActionListener添加到按钮的方法:

public void addConfirmListener(ActionListener listener) {

confirmBtn.addActionListener(listener);

}

然后外部类可以简单地调用`addConfirmListener(…)方法将其ActionListener添加到confirmBtn.

例如:

import java.awt.Dimension;

import java.awt.event.ActionEvent;

import java.awt.event.ActionListener;

import javax.swing.*;

public class OutsideListener extends JFrame {

private JTextField textField = new JTextField(10);

private JButton showDialogBtn = new JButton("Show Dialog");

private MyDialog myDialog = new MyDialog(this, "My Dialog");

public OutsideListener(String title) {

super(title);

textField.setEditable(false);

showDialogBtn.addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent arg0) {

if (!myDialog.isVisible()) {

myDialog.setVisible(true);

}

}

});

// !! add a listener to the dialog's button

myDialog.addConfirmListener(new ActionListener() {

public void actionPerformed(ActionEvent e) {

String text = myDialog.getTextFieldText();

textField.setText(text);

}

});

JPanel panel = new JPanel();

panel.add(textField);

panel.add(showDialogBtn);

add(panel);

}

@Override

public Dimension getPreferredSize() {

return new Dimension(400, 300);

}

private static void createAndShowGui() {

JFrame frame = new OutsideListener("OutsideListener");

frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

frame.pack();

frame.setLocationRelativeTo(null);

frame.setVisible(true);

}

public static void main(String[] args) {

SwingUtilities.invokeLater(new Runnable() {

public void run() {

createAndShowGui();

}

});

}

}

class MyDialog extends JDialog {

private JTextField textfield = new JTextField(10);

private JButton confirmBtn = new JButton("Confirm");

public MyDialog(JFrame frame, String title) {

super(frame, title, false);

JPanel panel = new JPanel();

panel.add(textfield);

panel.add(confirmBtn);

add(panel);

pack();

setLocationRelativeTo(frame);

}

public String getTextFieldText() {

return textfield.getText();

}

public void addConfirmListener(ActionListener listener) {

confirmBtn.addActionListener(listener);

}

}

注意事项:除非绝对必要,否则我不建议继承JFrame或JDialog.这只是为了简洁起见.我自己也更喜欢使用模态对话框来解决这个问题,并在需要时重新打开对话框.

编辑2

使用模态对话框的示例:

import java.awt.Dimension;

import java.awt.event.ActionEvent;

import java.awt.event.ActionListener;

import javax.swing.*;

public class OutsideListener2 extends JFrame {

private JTextField textField = new JTextField(10);

private JButton showDialogBtn = new JButton("Show Dialog");

private MyDialog2 myDialog = new MyDialog2(this, "My Dialog");

public OutsideListener2(String title) {

super(title);

textField.setEditable(false);

showDialogBtn.addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent arg0) {

if (!myDialog.isVisible()) {

myDialog.setVisible(true);

textField.setText(myDialog.getTextFieldText());

}

}

});

JPanel panel = new JPanel();

panel.add(textField);

panel.add(showDialogBtn);

add(panel);

}

@Override

public Dimension getPreferredSize() {

return new Dimension(400, 300);

}

private static void createAndShowGui() {

JFrame frame = new OutsideListener2("OutsideListener");

frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

frame.pack();

frame.setLocationRelativeTo(null);

frame.setVisible(true);

}

public static void main(String[] args) {

SwingUtilities.invokeLater(new Runnable() {

public void run() {

createAndShowGui();

}

});

}

}

class MyDialog2 extends JDialog {

private JTextField textfield = new JTextField(10);

private JButton confirmBtn = new JButton("Confirm");

public MyDialog2(JFrame frame, String title) {

super(frame, title, true); // !!!!! made into a modal dialog

JPanel panel = new JPanel();

panel.add(new JLabel("Please enter a number between 1 and 100:"));

panel.add(textfield);

panel.add(confirmBtn);

add(panel);

pack();

setLocationRelativeTo(frame);

ActionListener confirmListener = new ConfirmListener();

confirmBtn.addActionListener(confirmListener); // add listener

textfield.addActionListener(confirmListener );

}

public String getTextFieldText() {

return textfield.getText();

}

private class ConfirmListener implements ActionListener {

public void actionPerformed(ActionEvent e) {

String text = textfield.getText();

if (isTextValid(text)) {

MyDialog2.this.setVisible(false);

} else {

// show warning

String warning = "Data entered, \"" + text +

"\", is invalid. Please enter a number between 1 and 100";

JOptionPane.showMessageDialog(confirmBtn,

warning,

"Invalid Input", JOptionPane.ERROR_MESSAGE);

textfield.setText("");

textfield.requestFocusInWindow();

}

}

}

// true if data is a number between 1 and 100

public boolean isTextValid(String text) {

try {

int number = Integer.parseInt(text);

if (number > 0 && number <= 100) {

return true;

}

} catch (NumberFormatException e) {

// one of the few times it's OK to ignore an exception

}

return false;

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值