2011-03-06 82 views
2

如何將文本追加到java文本區域中的某些列。我將列設置爲2,但我想在1列上追加一個文本,在另一列上追加另一個文本。我怎麼做?將文本追加到java文本區域中的某些列

jTextArea1.append("\n"); 
jTextArea1.setColumns(2); 

jTextArea1.append("a"); 
jTextArea1.append("\n"); 
jTextArea1.append("b"); 

回答

1

儘管聽起來像setColumns設置了JTextArea上的文本列的數量,但它確實是在定義用於計算文本區域的首選大小的單字符列的數量。例如。如果您指定setColumns(80),則首選大小的計算結果至少爲80個字符(如果我記得正確計算爲使用'm'的八十倍)。

如果你想有多個文本列,你可以使用JTable作爲垃圾建議或並排使用兩個textareas(如果你把它們放在一個滾動窗格中,它們也將同時滾動)。

0
import javax.swing.*; 
import java.awt.*; 
import java.awt.event.*; 
import java.awt.event.ActionListener; 
import javax.swing.event.*; 
import javax.swing.ListSelectionModel; 


class Listfile extends JFrame { 

private String name_v; 
private int age; 
private JButton btn; 
private JTextArea reply; 
private JPanel pane; 
private JScrollPane scrollbar; 
public static void main(String[] args){ 
    String name = "lee-roy"; 
String password = "anointed23"; 

String body = "hi my name is: "; 
String body2 = "and this is my account im glad you could join in"; 
Listfile account1 = new Listfile(); 
account1.setListfile("Jamal", 19); 
account1.getListfile(); 
Listfile app = new Listfile(); 
} 
public Listfile(){ 
super("App chat log Gui"); 

setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
setVisible(true); 
    setSize(550, 600); 
pane = new JPanel(); 
reply = new JTextArea(10, 35); 
scrollbar = new JScrollPane(reply); 
btn = new JButton("Send"); 


reply.setLineWrap(true); 
reply.setWrapStyleWord(true); 
             scrollbar.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS); 
    add(pane); 
pane.add(scrollbar); 
    pane.add(btn);  
AreaHandler handle = new AreaHandler(); 
btn.addActionListener(handle); 
} 

class AreaHandler implements ActionListener{ 
public void actionPerformed(ActionEvent event){ 
if(event.getSource()==btn){ 
reply.append("Button has been clicked"); 
} 
} 
} 
public void setListfile(String name, int age_r){ 
name_v = name; 
age = age_r; 
} 

public void getListfile(){ 

JOptionPane.showMessageDialog(null, "Hi my name is " + name_v + " the discussion for to day is too sencetive so no viewers of under the age of " + age); 
} 
    } 
+0

感謝您試圖提供幫助,但是如何才能將_columnar text_設置爲該區域?換句話說:這不是對問題的回答...... – kleopatra 2013-08-14 08:45:29

相關問題