⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 display.java

📁 javaAPI程序
💻 JAVA
字号:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*; 

 



public    class  Display  extends JFrame{
 	
 
 	public static void main(String arge[])
{
    	Display frame = new Display();			
		frame.pack(); 		
      	frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      	frame.show();  
}

 	
	public Display(){		
		JComponent content = (JComponent)getContentPane();
		content.setBackground(Color.LIGHT_GRAY);	
		content.setLayout(new BoxLayout(content, BoxLayout.Y_AXIS)); 
 

//	content.add(Box.createVerticalStrut(12)); 
	
		content.add(new JLabel("Input Numbers ")); 
		tfield1 =new JTextField("-1 0 1 2 3",20);     	
		content.add(tfield1);

		
  		content.add(new JLabel(" "));
  
	
	 	JButton Buttonl= new JButton("Sort low to high ");
     	content.add(Buttonl);
       	lowTohighListener ll=new lowTohighListener();
      	Buttonl.addActionListener(ll);
 

 		 content.add(new JLabel(" "));
     
     	JButton Buttonh=new JButton("Sort high to low");
     	content.add(Buttonh);
    	 highTolowListener hh=new highTolowListener();
	 	Buttonh.addActionListener(hh);
		
  		content.add(new JLabel(" "));
		content.add(new JLabel("Output Numbers "));
		 
		tfield2 = new JTextField(" ",20);
		content.add(tfield2);
		tfield2.setText("^-^");					
  		content.add(new JLabel(" "));
		
		JButton Buttonclose=new JButton(" Exit Program ");
     	content.add(Buttonclose);
     	 close ccll=new close();
    	Buttonclose.addActionListener(ccll);

  }
   

  
   	private JTextField tfield1;
 	private JTextField tfield2;
 	private String text="";
     	/////////////////////////////////////////
     	
     	
     	
       class	lowTohighListener implements ActionListener 
     {public void actionPerformed(ActionEvent event)
     {	
     	try{
     	 	text=tfield1.getText();		 
     		String c="";
	 
			String b[]=text.split(",");
			int[] a=new int[b.length];
			for(int i=0;i<b.length;i++)
			{
				 b[i].trim();
		 
		//	if(b[i]==""){tfield2.setText("nonono");}
					
					for(int j=0;j<b[i].length();j++)
					{
		
					if((b[i].charAt(j)<48)||(b[i].charAt(j)>59))
						{ if(b[i].charAt(j)!=45)	
						
 						throw  new NotNumberException (b[i])  ;
 					}
 					
 							

 					}
 					 if(b[i]!=","){
				
				 a[i]=Integer.parseInt(b[i]);
			}
			}
			find.bubbleSort(a);
			for(int i=0;i<b.length;i++)
			{
				b[i]=Integer.toString(a[i]);
				 c+=b[i];				
				c+=" ";
				}
			//tfield2.setText(c);			
		}
		catch (NotNumberException x)
		 {
			 //	System.err.println("only number is need!");
				tfield2.setText("only number is need at: "+x.getMessage());
     	}
     } 
     
    }
     
     class  highTolowListener implements ActionListener 
     {public void actionPerformed(ActionEvent event)
		{ 
		try{
		
			text=tfield1.getText();
			String c="";
			String b[]=text.split(" ");
			int[] a=new int[b.length];
			for(int i=0;i<b.length;i++)
			{
			 b[i].trim();
			 tfield2.setText(b[i]);
		
					
			for(int j=0;j<b[i].length();j++){
				
				if((b[i].charAt(j)<48)||(b[i].charAt(j)>59))
				{ 	if(b[i].charAt(j)!=45)
 					throw  new NotNumberException (b[i])  ;
 					} 
 				}
				//a[i]=Integer.parseInt(b[i]);
				}
				find.bubbleSort(a);

				for(int i=b.length-1;i>=0;i--)
			{
				b[i]=Integer.toString(a[i]);
				c+=b[i];
				c+=" ";
	
			//	tfield2.setText(c);
			 }				
			}	 	
		catch (NotNumberException x)
		 {
			 	tfield2.setText("only number is need  at: "+x.getMessage());
			 	} 
			
					
			}
				}
					
  private    class close implements ActionListener
     {public void actionPerformed(ActionEvent event)
		{
			System.exit(0);}
		} 	
	
	}



class MyComponent extends JComponent{
public MyComponent(){

	super();}
	}
	
	class find {
		public static void bubbleSort(  int array2[] )
    {   
	for ( int pass = 1; pass < array2.length; pass++ )
	    {
		for ( int element = 0; element < (array2.length - 1); element++ )
		    {
			if ( array2[ element ] > array2[ element + 1 ] )  
			    {     
				swap( array2, element, element + 1 );
			    } 
		    }
	    } 
    }
    
    
    public static void swap( int array3[], int first, int second )
    {
        int hold;
        
        hold  = array3[ first ];
        array3[ first ]  = array3[ second ];
        array3[ second ] = hold;
	
    }
 

}


class NotNumberException extends Exception{
	public NotNumberException(String aa)
	{
		super(aa);
		}
 		}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -