簡體   English   中英

在JOptionPane中從JTextArea獲取輸入

[英]Getting input from JTextArea in a JOptionPane

我正在編寫一個允許用戶設計選擇/出價表的編輯器。 該程序的一部分是添加子類別的能力,這些子類別具有名稱和描述。 之前,我已經做了類似的更復雜的事情,但是因為我需要幾天的時間來完成這項工作,所以我決定嘗試使用JOptionPane並傳入JTextField作為名稱,並傳遞JTextArea來進行冗長的描述。 這是我目前擁有的代碼。

    JPanel mainPanel = new JPanel( new GridBagLayout());
    mainPanel.setBorder( new TitledBorder("Set Creation Pane") );

    JTextField setNameField = new JTextField( 20 );
    JLabel dialogLabel1, dialogLabel2;
    dialogLabel1 = new JLabel("Create a new set called");

    if(possibleSuperSetName == null || possibleSuperSetName.length() == 0)
    {   
        dialogLabel2 = new JLabel("at a global level");
    }
    else
    {
        dialogLabel2 = new JLabel("that is a subset to "+possibleSuperSetName);
    }

    JLabel description = new JLabel("Description for set");
    JTextArea textArea = new JTextArea("Testing Testing 123");
    textArea.setColumns(80);
    textArea.setRows(10);
    textArea.setFont( new Font( Font.MONOSPACED, Font.PLAIN, textArea.getFont().getSize() ) );
    textArea.setEditable( true );
    textArea.setLineWrap(true);
    textArea.setWrapStyleWord( true );

    GridBagConstraints c = new GridBagConstraints();
    c.anchor = GridBagConstraints.CENTER;
    c.gridx = 0;
    c.gridy = 0;
    mainPanel.add( dialogLabel1, c );
    c.gridx = 1;
    mainPanel.add( setNameField, c );
    c.gridx = 2;
    mainPanel.add( dialogLabel2, c );
    c.gridx = 0;
    c.gridy = 1;
    mainPanel.add(description, c);
    c.gridy = 2;
    c.gridwidth = 3;
    mainPanel.add(description, c);

    int option = JOptionPane.showConfirmDialog( null,
                                              mainPanel,
                                              "Set Creation",
                                              JOptionPane.OK_CANCEL_OPTION );

    if( option == JOptionPane.OK_OPTION )
    {
        ItemSet newSet = new ItemSet();
        newSet.setSetName(setNameField.getText());
        newSet.setDescription(textArea.getText());
        caller.addSet(newSet);
    }       

當我運行此代碼時,OptionPane可以正確打開並與JTextField正常工作,但JTextArea根本不會顯示。 有什么原因嗎?

您沒有在任何面板/對話框等中添加textArea,這就是為什么它不顯示在屏幕上的原因。

    c.gridx = 0;
    c.gridy = 1;
    mainPanel.add(description, c);
    c.gridy = 2;
    c.gridwidth = 3;
    //change it mainPanel.add(description, c);
    mainPanel.add(textArea, c);

暫無
暫無

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

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