자바 코드 swt 대화 상자

5695 단어 자바shelldialogSWT
import java.io.File;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.graphics.Font;
import org.eclipse.swt.graphics.FontData;
import org.eclipse.swt.graphics.RGB;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.printing.PrintDialog;
import org.eclipse.swt.printing.Printer;
import org.eclipse.swt.printing.PrinterData;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.ColorDialog;
import org.eclipse.swt.widgets.DirectoryDialog;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.FileDialog;
import org.eclipse.swt.widgets.FontDialog;
import org.eclipse.swt.widgets.MessageBox;
import org.eclipse.swt.widgets.Shell;
public class DialogSample {
public static void main(String[] args) {
   final Display display = new Display();
   final Shell shell = new Shell(display);
   shell.setLayout( new GridLayout());
   shell.setText("Dialog Sample");
  
   Button b1 = new Button ( shell,SWT.NONE);
   b1.setText("     ");
   b1.addSelectionListener( new SelectionAdapter(){
    public void widgetSelected(SelectionEvent e) {
     //       ,              
     MessageBox box = new MessageBox( shell ,SWT.ICON_ERROR|SWT.YES|SWT.NO);
     //        
     box.setText("       ");
     //          
     box.setMessage("        !");
     //     ,      choice
     int choice = box.open();
     //        
     if (choice==SWT.YES)
      System.out.print("Yes");
     else if ( choice==SWT.NO)
      System.out.print("No");
    }
   
   });
  
   Button b2 = new Button ( shell,SWT.NONE);
   b2.setText("       ");
   b2.addSelectionListener( new SelectionAdapter(){
    public void widgetSelected(SelectionEvent e) {
     DirectoryDialog dialog = new DirectoryDialog(shell);
     //              
     dialog.setMessage("           ");
     //        
     dialog.setText("      ");
     //            
     dialog.setFilterPath("C:\\");
     //    ,           
     String saveFile = dialog.open();
     if ( saveFile != null )
     {
      //    File  
      File directory = new File(saveFile);
      System.out.print(directory.getPath());
     }
    }
   });
  
   Button b3 = new Button ( shell,SWT.NONE);
   b3.setText("  open   ");
   b3.addSelectionListener( new SelectionAdapter(){
    public void widgetSelected(SelectionEvent e) {
     //         ,     SWT.OPEN,      SWT.SAVE、SWT.MULTI
     FileDialog dialog = new FileDialog(shell,SWT.OPEN);
     //         
     dialog.setFilterPath(System.getProperty("java.home"));
     //           
     dialog.setFilterExtensions(new String[] {"*.txt", "*.*"});
     //                
     dialog.setFilterNames( new String[]{"Text Files (*.txt)", "All Files (*.*)"});
     //    ,           
     String file = dialog.open();
     if ( file != null )
     {
      System.out.print(file);
     }
    }
   });
  
   Button b5 = new Button ( shell,SWT.NONE);
   b5.setText("  save   ");
   b5.addSelectionListener( new SelectionAdapter(){
    public void widgetSelected(SelectionEvent e) {
     //         ,     SWT.OPEN,      SWT.SAVE、SWT.MULTI
     FileDialog dialog = new FileDialog(shell,SWT.SAVE);
     //         
     dialog.setFilterPath(System.getProperty("java.home"));
     //           
     dialog.setFilterExtensions(new String[] {"*.txt", "*.*"});
     //                
     dialog.setFilterNames( new String[]{"Text Files (*.txt)", "All Files (*.*)"});
     //    ,           
     String file = dialog.open();
     if ( file != null )
     {
      System.out.print(file);
     }
    }
   });
   Button b4 = new Button ( shell,SWT.NONE);
   b4.setText("     ");
   b4.addSelectionListener( new SelectionAdapter(){
    public void widgetSelected(SelectionEvent e) {
     //         
     ColorDialog dialog = new ColorDialog(shell);
     //         
     dialog.setRGB( new RGB( 255 ,255 ,128));
     //     ,         rgb  
     RGB rgb = dialog.open();
     if ( rgb != null )
     {
      System.out.print(rgb);
      //      
      Color color = new Color( display , rgb );
      //         ,    
      color.dispose();
     
     }
    }
   });
  
   Button b55 = new Button ( shell,SWT.NONE);
   b55.setText("     ");
   b55.addSelectionListener( new SelectionAdapter(){
    public void widgetSelected(SelectionEvent e) {
     //         
     FontDialog dialog = new FontDialog (shell);
     //         
     dialog.setRGB( new RGB( 255 ,255 ,128));
     //     ,         fontData  
     FontData fontData = dialog.open();
     if ( fontData != null )
     {
      System.out.print(fontData);
      //      
      Font font = new Font( display , fontData );
      //         ,    
      font.dispose();
     }
    }
   });
  
   Button b6 = new Button ( shell,SWT.NONE);
   b6.setText("     ");
   b6.addSelectionListener( new SelectionAdapter(){
    public void widgetSelected(SelectionEvent e) {
     //         
     PrintDialog dialog = new PrintDialog (shell);
     //     ,         fontData  
     PrinterData printData = dialog.open();
     if ( printData != null )
     {
      //      
      Printer printer = new Printer( printData );
      //        ,    
      printer.dispose();
     }
    }
   });
  
   //shell.setSize(200, 150);
   shell.pack();
   shell.open();
   while (!shell.isDisposed()) {
    if (!display.readAndDispatch())
     display.sleep();
   }
   display.dispose();
}
}

좋은 웹페이지 즐겨찾기