我是
靠谱客的博主
香蕉服饰,最近开发中收集的这篇文章主要介绍
用模态的方式打开自定义JDialog,并获取返回值,觉得挺不错的,现在分享给大家,希望可以做个参考。
概述
JFrame的变量名为jf
JDialog的类名为Fff
- btnLoadImage.addMouseListener(new MouseAdapter() {
- @Override
- public void mouseClicked(MouseEvent e) {
- Fff myff = new Fff();
-
- 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的类
- 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;
-
-
-
-
-
-
-
- public class Fff extends JDialog {
-
- private final JPanel contentPanel = new JPanel();
- private JTextField textOut;
- private int iCloseType;
-
-
-
-
- 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();
- }
- }
-
-
-
-
- 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,并获取返回值的全部内容,希望文章能够帮你解决用模态的方式打开自定义JDialog,并获取返回值所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复