如何限制Joptionpane消息对话框提示输入条件? [英] how to restrict Joptionpane message dialog box prompting once for the Condition?

查看:56
本文介绍了如何限制Joptionpane消息对话框提示输入条件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我下面的代码,必须以GUI形式生成(使用Swing awt) 我的代码工作是从文件夹中读取文本文件,并获得重复的单词计数,并将那些选定的文件保存到用户指定的文件夹中.

This is my code below that have to be generated in GUI form(Using Swing awt) My code work is to read text files from a folder and get repeated words count and saving those selected files to a user specified folder.

if(tmp.isSelected()) {
            String filepathNName =  tf.getText() +"\\"+ tmp.getText();
            //filepathNName = System.getProperty("path.separator");
            String filename = tmp.getText();
            System.out.println("filename = " + filename);
            System.out.println("filepaname = " + filepathNName);
            System.out.println("outputDir = " + outputDir);
            boolean success = latestex1.exlCreator(filepathNName, outputDir, filename);
            if(success) {
                JOptionPane.showMessageDialog(null, "Successfully Completed. Pls refer to the path " + outputDir + " for output files");
            } else {
                JOptionPane.showMessageDialog(null, "OOPS. Some Error!!!");
            }

在此代码段中,变量"success"通过从另一个程序调用属性来执行文件保存操作.

In this code snippet The variable "success " does the file saving action by calling the properties from another program.

保存后保存几个文件,然后说(100)我的消息对话框应该仅显示一次提示. 但是,对于每个保存文件操作,都会一次又一次提示.

After saving saving several files say(100) My message dialog box should show the prompt only once. But for each and every save file action it prompts again and again.

如果我保存100个文件,则消息框将出现100次.*(如果是,则为其他).

If I save 100 Files the message box appears 100 times.*(IF and else).

所有文件保存后,我只需要显示一次即可!!

I need to make appear it only once after all saving the files.!

欢迎任何建议! 预先感谢.

Any suggestions welcomed! Thanks in advance.

full code below:

package latestprojectswing;

import java.awt.Dimension;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.util.ArrayList;
import java.util.Iterator;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollBar;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.ScrollPaneConstants;
public class latestswingpgm3 extends Thread implements ActionListener
{
 JFrame frame;
JPanel panel;
JTextField tf,text,tf1;
JTextArea ta,text1;
JLabel lab1;
String str;
JScrollBar scrol;
JScrollPane scrollPane;
File fl;
private JCheckBox chckbxSelectAll;
private JCheckBox chckbxf1;
private JTextField textField;
private JLabel lblSourceFolderfiles;
private JButton btnChoosedirectoryfrom;
private JButton btnDisplay;
private JLabel lblListFilesBelow;
private JScrollPane jScrollPane1;
ArrayList<JCheckBox> aL = new ArrayList<JCheckBox>();
int selectedCounter = 0;
int y=100;
JPanel contentPane;
String outputDir;
latestswingpgm3()
{
frame = new JFrame( "Search box" );
frame.getContentPane().setLayout( null );
frame.setSize( 820, 700 );

panel = new JPanel();
panel.setBounds( 25, 90, 750, 400 );
//panel.setLayout(null);
//panel.setBounds( 25, 90, 750, 400 );
scrollPane = new JScrollPane( panel );
scrollPane.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
scrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
//scrollPane.setBounds( 25, 90, 750, 300 );
scrollPane.setPreferredSize(new Dimension(500, 300));
//panel.setVisible(true);
scrollPane.setVisible(true);
frame.add(panel);
frame.add(scrollPane);

tf = new JTextField();
tf.setBounds( 25, 50, 750, 40 );
tf.setFont( new Font( "Latha", Font.BOLD, 20 ) );
tf.setHorizontalAlignment( JTextField.CENTER );
frame.getContentPane().add( tf );


chckbxSelectAll = new JCheckBox("Select All");
chckbxSelectAll.setBounds(25, 557, 97, 23);
frame.getContentPane().add(chckbxSelectAll);
chckbxSelectAll.addActionListener(new ActionListener() {
    public void actionPerformed(ActionEvent e) {
        Iterator<JCheckBox> i = aL.iterator();
        while(i.hasNext()) {
            JCheckBox tmp = i.next();
            if(chckbxSelectAll.isSelected()) {
                tmp.doClick();
            } else {
                tmp.setSelected(false);
                selectedCounter -= 1;
                if(selectedCounter < 0) {
                    selectedCounter = 0;
                }
                textField.setText(Integer.toString(selectedCounter));
            }

        }
    }
    });

JButton btnGenerate = new JButton("Generate");
btnGenerate.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
    Iterator<JCheckBox> i = aL.iterator();
    while(i.hasNext()) {
        JCheckBox tmp = i.next();

        //scroll adder
        jScrollPane1 = new JScrollPane(frame);
        jScrollPane1.add(tmp);
        jScrollPane1.revalidate();
        if(tmp.isSelected()) {
            String filepathNName =  tf.getText() +"\\"+ tmp.getText();
            //filepathNName = System.getProperty("path.separator");
            String filename = tmp.getText();
            System.out.println("filename = " + filename);
            System.out.println("filepaname = " + filepathNName);
            System.out.println("outputDir = " + outputDir);
            boolean success = latestex1.exlCreator(filepathNName, outputDir, filename);

            if(success) {
                JOptionPane.showMessageDialog(null, "Successfully Completed. Pls refer to the path " + outputDir + " for output files");


            } else {

                JOptionPane.showMessageDialog(null, "OOPS. Some Error!!!");

            }
        }
    }

}
});
btnGenerate.setBounds(316, 604, 89, 23);
frame.getContentPane().add(btnGenerate);

textField = new JTextField();
textField.setBounds(268, 558, 86, 20);
frame.getContentPane().add(textField);
textField.setColumns(10);

JLabel lblNoOfFiles = new JLabel("NO of Files Selected");
lblNoOfFiles.setBounds(141, 561, 139, 14);
frame.getContentPane().add(lblNoOfFiles);

JLabel lblDestinationFolderTo = new JLabel("Destination PathTo Generate Files");
lblDestinationFolderTo.setBounds(553, 561, 226, 14);
frame.getContentPane().add(lblDestinationFolderTo);

JButton btnBrowse = new JButton("Browse");
btnBrowse.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {   

    JFileChooser chooser = new JFileChooser();
    chooser.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);
    int returnVal = chooser.showOpenDialog(frame);
    if(returnVal == JFileChooser.APPROVE_OPTION) {
        str = chooser.getSelectedFile().getAbsolutePath();
    }

    if ( str != null && !str.trim().equals( "" ) )
    {
        tf1.setText( str );
        outputDir = str;
      // Enable the search button
      //btnDisplay.setEnabled( true );
    }
    else
    {
   //btnDisplay.setEnabled( false );
    }
}
});
btnBrowse.setBounds(553, 583, 89, 23);
frame.getContentPane().add(btnBrowse);

tf1 = new JTextField();
tf1.setBounds( 553, 620, 400, 30 );
tf1.setFont( new Font( "Latha", Font.BOLD, 20 ) );
frame.getContentPane().add( tf1 );

lblSourceFolderfiles = new JLabel("Source Folder/ Files");
lblSourceFolderfiles.setBounds(6, 17, 138, 14);
frame.getContentPane().add(lblSourceFolderfiles);


btnChoosedirectoryfrom = new JButton("ChooseDirectory From");
btnChoosedirectoryfrom.addActionListener(this);
btnChoosedirectoryfrom.setBounds(141, 9, 170, 30);
frame.getContentPane().add(btnChoosedirectoryfrom);

btnDisplay = new JButton("Select To Display");
btnDisplay.setEnabled(false);
btnDisplay.setBounds(534, 9, 180, 30);
btnDisplay.addActionListener( this );

frame.getContentPane().add(btnDisplay);

lblListFilesBelow = new JLabel("List files Below to choose ");
lblListFilesBelow.setBounds(344, 17, 180, 14);
frame.getContentPane().add(lblListFilesBelow);
frame.setVisible( true );
frame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
}

public void actionPerformed( ActionEvent ae )
{
if ( ae.getActionCommand().equals( "ChooseDirectory From" ) )
{
  JFileChooser chooser = new JFileChooser();
  chooser.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);
  int returnVal = chooser.showOpenDialog(frame);
  if(returnVal == JFileChooser.APPROVE_OPTION) {
        str = chooser.getSelectedFile().getAbsolutePath();
    }

  if ( str != null && !str.trim().equals( "" ) )
  {
    tf.setText( str );

    // Enable the search button
    btnDisplay.setEnabled( true );
  }
  else
  {
 btnDisplay.setEnabled( false );
  }
}

if ( ae.getActionCommand().equals( "Select To Display" ) )
{
  if(aL!=null) {
      Iterator<JCheckBox> i = aL.iterator();
      while(i.hasNext()) {
          panel.remove(i.next());
      }
      selectedCounter = 0;
      textField.setText(Integer.toString(selectedCounter));
  }
  fl = new File( str );
  File[] flist = fl.listFiles();
  for ( int i = 0; i < flist.length; i++ )
  {
      if ( flist[i].isFile() )
      {
     final JCheckBox cb1 = new JCheckBox(flist[i].getName());


     scrollPane.add(cb1);
     panel.add(cb1);



     cb1.setBounds(25, y, 200, 25);
     cb1.setFont(new Font( "Latha", Font.BOLD, 20 ));
     cb1.addActionListener(new ActionListener() {
         public void actionPerformed(ActionEvent e){






                if(cb1.isSelected()) {
                    selectedCounter += 1;
                } else {
                    selectedCounter -= 1;
                    if(selectedCounter < 0) {
                        selectedCounter = 0;
                    }
                }
                textField.setText(Integer.toString(selectedCounter));
            }
         });
     aL.add(cb1);
     panel.add(cb1);
     y+=30;   
      }
  }
}
}

public static void main( String args[] )
 {
new latestswingpgm3();
}
}

推荐答案

更改此

if(success) { JOptionPane.showMessageDialog(null, "Successfully Completed. Pls refer to the path " + outputDir + " for output files"); }else { JOptionPane.showMessageDialog(null, "OOPS. Some Error!!!"); }

收件人

if(success&&!once) { JOptionPane.showMessageDialog(null, "Successfully Completed. Pls refer to the path " + outputDir + " for output files");once=true; }else if(once! =true) { JOptionPane.showMessageDialog(null, "OOPS. Some Error!!!"); }

在if ..外部声明一次为boolean一次= false.

Declare once as boolean once=false outside if..

修改

自从您发布了完整的源代码以来..现在很清楚..您必须在while循环外声明一次变量.

Since you posted the full source code.. Now it's clear.. You have to declare once variable outside the while loop..

这篇关于如何限制Joptionpane消息对话框提示输入条件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆