2015-11-18 3 views
-4

Я пытаюсь установить текст по умолчанию в JTextArea. Я пробовал .setText, но это не работает. Может быть, я неправильно это кодирую?По умолчанию текст в JTextArea

Код выглядит следующим образом:

package RootOfFunctionX; 

    import java.awt.BorderLayout; 
    import java.awt.Dimension; 
    import java.awt.FlowLayout; 
    import java.awt.Font; 
    import java.awt.event.ActionEvent; 
    import java.awt.event.ActionListener; 
    import javax.swing.JButton; 
    import javax.swing.JFrame; 
    import javax.swing.JPanel; 
    import javax.swing.JScrollPane; 
    import javax.swing.JTextArea; 

    public class BisectionIterations extends JFrame 
    implements ActionListener { 

private JTextArea textArea = new JTextArea("This text should display"); 
private JScrollPane scrollPane = new JScrollPane(textArea); 
private JButton closeBtn = new JButton("Close"); 
//Array 
Double[] iterationBi = new Double[1000]; 

public BisectionIterations(Double[] iter) { 
    this.iterationBi = iter; 
    setLayout(new BorderLayout()); 
    setSize(500, 400); 
    setTitle("Bisection Method Iterations"); 
    setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE); 
    setLocationRelativeTo(null); 
    setResizable(false); 

    JPanel middle = new JPanel(new FlowLayout()); 
    middle.add(scrollPane); 
    textArea.setEditable(false); 
    textArea.setFont(new Font(Font.MONOSPACED, Font.PLAIN, 12)); 
    textArea.setLineWrap(true); 
    textArea.setWrapStyleWord(true); 
    scrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED); 
    scrollPane.setPreferredSize(new Dimension(490, 330)); 
    add("Center", middle); 

    JPanel bottom = new JPanel(new FlowLayout()); 
    middle.add(closeBtn); 
    closeBtn.addActionListener(this); 
    add("South", bottom); 
    displayIterations(); 

    setVisible(true); 
} 

public void actionPerformed(ActionEvent e) { 
    if (e.getSource() == closeBtn) { 
     dispose(); 
    } 
} 

public void displayIterations() { 
    String j = ""; 
    for (int i = 1; i < 999; i++) { 
     if (iterationBi[i] == null) { 

     } else { 
      j += "Approximation for iteration '" + i + "' = " + iterationBi[i] + "\n"; 
     } 
    } 
    textArea.setText(j); 
} 

}

+2

'JTextArea TEXTAREA = новый JTextArea («default_text_here»);' –

+0

I попробовали это, и он не отображает – Stefan

+0

. Тогда у вас есть но, но вы не показываете нам код, вызывающий ошибку. Создайте и опубликуйте свой [mcve] (пожалуйста, прочитайте ссылку). Прежде чем вы подумаете, что мы хотим видеть весь ваш код или небольшие фрагменты, мы этого не делаем, снова прочитайте ссылку. –

ответ

1

Это устанавливает текст по умолчанию. Я сделал это как рев.

public class BisectionIterations extends JFrame 
implements ActionListener { 

private JTextArea textArea = new JTextArea("This text should display"); 
private JScrollPane scrollPane = new JScrollPane(textArea); 
private JButton closeBtn = new JButton("Close"); 
//Array 
Double[] iterationBi = new Double[1000]; 

public BisectionIterations(Double[] iter) { 
this.iterationBi = iter; 
setLayout(new BorderLayout()); 
setSize(500, 400); 
setTitle("Bisection Method Iterations"); 
setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE); 
setLocationRelativeTo(null); 
setResizable(false); 

JPanel middle = new JPanel(new FlowLayout()); 
middle.add(scrollPane); 
textArea.setEditable(false); 
textArea.setFont(new Font(Font.MONOSPACED, Font.PLAIN, 12)); 
textArea.setLineWrap(true); 
textArea.setWrapStyleWord(true); 
scrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED); 
scrollPane.setPreferredSize(new Dimension(490, 330)); 
add("Center", middle); 

JPanel bottom = new JPanel(new FlowLayout()); 
middle.add(closeBtn); 
closeBtn.addActionListener(this); 
add("South", bottom); 
displayIterations(); 

setVisible(true); 
} 

public void actionPerformed(ActionEvent e) { 
if (e.getSource() == closeBtn) { 
    dispose(); 
} 
} 

public void displayIterations() { 
String j = ""; 
for (int i = 1; i < 999; i++) { 
    if (iterationBi[i] == null) { 

    } else { 
     j += "Approximation for iteration '" + i + "' = " +iterationBi[i] + "\n"; 
    } 
} 
//textArea.setText(j); 
} 

public static void main(String args[]){ 
Double s[]; 
s = new Double[1000]; 
for(int i=0;i<1000;i++) { 
    s[i]=(double)i; 
} 
BisectionIterations b=new BisectionIterations(s); 
} 
} 

Теперь, если удалить комментарий //textArea.setText(j); Он перезапишет установленный по умолчанию текст.

Это на самом деле установка текста по умолчанию, но вы банкнота в состоянии видеть, что, поскольку он перезаписывается приближению для итерации ......

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