用模态的方式打开自定义JDialog,并获取返回值
65 阅读
0 评论
43 点赞
JFrame的变量名为jf
JDialog的类名为Fff
[java]
view plain
copy
- btnLoadImage.addMouseListener(new MouseAdapter() {
- @Override
- public void mouseClicked(MouseEvent e) {
- Fff myff = new Fff();
- //myff.setModal(false);
- myff.setModalityType(ModalityType.APPLICATION_MODAL);
- myff.setVisible(true);
- //如果是点击确定,则文本框中输入的内容显示出来
- String strText;
- int iReturn;
- iReturn = myff.f_GetCoseType();
- if (iReturn == 0) {
- lbImage.setText("选择了取消");
- }
- else
- {
- strText = myff.f_GetText();
- lbImage.setText(strText);
- }
- }
- });
Fff的类
[java]
view plain
copy
- package bb;
- import java.awt.BorderLayout;
- import java.awt.FlowLayout;
- import javax.swing.JButton;
- import javax.swing.JDialog;
- import javax.swing.JOptionPane;
- import javax.swing.JPanel;
- import javax.swing.border.EmptyBorder;
- import javax.swing.JTextField;
- import java.awt.event.MouseAdapter;
- import java.awt.event.MouseEvent;
- /**
- * @author 作者 E-mail:
- * @version 创建时间:Sep 18, 2017 2:48:29 PM
- * 类说明
- */
- public class Fff extends JDialog {
- private final JPanel contentPanel = new JPanel();
- private JTextField textOut;
- private int iCloseType; //点击确定,该值为1,点击取消,该值为0
- /**
- * Launch the application.
- */
- public static void main(String[] args) {
- try {
- Fff dialog = new Fff();
- dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
- dialog.setVisible(true);
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
- /**
- * Create the dialog.
- */
- public Fff() {
- iCloseType = 0 ; //默认是点击取消按钮
- setBounds(100, 100, 450, 300);
- getContentPane().setLayout(new BorderLayout());
- contentPanel.setBorder(new EmptyBorder(5, 5, 5, 5));
- getContentPane().add(contentPanel, BorderLayout.CENTER);
- contentPanel.setLayout(new BorderLayout(0, 0));
- {
- JPanel panel = new JPanel();
- contentPanel.add(panel);
- panel.setLayout(new BorderLayout(0, 0));
- {
- textOut = new JTextField();
- panel.add(textOut, BorderLayout.NORTH);
- textOut.setColumns(10);
- }
- }
- {
- JPanel buttonPane = new JPanel();
- buttonPane.setLayout(new FlowLayout(FlowLayout.RIGHT));
- getContentPane().add(buttonPane, BorderLayout.SOUTH);
- {
- JButton okButton = new JButton("OK");
- okButton.addMouseListener(new MouseAdapter() {
- @Override
- public void mouseClicked(MouseEvent e) {
- //检查用户是否输入了内容
- if(f_chkInput()) {
- iCloseType = 1; //点击了确定性按钮
- dispose();
- }
- }
- });
- okButton.setActionCommand("OK");
- buttonPane.add(okButton);
- getRootPane().setDefaultButton(okButton);
- }
- {
- JButton cancelButton = new JButton("Cancel");
- cancelButton.addMouseListener(new MouseAdapter() {
- @Override
- public void mouseClicked(MouseEvent e) {
- iCloseType = 0; //点击了取消按钮
- dispose();
- }
- });
- cancelButton.setActionCommand("Cancel");
- buttonPane.add(cancelButton);
- }
- }
- }
- //检查用户是否输入了文本
- public boolean f_chkInput() {
- String strText;
- boolean bRet;
- strText = textOut.getText();
- if ( strText.length()==0 || strText==null) {
- JOptionPane jMsg = new JOptionPane();
- jMsg.showMessageDialog(null, "请输入内容");
- bRet = false;
- }
- else
- {
- bRet = true;
- }
- return bRet;
- }
- public int f_GetCoseType() {
- return iCloseType;
- }
- public String f_GetText() {
- String strOut;
- strOut = textOut.getText();
- return strOut;
- }
- }
最后
以上就是香蕉服饰最近收集整理的关于用模态的方式打开自定义JDialog,并获取返回值的全部内容,更多相关用模态内容请搜索靠谱客的其他文章。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
- 本文分类:Swing
- 浏览次数:65 次浏览
- 发布日期:2023-09-25 21:01:24
- 本文链接:https://www.kaopuke.com/article/k-p-k_14_uzocf0_13__23__10_3.html
发表评论 取消回复