import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import javax.swing.filechooser.*;

public class NotePadExam extends JFrame
{
 JFrame jf,jf2;
 JPanel jp1,jp2;
 JMenuBar jmb;
 JMenu jmFile;
 JMenuItem jmi_CreateNew,jmi_Open,jmi_Save,jmi_OtherSave,jmi_Exit;
 JLabel jlb1;
 JTextArea jta;
 JDialog jd;

 public NotePadExam()
 {
 super("제목없음");
 try
 {
 
  //this.setDefaultCloseOperation(EXIT_ON_CLOSE);
  //jf = new JFrame();
  jf = this;
  jf.getContentPane().setLayout(new FlowLayout());
  //jf.getContentPane().setLayout(new BorderLayout());
  //jf = new JFrame();
 
  jp1 = new JPanel();
  jp2 = new JPanel();
  //jp1.setLayout(new FlowLayout());
 
  jmb = new JMenuBar();
  /*//////////////
  파일메뉴추가
  /*//////////////
  jmFile = new JMenu("파일");

 

  //jm=createMenuItem(jm,"열기");
  //jm=createMenuItem(jm,"저장");
  //jm=createMenuItem(jm,"다른이름으로 저장");
  //jm=createMenuItem(jm,"끝내기");
 
 
  jta = new JTextArea();

  JScrollPane scrollPane = new JScrollPane();
 scrollPane.setPreferredSize(new Dimension(450,400));
 scrollPane.setBorder(BorderFactory.createTitledBorder(""));
 scrollPane.getViewport().add(jta, null);

 //jpanel.add(scrollPane);

 

  //jta.setSize(30,50);
 
  //jd = new JDialog(this,"yes");
  //jd.setLayout(new FlowLayout());
  //jd.add(new Button("저장"));
  //jd.setSize(100,100);
 


  //jd.setVisible(true);
  //새로 만들기 이벤트 생성
  makeNewDoc(createMenuItem(jmFile,"새로만들기"),jta);
  //문서열기
  openDoc(createMenuItem(jmFile,"열기"),jta);
  //메뉴바추가
  jmb.add(jmFile);
  /*//////////////
  파일메뉴추가
  /*//////////////

  jp1.add(jmb);

  //jmb.setSize(50,200);
  //텍스트창추가
  jp2.add(scrollPane);

  jf.add(jp1);
  jf.add(jp2);
 

  //add(jf); 
  //jf.getContentPane().add(jp1,BorderLayout.CENTER);
  //createDialogBOx(this);

  jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

 }
 catch (Exception ex)
 {
  ex.printStackTrace();
 }
 }

 public void openDoc(JMenuItem jmim,final JTextArea jtaa)
 {
  //문서열기 작업하기
 
  ActionListener al = new ActionListener()
  {
   public void actionPerformed(ActionEvent ae)
   {

    System.out.println("문서열기");

    String jtaaStr = jtaa.getText().trim();

    if(!jtaaStr.equals("") && jtaaStr != null)
    {
     System.out.println("널값 아님");
     //doOpenDialogForSave(jtaa);
     //기존파일이 있는 경우 저장창이 나온다.
    }
    else
    {
     //파일열기 대화상자를 열어야 한다.    
     NotePadJFileChooser dpjc = new NotePadJFileChooser("열기");
     int returnVal = dpjc.showOpenDialog(NotePadExam.this);
     if(returnVal == JFileChooser.APPROVE_OPTION)
     {
      File selectedFile = dpjc.getSelectedFile();
      FileInputStream fis=null;
   //FileOutputStream fos = null;
   BufferedInputStream dos = null;
      //StringBuffer strB = new StringBuffer();
      try
      {
       fis = new FileInputStream(selectedFile);   
    //dos = new BufferedInputStream(fis);
    byte[] buffer = new byte[512];
    int readcount = 0;
    //StringBuffer openedFileBuffer = new StringBuffer();
    String str = new String();
   
       while((readcount = fis.read(buffer)) != -1)
       {
  str+=new String(buffer,0,readcount,"EUC-KR");
  //System.out.println(str);
  
   //  for(int i=readcount;i<buffer
   // System.out.println(buffer.toString());
   //System.out.write(buffer,0,readcount);
   //openedFileBuffer.append(buffer,0,readcount);       
       }
   // System.out.println(openedFileBuffer);
  
    //jtaa.setText(openedFileBuffer);
    //BufferedInputStream bis = new BufferedInputStream(dos);
    System.out.println(str);
       jtaa.setText(str);
    jtaa.requestFocus();
    //jtta.setFocusable(true);
    //jtta.setCaretPosition(0);

      }
      catch (IOException ioe)
      {
       ioe.printStackTrace();
      }
      finally
      {
       try
       {
        fis.close();
       }
       catch (IOException e)
       {
       
       }
      
      }

     }
    }
   }
  };

  jmim.addActionListener(al);
 }
 public void makeNewDoc(JMenuItem jmim,final JTextArea jtaa)
 {
 //새파일 만들기 알고리즘////////////////////////////////////////////////////////////////////
 //새로만들기 메뉴아이템 이벤트 생성
 //1) 작업하던 문서가 없는 경우 바로 새문서 만듬///////////////////////////////////////////
 //2) 작업하던 문서가 있는 경우(저장 안했을 경우) 저장,저장안함,취소를 물어본후 1번 작업//
 //3) 작업하던 파일이 전과 비교했을 경우 변경된 게 없는 경우 이전 문서 닫고 새문서 만듬//
 ////////////////////////////////////////////////////////////////////////////////////////////
  
  ActionListener al = doOpenDialogForSave(jtaa);

  jmim.addActionListener(al);


 }
 
 public ActionListener doOpenDialogForSave(final JTextArea jtaa)
 {
  //return new ActionListener(){public void actionPerformed(ActionEvent ae){}};
 
  ActionListener al = new ActionListener()
  {
   public void actionPerformed(ActionEvent ae)
   {
    String str = jtaa.getText().trim();
    String msg[] = {"저장","저장안함","취소"};
    //일단 작업하던 문서가 있는 지 없는 지 체크함
   
    if(str.equals("") || str == null)
    {
     createNewDoc(jtaa);
    }
    else
    {   
     //System.out.println("dddd");
     int dialogResult =
     JOptionPane.showOptionDialog(
             NotePadExam.this,
           "변경된 내용을 저장하시겠습니까?",
           "새로만들기",
           JOptionPane.YES_NO_CANCEL_OPTION,
           JOptionPane.QUESTION_MESSAGE,
           null,msg,msg[0]
             );
    
     if(dialogResult == JOptionPane.YES_OPTION)   
      saveDoc(jtaa);//저장메소드
     else if(dialogResult == JOptionPane.NO_OPTION)//저장취소메소드
      createNewDoc(jtaa);//저장취소
     else //취소
      System.out.print("cancel");
     //저장하던 문서가 있는경우 저장,저장안함,취소 물어봄
    
    }
    //System.out.println("새로만들기 클릭했음");
   }
  };
  return al;
 }
 
 public void saveDoc(JTextArea jta)
 {
  NotePadJFileChooser jfChooser = new NotePadJFileChooser("저장");

  int returnVal = jfChooser.showOpenDialog(this);
  if(returnVal == JFileChooser.APPROVE_OPTION)
  {
   //System.out.println("You were choosen file what named " +jfChooser.getSelectedFile().getName());
   String savingFileName = jfChooser.getSelectedFile().getName();
   FileWriter fw = null;
   try
   {
    File choosenFileDirectory = new File(jfChooser.getCurrentDirectory().getAbsolutePath()+"\\"+savingFileName+".txt");

    //System.out.println(choosenFileDirectory.getAbsolutePath());
   
    fw = new FileWriter(choosenFileDirectory);
    fw.write(jta.getText());
    fw.close();
    this.setTitle(savingFileName);
   }
   catch (IOException ie)
   {
    ie.printStackTrace();
   }
  }
 
  //this.setTitle("안녕");//쓰불 타이틀 바꾼다고 몇시간 뻘짓 했넹..==;
  //StringBuffer strB = new StringBuffer(jta.getText());
 
 }
 /*
 public void fileWrite(InputStream in)
 {
  byte[] buffer = new byte[512];
  int readcount = 0;

  try
  {
   while((readcount = in.read(buffer)) != -1)
   {
    System.out.write(buffer,0,readcount);
   }
  }
  catch (IOException e)
  {
   System.out.println(e);
  }
 }
 */
 public void createNewDoc(JTextArea jtaa)
 {
//  if(jf==null)
//  setTitle("");
 
  //super.setTitle("새로만들기");
  //setTitle2("새로만들자");
  this.setTitle("제목없음");
  jtaa.setText("");

 }
 public JMenuItem createMenuItem(JMenu jmu,String name2)
 {
  //System.out.println("111111111");
  JMenuItem jmui = new JMenuItem(name2); 
  jmu.add(jmui);
  return jmui;
 }
 public static void main(String[] args)
 {
  NotePadExam npe = new NotePadExam();
  npe.setSize(500,500);
  npe.setVisible(true);

 //System.out.println("Hello World!");
 }
}
class NotePadJFileChooser extends JFileChooser
{
 //JFileChooser jfChooser;
 FileNameExtensionFilter filter;

 public NotePadJFileChooser(String buttonName)
 {
  super();
  filter =  new FileNameExtensionFilter(
          "텍스트 문서(*.txt)"
          ,"txt"
          );
  this.setFileFilter(filter);
  this.setApproveButtonText(buttonName);
  this.setApproveButtonToolTipText(buttonName);

 }
 /*
 JFileChooser jfChooser = new JFileChooser();
  FileNameExtensionFilter filter =
  jfChooser.setFileFilter(filter);
  jfChooser.setApproveButtonText("저장");
  jfChooser.setApproveButtonToolTipText("저장하기");
  int returnVal = jfChooser.showOpenDialog(this);
 */
}

'자바 > 메모장' 카테고리의 다른 글

java메모장만들기 2일차(2시간 작업)  (0) 2010.10.18
메모장 제 1일차(2시간작업)  (0) 2010.10.14


import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

public class NotePadExam extends JFrame
{
 JFrame jf,jf2;
 JPanel jp1,jp2;
 JMenuBar jmb;
 JMenu jmFile;
 JMenuItem jmi_CreateNew,jmi_Open,jmi_Save,jmi_OtherSave,jmi_Exit;
 JLabel jlb1;
 JTextArea jta;
 ActionListener al;
 JDialog jd;

 public NotePadExam()
 {
 //super("메모장");
 try
 {
  
  //this.setDefaultCloseOperation(EXIT_ON_CLOSE);
  //jf = new JFrame();
  jf = this; 
  jf.getContentPane().setLayout(new FlowLayout());
  //jf.getContentPane().setLayout(new BorderLayout());
  //jf = new JFrame();
  
  jp1 = new JPanel();
  jp2 = new JPanel();
  //jp1.setLayout(new FlowLayout());
  
  jmb = new JMenuBar();
  /*//////////////
  파일메뉴추가
  /*//////////////
  jmFile = new JMenu("파일");

  

  //jm=createMenuItem(jm,"열기");
  //jm=createMenuItem(jm,"저장");
  //jm=createMenuItem(jm,"다른이름으로 저장");
  //jm=createMenuItem(jm,"끝내기");
  
  
  jta = new JTextArea(10,25);
  //jta.setSize(30,50);
  
  //jd = new JDialog(this,"yes");
  //jd.setLayout(new FlowLayout());
  //jd.add(new Button("저장"));
  //jd.setSize(100,100);
  


  //jd.setVisible(true);
  //새로 만들기 이벤트 생성
  makeNewEvent(createMenuItem(jmFile,"새로만들기"),jta);

  //메뉴바추가
  jmb.add(jmFile);
  /*//////////////
  파일메뉴추가
  /*//////////////

  jp1.add(jmb);

  //jmb.setSize(50,200);
  //텍스트창추가
  jp2.add(jta);

  jf.add(jp1);
  jf.add(jp2);
  

  //add(jf);  
  //jf.getContentPane().add(jp1,BorderLayout.CENTER);
  //createDialogBOx(this);

  jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

 }
 catch (Exception ex)
 {
  ex.printStackTrace();
 }
 }

 public void makeNewEvent(JMenuItem jmim,final JTextArea jtaa)
 {
 //새파일 만들기 알고리즘////////////////////////////////////////////////////////////////////
 //새로만들기 메뉴아이템 이벤트 생성
 //1) 작업하던 문서가 없는 경우 바로 새문서 만듬///////////////////////////////////////////
 //2) 작업하던 문서가 있는 경우(저장 안했을 경우) 저장,저장안함,취소를 물어본후 1번 작업//
 //3) 작업하던 파일이 전과 비교했을 경우 변경된 게 없는 경우 이전 문서 닫고 새문서 만듬//
 ////////////////////////////////////////////////////////////////////////////////////////////
 al = new ActionListener()
 {
  public void actionPerformed(ActionEvent ae)
  {
   String msg[] = {"저장","저장안함","취소"};
   //일단 작업하던 문서가 있는 지 없는 지 체크함
   String str = jtaa.getText().trim();
   if(str.equals("") || str == null)
   {
    createNewDoc(jtaa);
   }
   else
   {
    //System.out.println("dddd");
    JOptionPane.showConfirmDialog(
            NotePadExam.this,
             "변경된 내용을 저장하시겠습니까?",
          "새로만들기",
             JOptionPane.YES_NO_CANCEL_OPTION,
          null,"저장","저장안함","취소"
            );
    //저장하던 문서가 있는경우 저장,저장안함,취소 물어봄
    
   }
   //System.out.println("새로만들기 클릭했음");
  }
 };

 jmim.addActionListener(al);


 }
 public static void createNewDoc(JTextArea jtaa)
 {
//  if(jf==null)
//  setTitle("");
  jtaa.setText("");

 }
 public JMenuItem createMenuItem(JMenu jmu,String name2)
 {
  //System.out.println("111111111");
  JMenuItem jmui = new JMenuItem(name2); 
  jmu.add(jmui);
  return jmui;
 }
 public static void main(String[] args)
 {
  NotePadExam npe = new NotePadExam();
  npe.setSize(300,300);
  npe.setVisible(true);

 //System.out.println("Hello World!");
 }
}

 

잘사용하시면, 간편하게 훌륭한 대화상자들을 만들어 낼수 있습니다.

끝부분에 커스터마이징 부분을 잘 보시길 바랍니다.

 

BEYOND THE BASICS OF JOPTIONPANE

The Swing component set in J2SE includes a special class for creating a panel to be placed in a popup window. You can use this panel to display a message to the user and to get a response from the user. The panel presents content in four areas, one each for an icon, message, input, and buttons. You don't need to use any of the areas, although you would typically present at least a message and a button.

The icon area is for the display of a javax.swing.Icon. The icon is meant to indicate the type of message being displayed. There are default icons provided by the specific look and feel you use. The default images for these icons are specified through a property setting to the look and feel. The property setting points to the appropriate resource for each message type:

  • OptionPane.informationIcon
  • OptionPane.warningIcon
  • OptionPane.errorIcon
  • OptionPane.questionIcon

You don't have to change the default properties, though you could with a call to UIManager.put(property name, new resource). With the defaults in place, you simply identify the appropriate message type with a constant of JOptionPane:

  • JOptionPane.PLAIN_MESSAGE
  • JOptionPane.INFORMATION_MESSAGE
  • JOptionPane.WARNING_MESSAGE
  • JOptionPane.ERROR_MESSAGE
  • JOptionPane.QUESTION_MESSAGE

The plain variety maps to no icon, while the others use a default icon from the look and feel.

The second area of the JOptionPane is the message area. This is typically used to display a text message. However, you could show any number of objects here. The message type determines how a message is displayed.

The input area is next. Here is where you can get a response to a message from the user. To prompt for a response you can use a free form text field, a combo box, or even a list control. For Yes/No type prompts, you would instead use the button area, described next.

Last is the button area, another response-like area. When a user selects a button it signals the end of usage for the JOptionPane. Default sets of button labels are available. (As is the case for icons, these too come from property settings, such as OptionPane.yesButtonText.) You can also provide your own button labels. You can display any number of buttons (including no buttons) with any set of labels. The predefined button label is localized for several languages: English (en), German (de), Spanish (es), French (fr), Italian (it), Japanese (ja), Korean (ko), Swedish (sv), and two varieties of Chinese (zh_CN / zh_TW). If you provide your own labels, you need to localize them yourself.

The predefined sets of buttons are defined by a set of JOptionPane constants:

  • DEFAULT_OPTION
  • YES_NO_OPTION
  • YES_NO_CANCEL_OPTION
  • OK_CANCEL_OPTION

Default maps to a single OK button.

Using JOptionPane

The JOptionPane class contains seven constructors, each returns a component that you can place anywhere. However, more typically you would use a factory method that creates the component and automatically places it inside a JDialog or JInternalFrame. There are eight of these factory methods (two varieties of four methods) that JOptionPane provides:

  • show[Internal]MessageDialog
  • show[Internal]ConfirmDialog
  • show[Internal]InputDialog
  • show[Internal]OptionDialog

The Internal variety creates a JOptionPane and shows it in a JInternalFrame. The other variety creates a JOptionPane and shows it in a JDialog. This tip only discusses the dialog variety.

The message dialog is meant to show a message that the user confirms by selecting the button (or closing the dialog). The message dialog is not meant to return a value.

In its simplest case, you would use code like the following to show a message dialog:

   JOptionPane.showMessageDialog(
      component, "Hello, World");

The system centers the dialog over the component argument.

There are two other variations for showing a message dialog. These allow you to customize the window title, customize the message type (to use the default icon), and set a customized icon:

  • showMessageDialog(Component parentComponent,
    Object message,
    String title,
    int messageType)
  • showMessageDialog(Component parentComponent,
    Object message,
    String title,
    int messageType,
    Icon icon)

The method for showing a confirm dialog has four variations:

  • showConfirmDialog(Component parentComponent,
    Object message)
  • showConfirmDialog(Component parentComponent,
    Object message,
    String title,
    int optionType)
  • showConfirmDialog(Component parentComponent,
    Object message,
    String title,
    int optionType,
    int messageType)
  • showConfirmDialog(Component parentComponent,
    Object message,
    String title,
    int optionType,
    int messageType,
    Icon icon)

The simplest variation brings up a dialog with a question icon. The dialog displays Select an Option as the title, has Yes, No, and Cancel as button labels.

   JOptionPane.showConfirmDialog(component, "Lunch?");

If you don't like the defaults, you can change them by using one of the other methods. You'll find that everything is changeable with JOptionPane.

Unlike the message dialog, the confirm dialog does require a return value. Here, you really do want to know which button the user selected. The confirm dialog returns an integer, indicated by one of the following constants of the JOptionPane class:

  • CANCEL_OPTION
  • CLOSED_OPTION (used when the used closed popup window)
  • NO_OPTION
  • OK_OPTION
  • YES_OPTION

Passing in a setting of OK_CANCEL_OPTION for the option type changes the buttons shown in the confirm dialog to OK and Cancel. Comparing the value returned to the constant, indicates which button the user selected.

Checking for the return value changes the one line above to quite a few more:

   int returnValue = JOptionPane.showConfirmDialog(
     component, "Lunch?");
   String message = null;
   if (returnValue == JOptionPane.YES_OPTION) {
     message = "Yes";
   } else if (returnValue == JOptionPane.NO_OPTION) {
     message = "No";
   } else if (returnValue == JOptionPane.CANCEL_OPTION) {
     message = "Cancel";
   } else if (returnValue == JOptionPane.CLOSED_OPTION) {
     message = "Closed";
   }
   System.out.println("User selected: " + message);

The input dialog method has six variations. Five return a String:

  • showInputDialog(Object message)
  • showInputDialog(Object message,
    Object initialSelectionValue)
  • showInputDialog(Component parentComponent,
    Object message)
  • showInputDialog(Component parentComponent,
    Object message,
    Object initialSelectionValue)
  • showInputDialog(Component parentComponent,
    Object message,
    String title,
    int messageType)

One of the variations returns an Object:

  • showInputDialog(Component parentComponent,
    Object message,
    String title,
    int messageType,
    Icon icon,
    Object[] selectionValues,
    Object initialSelectionValue)

The simplest variation shows the message in a question dialog. The dialog displays Input as the frame title, and has OK and Cancel buttons. Because there is no component provided, the frame is centered over the whole screen.

   String value = JOptionPane.showInputDialog("Name");

As is the case for other types, when you show the input dialog, you can set the parent component, frame title, message type, or icon. However the buttons are fixed at OK and Cancel. You can also set the initial value in the text field.

The last method variation for the input dialog is special. Instead of offering the user a text field to enter selections, you provide an array of objects (typically strings) from which to choose. Depending on how many values you provide, the look and feel will present either a JComboBox or JList for the user selections. Here's an example that uses this variation. Here an input dialog prompts a user to select a day of the week. The default is the last day of the week. The example uses a smallList for the days of the week.

   String smallList[] = {
     "Sunday",
     "Monday",
     "Tuesday",
     "Wednesday",
     "Thursday",
     "Friday",
     "Saturday"
   };
   String value = 
     (String)JOptionPane.showInputDialog(
       component, 
       "Which Day?", 
       "Day", 
       JOptionPane.QUESTION_MESSAGE, 
       null, // Use default icon
       smallList, 
       smallList[smallList.length-1]);
   System.out.println("Day: " + value);

For a larger list, the code looks essentially the same. Here the list of system properties are used as the choices.

   Object largeList[] = 
     System.getProperties().keySet().toArray();
   String value = 
     (String)JOptionPane.showInputDialog(
       component, 
       "Which Property?", 
       "Property", 
      JOptionPane.QUESTION_MESSAGE, 
       null, 
       largeList, 
       largeList[largeList.length-1]);


   System.out.println("Property: " + value);

The final dialog type is an all encompassing one, showOptionDialog. There is only one version of the method. Here, you can set everything:

  • showOptionDialog(Component parentComponent,
    Object message,
    String title,
    int optionType,
    int messageType,
    Icon icon,
    Object[] options,
    Object initialValue)

The only thing new here is the options array. This is how you can customize the set of available buttons. Notice that this is an Object array, not a String array. If the Object is a Component, that component is used in the dialog. This allows you to place icons in the buttons, for instance. More typically, you would provide an array of strings, and their labels would be used as the button labels. The initialValue is then used to select which of the options gets the default selection.

The showOptionDialog method returns an int. This indicates the position selected from the options array. CLOSED_OPTION is still returned if the user closed the dialog.

   String options[] = {"Yes", "Not Now", "Go Away"};
   int value = JOptionPane.showOptionDialog(
       component,
       "Lunch?",
       "Lunch Time",
       JOptionPane.YES_NO_OPTION, // Need something  
         JOptionPane.QUESTION_MESSAGE,
       null, // Use default icon for message type
       options,
       options[1]);
   if (value == JOptionPane.CLOSED_OPTION) {
     System.out.println("Closed window");
   } else {
     System.out.println("Selected: " + options[value]);
   }

If you don't want buttons, pass in an empty array for the options.

Adding Word Wrap

The JOptionPane component has a read-only property (MaxCharactersPerLineCount) for the maximum number of characters per line. By default, this is Integer.MAX_VALUE. By subclassing JOptionPane, you can override this setting. Changing this setting allows the component to word-wrap when a message is really long.

   public static JOptionPane getNarrowOptionPane(
                       int maxCharactersPerLineCount) {
     // Our inner class definition
     class NarrowOptionPane extends JOptionPane {
       int maxCharactersPerLineCount;
       NarrowOptionPane(int maxCharactersPerLineCount) {
         this.maxCharactersPerLineCount = 
           maxCharactersPerLineCount;
       }
       public int getMaxCharactersPerLineCount() {
         return maxCharactersPerLineCount;
       }
     }


     return new NarrowOptionPane(
       maxCharactersPerLineCount);
   }

By subclassing, you can no longer use the static helper methods such as showMessageDialog. Here's how you manually create and show the component:

   String msg = "This is a really long message. ...";
   JOptionPane pane = getNarrowOptionPane(50);
   pane.setMessage(msg);
   pane.setMessageType(JOptionPane.INFORMATION_MESSAGE);
   JDialog dialog = pane.createDialog(
     component, "Width 50");
   dialog.show();

Of course, you can add a "\n" to the message, but then you have to count the characters per line.

Message as Object

At this point you might ask why the message argument to all the showXXXDialog methods is an Object. The answer is that it's because the message argument to all these methods doesn't have to be a String. You can pass in any Object. Each object is "added" to the message area, one on top of the other. For instance, if you pass in an array of two strings, it creates a message on two lines:

   String msg[] = {"Welcome", "Home"};
   JOptionPane.showMessageDialog(component, msg);

The objects you add can be components, icons, or objects. Components are added as such. Icons are shown in a label, as are strings. For other objects, their string representation (toString()) is shown. For instance, the following component can be added to an option pane. As the user changes the selection in the slider, the option pane's value is updated:

   public static JSlider getSlider(
                        final JOptionPane optionPane) {
     JSlider slider = new JSlider();
     slider.setMajorTickSpacing (10);
     slider.setPaintTicks(true);
     slider.setPaintLabels(true);
     ChangeListener changeListener = 
       new ChangeListener() {
       public void stateChanged(
                   ChangeEvent changeEvent) {
         JSlider theSlider = 
             (JSlider)changeEvent.getSource();
         if (!theSlider.getValueIsAdjusting()) {
           optionPane.setInputValue(
             new Integer(theSlider.getValue()));
         }
       }
     };
     slider.addChangeListener(changeListener);
     return slider;
   }

Because you have to pass the option pane to the method, here again, you can't use one of the shortcuts to create the dialog:

   JOptionPane optionPane = new JOptionPane();
   JSlider slider = getSlider(optionPane);
   Object msg[] = {"Select a value:", slider};
   optionPane.setMessage(msg);
   optionPane.setMessageType(
     JOptionPane.QUESTION_MESSAGE);
   optionPane.setOptionType(
     JOptionPane.OK_CANCEL_OPTION);
   JDialog dialog = optionPane.createDialog(
       Options.this, "Select Value");
   dialog.show();
   Object value = optionPane.getValue();
   if (value == null || !(value instanceof Integer)) {
     System.out.println("Closed");
   } else {
     int i = ((Integer)value).intValue();
     if (i == JOptionPane.CLOSED_OPTION) {
       System.out.println("Closed");
     } else if (i == JOptionPane.OK_OPTION) {
       System.out.println("OKAY - value is: " +
                  optionPane.getInputValue());
     } else if (i == JOptionPane.CANCEL_OPTION) {
       System.out.println("Cancelled");
     }
   }

Notice that as you start getting fancier with JOptionPane, you lose the ability to use the shortcut methods. And, you have to do special handling of the return value. You first need to use getValue for the JOptionPane to determine which button the user selected. Then, for when the user presses the OK button, you need to use the getInputValue method to get the value from the underlying slider.

Sounds

Swing provides for auditory cues related to the four types of icons:

  • OptionPane.errorSound
  • OptionPane.informationSound
  • OptionPane.questionSound
  • OptionPane.warningSound

By setting these properties, you can get sounds when your option panes are displayed. You can set the individual sounds with lines like the following:

   UIManager.put("OptionPane.questionSound", 
     "sounds/OptionPaneError.wav");

Or set all of them with the system defaults as follows:

   UIManager.put("AuditoryCues.playList",
     UIManager.get("AuditoryCues.defaultCueList"));

Audio cues are disabled by default because they might have problems on some platforms. It is recommended that you use them with care until the issues have been resolved.

Complete Example

Here is the source code for a complete example that uses the features explored in this tip.

   import javax.swing.*;
   import javax.swing.event.*;
   import java.awt.*;
   import java.awt.event.*;
   import java.util.Locale;

   public class Options extends JFrame {
     private static class FrameShower
         implements Runnable {
       final Frame frame;
       public FrameShower(Frame frame) {
         this.frame = frame;
       }
       public void run() {
         frame.show();
       }
     }

     public static JOptionPane getNarrowOptionPane(
         int maxCharactersPerLineCount) {
       // Our inner class definition
       class NarrowOptionPane extends JOptionPane {
         int maxCharactersPerLineCount;
         NarrowOptionPane(
                   int maxCharactersPerLineCount) {
           this.maxCharactersPerLineCount = 
              maxCharactersPerLineCount;
     }
         public int getMaxCharactersPerLineCount() {
           return maxCharactersPerLineCount;
         }
       }

    return new NarrowOptionPane(
               maxCharactersPerLineCount);
  }

     public static JSlider getSlider(
                        final JOptionPane optionPane) {
       JSlider slider = new JSlider();
       slider.setMajorTickSpacing (10);
       slider.setPaintTicks(true);
       slider.setPaintLabels(true);
       ChangeListener changeListener = 
                            new ChangeListener() {
         public void stateChanged(
                           ChangeEvent changeEvent) {
           JSlider theSlider = (
             JSlider)changeEvent.getSource();
           if (!theSlider.getValueIsAdjusting()) {
             optionPane.setInputValue(new Integer(  
               theSlider.getValue()));
           }
         }
       };
       slider.addChangeListener(changeListener);
       return slider;
     }

     public Options() {
       super("JOptionPane Usage");
       setDefaultCloseOperation(EXIT_ON_CLOSE);
       Container contentPane = getContentPane();
       contentPane.setLayout(new FlowLayout());
       JButton message = new JButton("Message");
       ActionListener messageListener = 
                               new ActionListener() {
         public void actionPerformed(ActionEvent e) {
           JOptionPane.showMessageDialog(
             Options.this, "Hello, World");
         }
       };
       message.addActionListener(messageListener);
       contentPane.add(message);
       JButton confirm = new JButton("Confirm");  
       ActionListener confirmListener = 
        new ActionListener() {
         public void actionPerformed(ActionEvent e) {
           int returnValue = 
             JOptionPane.showConfirmDialog(
           Options.this, "Lunch?");
           String message = null;           
           if (returnValue == JOptionPane.YES_OPTION) {
             message = "Yes";
           } else if (
               returnValue == JOptionPane.NO_OPTION) {
             message = "No";
           } else if (
               returnValue == JOptionPane.CANCEL_OPTION) {
             message = "Cancel";
           } else if (
               returnValue == JOptionPane.CLOSED_OPTION) {
             message = "Closed";
           }
           System.out.println("User selected: " + message);
         }
       };
       confirm.addActionListener(confirmListener);
       contentPane.add(confirm);
       JButton inputText = new JButton("Input Text");
       ActionListener inputTextListener = 
                               new ActionListener() {
         public void actionPerformed(ActionEvent e) {
           String value = 
             JOptionPane.showInputDialog("Name");
           System.out.println("Name: " + value);
         }
       };
       inputText.addActionListener(inputTextListener);
       contentPane.add(inputText);
       JButton inputCombo = new JButton("Input Combo");
       ActionListener inputComboListener = 
                                 new ActionListener() {
         public void actionPerformed(ActionEvent e) {
           String smallList[] = {
             "Sunday",
             "Monday",
             "Tuesday",
             "Wednesday",
             "Thursday",
             "Friday",
             "Saturday"
           };
           String value = 
             (String)JOptionPane.showInputDialog(
             Options.this, "Which Day?", "Day", 
             JOptionPane.QUESTION_MESSAGE, null, 
             smallList, smallList[smallList.length-1]);
           System.out.println("Day: " + value);
         }
       };
       inputCombo.addActionListener(inputComboListener);
       contentPane.add(inputCombo);
       JButton inputList = new JButton("Input List");     
       ActionListener inputListListener = 
                              new ActionListener() {
        public void actionPerformed(ActionEvent e) {
           Object largeList[] = 
             System.getProperties().keySet().toArray();
           String value = 
             (String)JOptionPane.showInputDialog(
             Options.this, "Which Property?", "Property", 
             JOptionPane.QUESTION_MESSAGE, null, 
             largeList, largeList[largeList.length-1]);
             System.out.println("Property: " + value);
         }
       };              
       inputList.addActionListener(inputListListener);
       contentPane.add(inputList);
       JButton all = new JButton("All");
       ActionListener allListener = 
                               new ActionListener() {
         public void actionPerformed(ActionEvent e) {
           String options[] = 
             {"Yes", "Not Now", "Go Away"};
           int value = JOptionPane.showOptionDialog(
               Options.this,
               "Lunch?",
               "Lunch Time",
               JOptionPane.YES_NO_OPTION, 
               // Message type
               JOptionPane.QUESTION_MESSAGE,
               null, // Use default icon for message type
               options,
               options[1]);
           if (value == JOptionPane.CLOSED_OPTION) {
             System.out.println("Closed window");
           } else {
             System.out.println(
               "Selected: " + options[value]);
           }
         }
       };       
       all.addActionListener(allListener);
       contentPane.add(all);
       JButton wide = new JButton("Wide");
       ActionListener wideListener = 
                               new ActionListener() {
         public void actionPerformed(ActionEvent e) {
           String msg = 
             "This is a really long message. " + 
             "This is a really long message. " + 
             "This is a really long message. " + 
             "This is a really long message. " + 
             "This is a really long message. " +
             "This is a really long message.";
           JOptionPane pane = getNarrowOptionPane(50);
           pane.setMessage(msg);
           pane.setMessageType(
             JOptionPane.INFORMATION_MESSAGE);
           JDialog dialog = 
              pane.createDialog(Options.this, "Width 50");
           dialog.show();
         }
       };       
       wide.addActionListener(wideListener);
       contentPane.add(wide);
       JButton twoLine = new JButton("Two Line");
       ActionListener twoLineListener = 
                               new ActionListener() {
         public void actionPerformed(ActionEvent e) {
           String msg[] = {"Welcome", "Home"};
           JOptionPane.showMessageDialog(
             Options.this, msg);
         }
       };       
       twoLine.addActionListener(twoLineListener);
       contentPane.add(twoLine);
       JButton slider = new JButton("Slider");
       ActionListener sliderListener = 
                               new ActionListener() {
         public void actionPerformed(ActionEvent e) {
           JOptionPane optionPane = new JOptionPane();
           JSlider slider = getSlider(optionPane);
           Object msg[] = {"Select a value:", slider};
           optionPane.setMessage(msg);
           optionPane.setMessageType(
             JOptionPane.QUESTION_MESSAGE);
           optionPane.setOptionType(
             JOptionPane.OK_CANCEL_OPTION);
             JDialog dialog = optionPane.createDialog(
                Options.this, "Select Value");
           dialog.show();
           Object value = optionPane.getValue();
           if (value == null || !(value instanceof Integer)) {
             System.out.println("Closed");
           } else {
             int i = ((Integer)value).intValue();
             if (i == JOptionPane.CLOSED_OPTION) {
               System.out.println("Closed");
             } else if (i == JOptionPane.OK_OPTION) {
               System.out.println("OKAY - value is: " +
                          optionPane.getInputValue());
             } else if (i == JOptionPane.CANCEL_OPTION) {
               System.out.println("Cancelled");
             }
           }
         }
       };
       slider.addActionListener(sliderListener);
       contentPane.add(slider);
       setSize(300, 200);
     }
     
     public static void main(String args[]) {
       UIManager.put("AuditoryCues.playList",
         UIManager.get("AuditoryCues.defaultCueList"));
       JFrame frame = new Options();
       Runnable runner = new FrameShower(frame);
       EventQueue.invokeLater(runner);
     }
   }

For additional information about JOptionPane, see the How to Make Dialogs trail in The Java Tutorial and the javadoc for the JOptionPane class.


'자바 > 자바팁' 카테고리의 다른 글

클래스패스 설정  (0) 2010.10.25
Borderlayout 기본설명  (0) 2010.10.21
에코 클라이언트 과정  (0) 2010.10.21
JFrame 관련 팁  (0) 2010.10.18
action event 를 쓸때 컴파일시 inner class 에러 나는 경우  (0) 2010.10.18
★ Top-level component - JFrame ★ - Java/Servlet/JSP

2006/11/28 23:21

복사 http://blog.naver.com/airdasom/90011367478

JFrame
 

'자바 > 자바팁' 카테고리의 다른 글

클래스패스 설정  (0) 2010.10.25
Borderlayout 기본설명  (0) 2010.10.21
에코 클라이언트 과정  (0) 2010.10.21
JDialog 고급활용  (0) 2010.10.18
action event 를 쓸때 컴파일시 inner class 에러 나는 경우  (0) 2010.10.18

addActionListener() 호출시 사용한 new ActionListener() { ... } 부분이

anonymous inner class이고 asd()의 인자인 str을 여기로 고이 전달하려면

str을 final로 선언해야 한다는 얘깁니다.

아래와 같이 final 붙여주면 됩니다.

 

 

class asd extends Frame
{
 public asd(final String str){
  
  setSize(300,200);
  setVisible(true);
  Button bt = new Button("go");
  add(bt);
  bt.addActionListener(new ActionListener(){
   
   public void actionPerformed(ActionEvent e){
    System.out.println(str);
   }

  });

 }

 

'자바 > 자바팁' 카테고리의 다른 글

클래스패스 설정  (0) 2010.10.25
Borderlayout 기본설명  (0) 2010.10.21
에코 클라이언트 과정  (0) 2010.10.21
JDialog 고급활용  (0) 2010.10.18
JFrame 관련 팁  (0) 2010.10.18

//메모장 만들자

import javax.swing.*;
import java.awt.*;

public class NotePadExam extends JFrame
{
 JFrame jf;
 JMenuBar jmb;
 JMenu jm;
 JMenuItem jmt;
 JLabel jlb1;
 JTextArea jta;
 
 public NotePadExam()
 {
  super("메모장");
  try
  {

  setLayout(new FlowLayout());
  jmb = new JMenuBar();
  /*//////////////
  파일메뉴추가
  /*//////////////
  jm = new JMenu("파일");
  
  jm=createMenuItem(jm,"새로만들기");
  jm=createMenuItem(jm,"열기");
  jm=createMenuItem(jm,"저장");
  jm=createMenuItem(jm,"다른이름으로 저장");
  jm=createMenuItem(jm,"끝내기");
  
  jta = new JTextArea(200,200);
  
  //메뉴바추가
  jmb.add(jm);
  /*//////////////
  파일메뉴추가
  /*//////////////

  add(jmb);
  
  //jmb.setSize(50,200);
  //텍스트창추가
  add(jta);
   
  }
  catch (Exception ex)
  {
   System.out.println(ex);
  }
 }
 public JMenu createMenuItem(JMenu jmi,String name2)
 {
  //System.out.println("111111111");
  JMenuItem jmui = new JMenuItem(name2);  
  jmi.add(jmui);
  return jmi;
 }
 public static void main(String[] args)
 {
  NotePadExam npe = new NotePadExam();
  npe.setSize(300,300);
  npe.setVisible(true);

  //System.out.println("Hello World!");
 }
}

awt로 된 완전 초보 계산기입니다..
웹개발만 하다보니 기초자바가 재미있네영..^^;;

import java.awt.*;
import java.awt.event.*;

public class CalcCreateExam extends Frame
{
 Frame f;
 Panel pl_0,pl_1;
 MenuBar mb;
 Menu m1,m2,m3;
 MenuItem menu_exit;
 Button b1,b2,b3,b4,b5,b6,b7,b8,b9,b0;
 Button bs,ce,bc;
 Button bmc,bmr,bms,bmplus;
 Button bslush,bsq,bminus,bplus;
 Button bresult;
 TextField result_tf;
 // button_al;
 int result_number = 0;
 char ch,privous_ch;
 String str_Result = "";
 String privous_number = "";
 String non_check_number = "";
 String final_result_number = "";

 public CalcCreateExam()
 {
  
  f= new Frame("자바계산기");
  //main = new Panel(new GridLayout());

  pl_0 = new Panel(new GridLayout());
  mb = new MenuBar();
  f.setMenuBar(mb);
  m1=new Menu("편집");
  m2=new Menu("보기");
  m3=new Menu("도움말");

  menu_exit = new MenuItem("나가기");

  mb.add(m1);
  mb.add(m2);
  mb.add(m3);

  m1.add(menu_exit);

  result_tf = new TextField();
  //result_tf.setAlignment();
  //System.out.println();
  
  pl_0.add(result_tf);

  pl_1 = new Panel(new GridLayout(4,4));
  //f = new Frame();
  //result_tf = new TextField(10);
  b1 = new Button("1");
  b2 = new Button("2");
  b3 = new Button("3");
  b4 = new Button("4");
  b5 = new Button("5");
  b6 = new Button("6");
  b7 = new Button("7");
  b8 = new Button("8");
  b9 = new Button("9");
  b0 = new Button("0");
  bc = new Button("C");
  
  bplus = new Button("+");
  bminus = new Button("-");
  bslush = new Button("/");
  bsq = new Button("*");
  //bplus = new Button("+");
  //bplus = new Button("+");
  bresult = new Button("=");//결과버튼
    
  pl_1.add(b9);
  pl_1.add(b8);
  pl_1.add(b7);
  pl_1.add(b6);
  pl_1.add(b5);
  pl_1.add(b4);
  pl_1.add(b3);
  pl_1.add(b2);
  pl_1.add(b1);
  pl_1.add(b0);
  pl_1.add(bplus);
  pl_1.add(bminus);
  pl_1.add(bslush);
  pl_1.add(bsq);
  pl_1.add(bc);
  pl_1.add(bresult);
  
  //pl.setLayout(new GridLayout());
  f.add(pl_1,BorderLayout.CENTER);
  f.add(pl_0,BorderLayout.NORTH);

  //f.add(main);
  
  //이벤트 구현

  ActionListener button_al = new ActionListener()
  {
   public void actionPerformed(ActionEvent ae)
   {
    //System.out.println(ae.getSource().getText());   
    
    
    non_check_number = (String)ae.getActionCommand();

    if(non_check_number.length()==1)
    {
     ch = non_check_number.charAt(0);
     if(Character.isDigit(ch))//숫자인지판단
     {
      str_Result += non_check_number;
      final_result_number = str_Result;
      //System.out.println("!!!!!!!!!!! ++++ : "+str_Result);
     }
     else
     {
      
      switch(ch)
      {       
       case '=':
        Caculate(privous_ch);
        break;
       case 'C':
        privous_number = "";
        str_Result = "";
        //privous_math=new char('');
        non_check_number = "";
        //System.out.println("c");
        break;       
       default :
        privous_ch=ch;
        break;
      }
     
     privous_number = str_Result;
     final_result_number = non_check_number;
     str_Result = "";//초기화
     }    
    }
    
       
   
   //System.out.println("결과창 : " + non_check_number);
   result_tf.setText(final_result_number);
   }

  };//ActionListener
  
  b1.addActionListener(button_al);
  b2.addActionListener(button_al);
  b3.addActionListener(button_al);
  b4.addActionListener(button_al);
  b5.addActionListener(button_al);
  b6.addActionListener(button_al);
  b7.addActionListener(button_al);
  b8.addActionListener(button_al);
  b9.addActionListener(button_al);
  b0.addActionListener(button_al);  
  bresult.addActionListener(button_al);
  bplus.addActionListener(button_al);
  bminus.addActionListener(button_al);
  bslush.addActionListener(button_al);
  bsq.addActionListener(button_al);
  bc.addActionListener(button_al);  
  menu_exit.addActionListener(new ActionListener()
  {
   public void actionPerformed(ActionEvent ae)
   {
    System.exit(0);
   }
  });
 }
 public void Caculate(Character ch1)
 {
  //System.out.println("111111111,"+privous_number+","+str_Result);
  int number1 = Integer.parseInt(privous_number);
  int number2 = Integer.parseInt(str_Result);
  //System.out.println("22222222,"+number1+":"+number2);

  switch(ch1)
  {
   case '+' :
    number1+=number2;
    //System.out.println("result : "+number1);
    break;
   case '-' :
    number1-=number2;
    break;
   case '*' :
    number1*=number2;
    break;
   case '/' :
    number1/=number2;
    break;
   
   //default :
    //System.out.println(result_tf.getText());
  }
  //텍스트창에서 안나옴..==;
  non_check_number = Integer.toString(number1);
  
  //System.out.println(number1+math+number2);
 }
 public void CreateFrame()
 {
  
  f.addWindowListener(new WindowAdapter() 
        {
          public void windowClosing(WindowEvent e) 
          {
              System.exit(0);
              
           }
         });

  f.setSize(300,250);
  f.setVisible(true);
  
 }
 public static void main(String[] args)
 {
  CalcCreateExam cce = new CalcCreateExam();
  cce.CreateFrame();
 }
}

완전 허접하다...하지만 조금 더 좋아질 것 같다..ㅋㅋ

+ Recent posts