2015-09-11 2 views
2

Я создаю небольшое всплывающее окно. У меня есть 2 ввода, которые являются JTextField и JTextArea. Я добавил JScrollPane в JTextArea. Когда я запускаю программу, просматривается scrollpane. Но я не мог использовать его. Я разделяю образ, как он выглядит. Я думаю, что я должен добавить высоту, но я не знаю, что высотаJScrollPane не работает с JTextArea

enter image description here

public class AddCommentDialog extends JDialog implements ShowableDialog, ActionListener { 

    private static final long serialVersionUID = 1L; 
    private static JTextField inputTitle = new JTextField(); 
    private static JTextArea inputComment = new JTextArea(); 
    private Rectangle rectangle = new Rectangle(); 
    private JLabel lblTitle = new JLabel("Başlık: "); 
    private JLabel lblComment = new JLabel("Yorum: "); 
    private JButton buttonSave = new JButton(" Kaydet "); 
    private JButton buttonCancel = new JButton(" İptal "); 

    // Kullanıcı tarafından girilen başlık ve yorum 
    private String title, comment; 


    /** 
    * The constructor that sets the fields and the dialog. 
    */ 
    public AddCommentDialog() { 

     super(ImageViewerGui.getMainFrame(), "Yorum Ekle", true); 

     JPanel panel = new JPanel(new BorderLayout(5, 5)); 
     // the input text fields 
     JPanel northContainer = new JPanel(new BorderLayout(7, 7)); 

     JPanel labelPanel = new JPanel(new BorderLayout(5, 5)); 
     JPanel fieldPanel = new JPanel(new BorderLayout(5, 5)); 

     // the buttons 
     JPanel buttonPanel = new JPanel(new FlowLayout(FlowLayout.RIGHT)); 

     buttonSave.addActionListener(this); 
     buttonCancel.addActionListener(this); 
     inputTitle.setPreferredSize(new Dimension(250, 25)); 
     inputComment.setPreferredSize(new Dimension(250, 75)); 
     buttonSave.setPreferredSize(new Dimension(90, 25)); 
     buttonCancel.setPreferredSize(new Dimension(90, 25)); 

     // Satır bitince alt satıra inmesi için 
     inputComment.setLineWrap(true); 
     JScrollPane scrollCommentArea = new JScrollPane(inputComment); 
     scrollCommentArea.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS); 

     try { 

      labelPanel.add(lblTitle, BorderLayout.NORTH); 
      labelPanel.add(lblComment, BorderLayout.CENTER); 

      fieldPanel.add(inputTitle, BorderLayout.NORTH); 
      fieldPanel.add(scrollCommentArea, BorderLayout.CENTER); 
      buttonPanel.add(buttonCancel); 
      buttonPanel.add(buttonSave); 

      northContainer.add(labelPanel, BorderLayout.WEST); 
      northContainer.add(fieldPanel, BorderLayout.CENTER); 
      panel.add(northContainer, BorderLayout.NORTH); 
      panel.add(buttonPanel, BorderLayout.SOUTH); 
      panel.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5)); 
      inputTitle.setText(""); 
      inputComment.setText(""); 
      add(panel); 
      this.setResizable(true); 

     } catch (Exception e) { 
      e.printStackTrace(); 
     } 
    } 
+2

'статическую 'собирается потенциальная проблема, но' inputComment.setPreferredSize (новое измерение (250, 75)); 'определенно является частью вашей основной проблемы. Вместо этого используйте 'setRows' и' setColumns'. См. Также [Следует ли мне избегать использования методов Set (Preferred | Maximum | Minimum) в Java Swing?] (Http://stackoverflow.com/questions/7229226/should-i-avoid-the-use-of-setpreferredmaximumminimumsize -methods-in-java-swi) – MadProgrammer

ответ

4

static собирается к потенциальной проблеме, но inputComment.setPreferredSize(new Dimension(250, 75));, безусловно, часть вашей основной проблемы. Вместо этого используйте setRows и setColumns.

inputComment.setRows(4); 
inputComment.setColumns(38); 

Основная проблема, JScrollPane зависит от компонентов предпочтительного размера, чтобы определить, должен ли он показать полосы прокрутки или нет (или если они на самом деле сделать что-нибудь)

Смотрите также Should I avoid the use of set(Preferred|Maximum|Minimum)Size methods in Java Swing?

+0

Да, я использую setRows и setColumns вместо setPreferredSize, тогда он работает. Спасибо! – Dauezevy

Смежные вопросы