当前位置: 技术问答>java相关
在JBuilder里面,如何在一个JFrame里面知道其中的一个对话框点击了哪个按钮?
来源: 互联网 发布时间:2015-11-15
本文导语: 一个JFrame,点击Open按钮,打开一个对话框,对话中点击某一个按钮后关闭该对话框,如何在Frame里面知道点击了对话框中的哪个按钮? | 在JFrame里面扩展此dialog类,覆盖其按钮方法即可 比如...
一个JFrame,点击Open按钮,打开一个对话框,对话中点击某一个按钮后关闭该对话框,如何在Frame里面知道点击了对话框中的哪个按钮?
|
在JFrame里面扩展此dialog类,覆盖其按钮方法即可
比如:我更新数据,弹出“是否确定更新”对话框,dialog如下:
package omt;
import javax.swing.*;
import java.awt.*;
import com.borland.jbcl.layout.*;
import javax.swing.border.*;
import java.awt.event.*;
/**
* 作者:alphazhao
* 日期:2002-5-29
* 描述:确定修改时,弹出此确定窗口
* */
public class ShowUpdateMsg extends JDialog {
private TitledBorder titledBorder1;
private String strMsg;
private Font font14 = new Font("Dialog", 0, 14);
private JPanel jPanel1 = new JPanel();
private XYLayout xYLayout1 = new XYLayout();
private JLabel jLabel1 = new JLabel();
private JButton jButton1 = new JButton();
private JButton jButton2 = new JButton();
// public ShowUpdateMsg(Frame owner, boolean modal) {
public ShowUpdateMsg(Dialog owner, boolean modal) {
super(owner,modal);
try {
this.setTitle("系统提示");
this.setResizable(false);
// this.setBackground(color);
//NewLineMsg nlm = new NewLineMsg();
//strMsg = nlm.newLineMsgWay(msg);//组织换行处理
jbInit();
}
catch(Exception e) {
e.printStackTrace();
}
}
private void jbInit() throws Exception {
jPanel1.setLayout(xYLayout1);
jLabel1.setFont(font14);
// jLabel1.setForeground(colorTxt);
jLabel1.setText("是否确定要修改此条记录?");
// jButton1.setBackground(color);
jButton1.setFont(font14);
//jButton1.setBorder(BorderFactory.createEtchedBorder());
jButton1.setText("确 定");
/*//
jButton1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(ActionEvent e) {
jButton1_actionPerformed(e);
}
});
//*/
// jButton2.setBackground(color);
jButton2.setFont(font14);
//jButton2.setBorder(BorderFactory.createEtchedBorder());
jButton2.setText("取 消");
/*//
jButton2.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(ActionEvent e) {
jButton2_actionPerformed(e);
}
});
//*/
this.setSize(350,130);
// jPanel1.setBackground(color);
this.getContentPane().add(jPanel1, BorderLayout.CENTER);
jPanel1.add(jLabel1, new XYConstraints(94, 22, 221, 35));
jPanel1.add(jButton1, new XYConstraints(91, 65, 75, 25));
jPanel1.add(jButton2, new XYConstraints(180, 65, 75, 25));
//Center the window
CenterShowDialog csf = new CenterShowDialog(this);
///
SymListener symListener = new SymListener();
jButton1.addActionListener(symListener);
jButton2.addActionListener(symListener);
//设置快捷键
jButton1.registerKeyboardAction(symListener,
KeyStroke.getKeyStroke(KeyEvent.VK_ENTER, 0),
JComponent.WHEN_IN_FOCUSED_WINDOW);
//确定按钮为回车键"ENTER"
jButton2.registerKeyboardAction(symListener,
KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0),
JComponent.WHEN_IN_FOCUSED_WINDOW);
//取消按钮为退出键"Escape"
//*/
}
void jButton1_actionPerformed(ActionEvent e) {
//调用时此方法须重写
//this.dispose();
}
void jButton2_actionPerformed(ActionEvent e) {
this.dispose();
}
///设置快捷键
class SymListener implements ActionListener {
public void actionPerformed(ActionEvent e) {
Object obj = e.getSource();
if (obj == jButton1) {
jButton1_actionPerformed(e);
}
else if (obj == jButton2) {
jButton2_actionPerformed(e);
}
}
}
//*/
}
比如:我更新数据,弹出“是否确定更新”对话框,dialog如下:
package omt;
import javax.swing.*;
import java.awt.*;
import com.borland.jbcl.layout.*;
import javax.swing.border.*;
import java.awt.event.*;
/**
* 作者:alphazhao
* 日期:2002-5-29
* 描述:确定修改时,弹出此确定窗口
* */
public class ShowUpdateMsg extends JDialog {
private TitledBorder titledBorder1;
private String strMsg;
private Font font14 = new Font("Dialog", 0, 14);
private JPanel jPanel1 = new JPanel();
private XYLayout xYLayout1 = new XYLayout();
private JLabel jLabel1 = new JLabel();
private JButton jButton1 = new JButton();
private JButton jButton2 = new JButton();
// public ShowUpdateMsg(Frame owner, boolean modal) {
public ShowUpdateMsg(Dialog owner, boolean modal) {
super(owner,modal);
try {
this.setTitle("系统提示");
this.setResizable(false);
// this.setBackground(color);
//NewLineMsg nlm = new NewLineMsg();
//strMsg = nlm.newLineMsgWay(msg);//组织换行处理
jbInit();
}
catch(Exception e) {
e.printStackTrace();
}
}
private void jbInit() throws Exception {
jPanel1.setLayout(xYLayout1);
jLabel1.setFont(font14);
// jLabel1.setForeground(colorTxt);
jLabel1.setText("是否确定要修改此条记录?");
// jButton1.setBackground(color);
jButton1.setFont(font14);
//jButton1.setBorder(BorderFactory.createEtchedBorder());
jButton1.setText("确 定");
/*//
jButton1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(ActionEvent e) {
jButton1_actionPerformed(e);
}
});
//*/
// jButton2.setBackground(color);
jButton2.setFont(font14);
//jButton2.setBorder(BorderFactory.createEtchedBorder());
jButton2.setText("取 消");
/*//
jButton2.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(ActionEvent e) {
jButton2_actionPerformed(e);
}
});
//*/
this.setSize(350,130);
// jPanel1.setBackground(color);
this.getContentPane().add(jPanel1, BorderLayout.CENTER);
jPanel1.add(jLabel1, new XYConstraints(94, 22, 221, 35));
jPanel1.add(jButton1, new XYConstraints(91, 65, 75, 25));
jPanel1.add(jButton2, new XYConstraints(180, 65, 75, 25));
//Center the window
CenterShowDialog csf = new CenterShowDialog(this);
///
SymListener symListener = new SymListener();
jButton1.addActionListener(symListener);
jButton2.addActionListener(symListener);
//设置快捷键
jButton1.registerKeyboardAction(symListener,
KeyStroke.getKeyStroke(KeyEvent.VK_ENTER, 0),
JComponent.WHEN_IN_FOCUSED_WINDOW);
//确定按钮为回车键"ENTER"
jButton2.registerKeyboardAction(symListener,
KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0),
JComponent.WHEN_IN_FOCUSED_WINDOW);
//取消按钮为退出键"Escape"
//*/
}
void jButton1_actionPerformed(ActionEvent e) {
//调用时此方法须重写
//this.dispose();
}
void jButton2_actionPerformed(ActionEvent e) {
this.dispose();
}
///设置快捷键
class SymListener implements ActionListener {
public void actionPerformed(ActionEvent e) {
Object obj = e.getSource();
if (obj == jButton1) {
jButton1_actionPerformed(e);
}
else if (obj == jButton2) {
jButton2_actionPerformed(e);
}
}
}
//*/
}