简体   繁体   中英

Default text in JTextArea

I am trying to set default text in a JTextArea. I have tried the .setText but that doesn't seem to work. Maybe I am coding it wrong?

The code is as follows:

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);
}

}

It is setting default text. I've done it as bellow.

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);
}
}

Now if I remove comment //textArea.setText(j); It will overwrite the default text we've set.

It is actually setting default text but you are note able to see that since it is overwritten by Approximation for iteration......

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM