2011-05-06 26 views
0

可能重複:
Simple popup java form with at least two fields如何添加到Java中的對話框(Swing)?

我試圖讓具有多個按鈕,地方輸入文本對話框,等我使用本教程 - http://download.oracle.com/javase/tutorial/uiswing/components/dialog.html,但它沒有關於有多個項目的任何東西。有沒有辦法做到這一點?

+2

該教程的第一頁顯示了一個包含單選按鈕,選項卡和按鈕的對話框。那是不是「有多個項目」? – 2011-05-06 01:24:33

+0

只有按下「顯示」按鈕,對話框纔會顯示出來。顯示的唯一對話框是之前顯示的較簡單的對話框。 – Travis 2011-05-06 01:36:07

+0

這裏是[一](http://stackoverflow.com/questions/3002787/simple-popup-java-form-with-at-least-two-fields/3002830#3002830)與更多的項目,但你必須運行它看到他們。 :-) – trashgod 2011-05-06 01:36:32

回答

2

許多showXXXXDialog方法需要一個對象作爲參數之一。如果該對象是一個字符串,它將顯示一個字符串。如果該對象是包含按鈕,輸入字段和其他小部件的容器,則會顯示該對象。

引述http://download.oracle.com/javase/6/docs/api/javax/swing/JOptionPane.html

Parameters: 
The parameters to these methods follow consistent patterns: 


parentComponent 

定義是成爲該對話框的父組件。它用 兩種方式使用: 包含它的幀用作對話框的父親 ,其 屏幕座標用於對話框的 位置。在一般情況下,對話框在組件下方僅放置 。此參數 可能爲空,在這種情況下,將使用默認的 幀作爲父級,並且 對話框將在屏幕 (取決於L & F)的中心。

message 

要放置在對話框中的描述性消息。在最常見的 用法中,消息只是一個字符串或 字符串常量。但是, 這個參數的類型實際上是Object。其 解釋取決於其類型:

對象[] 對象數組被解釋爲一系列佈置在垂直疊層消息(每 對象)。 解釋是遞歸的 - 數組中的每個對象是根據其類型解釋的 。

組件 的組件顯示在對話框中。

圖標 該Icon被包裝在JLabel並在對話框中顯示。

他人 該目的是通過調用其toString方法被轉換爲字符串。結果被包裝在JLabel中並顯示。

+0

+1不錯的答案。 – Boro 2011-05-06 09:47:46

1

你這裏有兩種選擇。 要麼使用JOptionPane方法來顯示對話框,詳見Paul Tomblin或構建您自己的對話框。

第二個選擇是必要的,如果你是一個對話框,例如的細粒度控制後如果你需要不同的名字(這可以通過使用JOptionPane.showOptionDialog來完成)或者按鈕在對話框上的位置,或者如果你需要一個非模態對話框。

簡單的例子:

import java.awt.Color; 
import javax.swing.JDialog; 
import javax.swing.JFrame; 
import javax.swing.JLabel; 
import javax.swing.JOptionPane; 
import javax.swing.JPanel; 
import javax.swing.JTextField; 
import javax.swing.SwingUtilities; 

public class DialogsTest 
{ 
    public static void main(String[] args) 
    { 
     SwingUtilities.invokeLater(new Runnable() 
     { 
      @Override 
      public void run() 
      {    
       JPanel p = new JPanel(); 
       JPanel contentPane = new JPanel(); 
       contentPane.add(p); 
       JFrame f = new JFrame(); 
       f.setContentPane(contentPane); 
       f.setSize(400, 300); 
       f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
       f.setVisible(true);   
       /* 
       * NOTE: It is not recomended to share the same instance of a component 
       * by different parents. Thought it is fine here since the first 
       * dialog will release it before the second will get it. 
       * But in a situation when we wouldn't make the 'dialog' modal and 
       * we would show it after the 'option pane dialog' it would be empty. 
       */ 
       JPanel message = new JPanel(); 
       message.add(new JLabel("Label:"));    
       message.add(new JTextField("ABCD")); 
       message.setBackground(Color.GREEN); 
       JOptionPane.showConfirmDialog(f, message, "Default made dialog", JOptionPane.YES_NO_OPTION); 

       Object[] options = new String[]{"a", "b", "c"}; 
       JOptionPane.showOptionDialog(f, message, "", JOptionPane.YES_OPTION, JOptionPane.INFORMATION_MESSAGE, 
         null, options, options[0]); 
       JDialog dialog = new JDialog(f, "Custom made dialog"); 
       dialog.setModal(true); 
       dialog.setContentPane(message); 
       dialog.pack(); 
       dialog.setLocationRelativeTo(f); 
       dialog.setVisible(true); 
      } 
     }); 
    } 
} 

順便說一句,你在你閱讀Java教程一個非常不錯的(也許太豐富)的例子。

相關問題