2011-03-06 103 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