java-Dialog对话框

要求:
1.编写一个应用程序,窗口中通过按钮input,弹出一个输入对话
框,并在对话框中输入字符串inputText,并对inputText进行处
理,要求提取文本中的数字数据,并对数组进行排序之后,输
出在文本区域output中,同时显示求和的结果与平均值。通过
按钮find,可以弹出一个输入框,可供输入数据num,并实现在
数组中查找num,查找的结果用message的对话框显示。
如图所示:

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
代码:

ublic class test {
   public static void main(String args[]) {
      WindowInput win=new WindowInput();
      win.setTitle("带输入对话框的窗口"); 
      win.setBounds(80,90,300,300);
   }
}
import java.awt.event.*; 
import java.awt.*;
import javax.swing.*;
import java.util.*;
public class WindowInput extends JFrame implements ActionListener {
   JTextArea output;
   JButton Input,find;
   String a[];
   int array1[];
   WindowInput() {
       Input=new JButton("弹出输入对话框");
       find=new JButton("请输入要查找的数据");
       output=new JTextArea();
       add(Input,BorderLayout.NORTH);
       add(find,BorderLayout.SOUTH);
       add(new JScrollPane(output),BorderLayout.CENTER);
       Input.addActionListener(this); 
       find.addActionListener(this);
       setVisible(true);
       setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
   }
   public void actionPerformed(ActionEvent e) { 
	   if(e.getSource()==Input){
		   String str=JOptionPane.showInputDialog(this,"输入数字,用空格分隔","输入对话框",JOptionPane.PLAIN_MESSAGE);
	       if(str!=null) {
	    	   Scanner scanner = new Scanner(str);
	           double sum=0,average;
               String regex="\\D+";
               int k=0;
               while(scanner.hasNext()){
               try{
                  double number=scanner.nextDouble();
                  if(k==0)
                      output.append(""+number);
                  else
                     output.append("+"+number);
                  sum=sum+number;
                  k++;
               } 
             catch(InputMismatchException exp){
                String t=scanner.next();
             }
          }
           average=sum/k;
           output.append("="+sum+"\n"+"平均值是:"+average+"\n");
           String a[]=str.split(regex);
            int array[]=new int[a.length];
            array1=array;
           output.append("数组排序后是:");
           for(int j=0;j<a.length;j++)
        	   array[j]=Integer.parseInt(a[j]);
           Arrays.sort(array);
           output.append(Arrays.toString(array));
         
          }
	       
      }
	    if(e.getSource()==find){
		   String num=JOptionPane.showInputDialog(this,"输入查找的数据","查找",JOptionPane.PLAIN_MESSAGE);
		   int item = Integer.parseInt(num); 
		   for(int i=0;i<array1.length;i++) {
			   if(item==array1[i]) {
				   JOptionPane.showMessageDialog(this, "该数字存在数组中","查找", JOptionPane.PLAIN_MESSAGE);
			   }
		       else {
			       JOptionPane.showMessageDialog(this, "该数字不存在数组中","查找", JOptionPane.PLAIN_MESSAGE);
			 
		       }
              break;
		   }   
	    	  
		   }
	   }
		    }
   

2.并且将要求1查找数据的功能,用自定义对话框的形式显示出来如图
在这里插入图片描述
代码:

public class test {
   public static void main(String args[]) {
      WindowInput win=new WindowInput();
      win.setTitle("带输入对话框的窗口"); 
      win.setBounds(80,90,300,300);
   }
}
import java.awt.event.*; 
import java.awt.*;
import javax.swing.*;
import java.util.*;
public class WindowInput extends JFrame implements ActionListener {
   JTextArea output;
   JButton Input,find;
   MyDialog dialog;
   int array1[];
   WindowInput() {
       Input=new JButton("弹出输入对话框");
       find=new JButton("请输入要查找的数据");
       dialog=new MyDialog(this, "查找");
       dialog.setModal(true);
       output=new JTextArea();
       add(Input,BorderLayout.NORTH);
       add(find,BorderLayout.SOUTH);
       add(new JScrollPane(output),BorderLayout.CENTER);
       Input.addActionListener(this); 
       find.addActionListener(this);
       setVisible(true);
       setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
   }
   
   public void actionPerformed(ActionEvent e) { 
	   if(e.getSource()==Input){
		   String str=JOptionPane.showInputDialog(this,"输入数字,用空格分隔","输入对话框",JOptionPane.PLAIN_MESSAGE);
	       if(str!=null) {
	    	   Scanner scanner = new Scanner(str);
	           double sum=0,average;
               String regex="\\D+";
               int k=0;
               while(scanner.hasNext()){
               try{
                  double number=scanner.nextDouble();
                  if(k==0)
                      output.append(""+number);
                  else
                     output.append("+"+number);
                  sum=sum+number;
                  k++;
               } 
             catch(InputMismatchException exp){
                String t=scanner.next();
             }
          }
           average=sum/k;
           output.append("="+sum+"\n"+"平均值是:"+average+"\n");
           String a[]=str.split(regex);
            int array[]=new int[a.length];
            array1=array;
           output.append("数组排序后是:");
           for(int j=0;j<a.length;j++)
        	   array[j]=Integer.parseInt(a[j]);
           Arrays.sort(array);
           output.append(Arrays.toString(array));
         
          }
	       
      }
	   if(e.getSource()==find){
		   dialog.setVisible(true);
		   String num=dialog.getnum();
		   int item = Integer.parseInt(num); 
		 
		   for(int i=0;i<array1.length;i++) {
			   if(item==array1[i]) {
				   
				  dialog.add(new JLabel("该数字存在数组中"));
				  
			   }
		       else {
		    	   dialog.add(new JLabel("该数字不存在数组中"));
		    	  
			 
		       }
			   break; 
		   }   
	    	  
		   }
	   } 
	   

      
    }
import java.awt.event.*; 
import java.awt.*;
import javax.swing.*;
public class MyDialog extends JDialog implements ActionListener{
	 JButton find;
	 JTextField field;
	 String a;
	 MyDialog(JFrame f,String s){
		 super(f,s);
		 field=new JTextField(10);
		 field.addActionListener(this);
		 setLayout(new FlowLayout());
		 add(new JLabel("输入查找的数字"));
		 add(field);
		 setBounds(60,60,200,200);
		 setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
	 }
	@Override
	public void actionPerformed(ActionEvent arg0) {
		a=field.getText();
		setVisible(false);
		
		
	}
	public String getnum(){
		return a;
	}

}

发布了15 篇原创文章 · 获赞 1 · 访问量 323

猜你喜欢

转载自blog.csdn.net/z_mawkish/article/details/105185292
今日推荐