2013-06-25 76 views
2

他們如何設計JFileChooser?兩個JFrame之間的揮杆信息交換模式

JFileChooser chooser = new JFileChooser(); 
int returnVal = chooser.showOpenDialog(this); 
if(returnVal == JFileChooser.APPROVE_OPTION) { 
    System.out.println("You chose to open this file: " + 
       chooser.getSelectedFile().getName()); 

我該如何設計這樣一個信息交流的框架。例如,我有Frame1和Frame2。 Frame1打開Frame2。 Frame2有一個JTextArea,我將在其中設置一些文本並對其進行編輯。在按下frame2中的OK按鈕後,它關閉,我想讓Frame1中的變量中的文本。

或者說如果我想做一個字體選擇器對話框。

JOptionPane不適合我。在frame2中,我將有一個HTML編輯器。在frame1中,我有JTable。點擊表格中的一行將打開帶有HTML編輯器的frame2。我正在使用SHEF來達到這個目的。當我按下OK/Save按鈕關閉frame2時,我需要frame1中的html文本String。並相應地設置行內容。但是frame2可以是一個模態對話框。

回答

4

開始由具有通過The Use of Multiple JFrames: Good or Bad Practice?

讀取然後具有How to make dialogs讀取。

JFileChooser是一個組件,它也有一個顯示對話框的方法。您的需求可能會有所不同,但它不看你的組件到開始需要在對話框

更新

你可以使用一個JOptionPane

一直顯示它不是一個壞的模式enter image description here

import java.awt.EventQueue; 
import javax.swing.JOptionPane; 
import javax.swing.JTextField; 
import javax.swing.UIManager; 
import javax.swing.UnsupportedLookAndFeelException; 

public class TestOptionPane12 { 

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

    public TestOptionPane12() { 
     EventQueue.invokeLater(new Runnable() { 
      @Override 
      public void run() { 
       try { 
        UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName()); 
       } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) { 
       } 

       JTextField field = new JTextField(); 
       int option = JOptionPane.showConfirmDialog(null, field, "Fill it out", JOptionPane.OK_CANCEL_OPTION, JOptionPane.QUESTION_MESSAGE); 
       switch (option) { 
        case JOptionPane.OK_OPTION: 
         System.out.println("You entered " + field.getText()); 
         break; 
       } 

      } 

     }); 
    } 

} 

或者你可以創建更多的自定義解決方案......

enter image description here

import java.awt.BorderLayout; 
import java.awt.Component; 
import java.awt.EventQueue; 
import java.awt.GridBagLayout; 
import java.awt.Window; 
import java.awt.event.ActionEvent; 
import java.awt.event.ActionListener; 
import javax.swing.JButton; 
import javax.swing.JDialog; 
import javax.swing.JOptionPane; 
import javax.swing.JPanel; 
import javax.swing.JTextField; 
import javax.swing.SwingUtilities; 
import javax.swing.UIManager; 
import javax.swing.UnsupportedLookAndFeelException; 

public class TestOptionPane12 { 

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

    public TestOptionPane12() { 
     EventQueue.invokeLater(new Runnable() { 
      @Override 
      public void run() { 
       try { 
        UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName()); 
       } catch (ClassNotFoundException ex) { 
       } catch (InstantiationException ex) { 
       } catch (IllegalAccessException ex) { 
       } catch (UnsupportedLookAndFeelException ex) { 
       } 

       FieldsPane pane = new FieldsPane(); 
       switch (pane.showDialog(null)) { 
        case JOptionPane.OK_OPTION: 
         String text = pane.getText(); 
         System.out.println("You entered: " + text); 
         break; 
       }     
      } 
     });  
    } 

    protected class FieldsPane extends JPanel { 

     private JTextField field; 
     private int state = JOptionPane.CANCEL_OPTION; 

     public FieldsPane() { 
      setLayout(new GridBagLayout()); 
      field = new JTextField(10); 
      add(field); 
     } 

     public String getText() { 
      return field.getText(); 
     } 

     public int showDialog(Component parent) { 

      JButton btnOkay = new JButton("Ok"); 
      JButton btnCancel = new JButton("Cancel"); 
      JPanel buttons = new JPanel(); 
      buttons.add(btnOkay); 
      buttons.add(btnCancel); 

      btnOkay.addActionListener(new ActionListener() { 
       @Override 
       public void actionPerformed(ActionEvent e) { 
        state = JOptionPane.OK_OPTION; 
        Window win = SwingUtilities.getWindowAncestor((Component)e.getSource()); 
        win.dispose(); 
       } 
      }); 
      btnCancel.addActionListener(new ActionListener() { 
       @Override 
       public void actionPerformed(ActionEvent e) { 
        state = JOptionPane.CANCEL_OPTION; 
        Window win = SwingUtilities.getWindowAncestor((Component)e.getSource()); 
        win.dispose(); 
       } 
      }); 

      JDialog dialog = new JDialog(parent == null ? (Window)null : SwingUtilities.getWindowAncestor(parent), "Fill it out"); 
      dialog.setModal(true); 
      dialog.add(this); 
      dialog.add(buttons, BorderLayout.SOUTH); 
      dialog.pack(); 
      dialog.setLocationRelativeTo(null); 
      dialog.setVisible(true); 

      return state;    
     }   
    }  
} 

更新,其中的JOptionPane和JEditorPane的例子

enter image description here

import java.awt.BorderLayout; 
import java.awt.Component; 
import java.awt.Dimension; 
import java.awt.EventQueue; 
import java.awt.GridBagLayout; 
import java.awt.Window; 
import java.awt.event.ActionEvent; 
import java.awt.event.ActionListener; 
import javax.swing.JButton; 
import javax.swing.JDialog; 
import javax.swing.JEditorPane; 
import javax.swing.JOptionPane; 
import javax.swing.JPanel; 
import javax.swing.JScrollPane; 
import javax.swing.JTextField; 
import javax.swing.SwingUtilities; 
import javax.swing.UIManager; 
import javax.swing.UnsupportedLookAndFeelException; 

public class TestOptionPane12 { 

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

    public TestOptionPane12() { 
     EventQueue.invokeLater(new Runnable() { 
      @Override 
      public void run() { 
       try { 
        UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName()); 
       } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) { 
       } 

       JEditorPane editorPane = new JEditorPane("text/html", null); 
       JScrollPane scrollPane = new JScrollPane(editorPane); 
       scrollPane.setPreferredSize(new Dimension(200, 200)); 
       int option = JOptionPane.showConfirmDialog(null, scrollPane, "Fill it out", JOptionPane.OK_CANCEL_OPTION, -1); 
       switch (option) { 
        case JOptionPane.OK_OPTION: 
         System.out.println("You entered " + editorPane.getText()); 
         break; 
       } 

      } 

     }); 
    } 
} 
+1

同時考慮在無模式對話框中觀察者模式,見[這裏](http://stackoverflow.com/a/11832979/230513)。 – trashgod

+0

我想要有JDialog。但仍然需要一個模式來處理OK,Cancel或其他事件,並獲取我的Frame1類中的文本或其他更多對象。 – karim

+0

從創建組件開始,可能使用'JPanel',將textfield添加到它。提供一個getter方法來獲取文本。創建一個方法來創建一個對話框 – MadProgrammer

0

您可以創建專班,將持有的結果。例如:

public class Result { 
    private String result; 

    public void setResult(String result) { ... } 

    public String getResult() { ... } 
} 

在第一幀中創建此類的實例並將其傳遞給第二幀。一旦關閉第二幀設置結果,然後第一幀就可以得到它。

+0

我明白了。但JFileChooser方法對我來說似乎更簡潔和容易。 – karim

0

讓它多對話代替框架,然後讀了有關調解模式:

http://blue-walrus.com/2013/06/mediator-pattern-in-swing/

你的基本問題是,你有分量的層次結構和組件上的一個分支想要與另一個遙遠的分支上的組件進行通信。您需要某種中介對象來在這些遙遠的分支之間進行通信。

0

看看"Event Driven Programming":不是組件之間的緊密耦合(每個組件必須知道每個其他組件),您可以發送事件並且組件可以響應它們。