我正在编写一个编辑器,让用户可以设计一个选择/出价表。该程序的一部分是能够添加具有名称和描述的子类别。我之前做过更复杂的事情,但因为我需要在几天内完成这项工作,所以我决定尝试使用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根本不会显示。这有什么原因吗?
答案 0 :(得分:4)
您不会在任何面板/对话框等中添加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);