[英]Getting input from JTextArea in a JOptionPane
I was writing an editor that lets the user design a selection/bidding table. 我正在编写一个允许用户设计选择/出价表的编辑器。 Part of this program was the ability to add sub categories, which have a name and description.
该程序的一部分是添加子类别的能力,这些子类别具有名称和描述。 I have done more sophisticated things like this before but because i needed this done in a few days I decided to try and go with using a JOptionPane and passing in a JTextField for the name and a JTextArea for the possibility of a lengthy description.
之前,我已经做了类似的更复杂的事情,但是因为我需要几天的时间来完成这项工作,所以我决定尝试使用JOptionPane并传入JTextField作为名称,并传递JTextArea来进行冗长的描述。 Here is the code I have currently.
这是我目前拥有的代码。
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);
}
When i run this code, the OptionPane opens correctly and works fine with the JTextField, but the JTextArea will not show up at all. 当我运行此代码时,OptionPane可以正确打开并与JTextField正常工作,但JTextArea根本不会显示。 Is there any reason why this is?
有什么原因吗?
You don't add your textArea in any panel/dialog etc. that's why It doesn't show up on the screen. 您没有在任何面板/对话框等中添加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.