簡體   English   中英

自定義對話框 java swing

[英]custom dialog java swing

我正在嘗試編寫一個自定義 SwingUtilities.InvokeandWait 事件,其中包含 Textarea 和 Button,因此一旦用戶將數據粘貼到 Textarea 並單擊按鈕。 直到那時控制不應該 go 下來,但不能使它正常工作。

我發現最好的方法是使用對話框消息,所以現在我嘗試在 InputDialogBox 中添加更大的 TextArea 而不是單行文本字段。 我還嘗試創建一個自定義對話框,但 InvokeandWait 甚至只是觸發對話框並轉到我不想要的下一行。

我需要專家的幫助

  1. 在 inputdialog(或)中添加 Textarea 而不是單行文本字段的方法
  2. 處理自定義對話框的方法,直到我按下其中的確定按鈕,然后控制轉到程序的下一行。

JDialog 就像 JFrame 一樣。 您可以添加任何您想要的組件。

此外,您不使用 invokeAndWait()。 只需將對話框設為模態,它就會按照您想要的方式工作。

創建自定義對話框的一個簡單示例 -

public class CustomDiaglogBox extends JFrame
{
    // Variables declaration
    private JLabel jLabel_Item;
    private JLabel jLabel_Value;
    public static JButton jButton_Add;
    private JPanel contentPane;
    public static JComboBox combo_item;
    public static JComboBox combo_value;
    public static JTextField text_Value;        
    public static JTextArea textArea_desc;      
    // End of variables declaration    

    public CustomDiaglogBox()
    {
        super();
        create();
        this.setVisible(true);
    }    

    private void create()
    {
        jLabel_Item = new JLabel();
        jLabel_Value = new JLabel();
        jLabel_Description = new JLabel();
        combo_value = new JComboBox();
        text_Value = new JTextField();          
        textArea_desc = new JTextArea(20,20);
        combo_item = new JComboBox(new String[]{""});
        combo_item.setSelectedIndex(-1);    
        jButton_Add = new JButton();
        contentPane = (JPanel)this.getContentPane();    
        //
        // jLabel1
        //
        jLabel_Item.setHorizontalAlignment(SwingConstants.LEFT);
        //jLabel_Item.setForeground(new Color(0, 0, 255));
        jLabel_Item.setText("Item");
        //
        // jLabel2
        //
        jLabel_Value.setHorizontalAlignment(SwingConstants.LEFT);
    //  jLabel_Value.setForeground(new Color(0, 0, 255));
        jLabel_Value.setText("Value");

        // jButton1
        //
        jButton_Add.setBackground(new Color(204, 204, 204));
        jButton_Add.setForeground(new Color(0, 0, 255));
        jButton_Add.setText("Add");
        jButton_Add.setEnabled(false);
        jButton_Add.addActionListener(new AddTagWidnowListener());      //
        // contentPane
        //
        contentPane.setLayout(null);
        contentPane.setBorder(BorderFactory.createEtchedBorder());
        contentPane.setBackground(Color.WHITE);
        addComponent(contentPane, jLabel_Item, 5,10,106,18);
        addComponent(contentPane, jLabel_Value, 5,47,97,18);
        addComponent(contentPane, new JLabel("Description"), 5,87,97,18);
        addComponent(contentPane, combo_item, 110,10,183,22);
        addComponent(contentPane, combo_value, 110,45,183,22);
        addComponent(contentPane, new JScrollPane(textArea_desc), 110,75,183,62);
        addComponent(contentPane, jButton_Add, 150,145,83,28);          
        this.setTitle("MY CUSTOM DIALOG");
        this.setLocation(new Point(276, 182));
        this.setSize(new Dimension(335, 221));
        this.setDefaultCloseOperation(WindowConstants.HIDE_ON_CLOSE);
        this.setResizable(false);
    }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM