如何将文本附加到java文本区域中的某些列。我已将列设置为2,但我想在1列上添加一个文本,在另一列上添加另一个文本。我该怎么做?
jTextArea1.append("\n");
jTextArea1.setColumns(2);
jTextArea1.append("a");
jTextArea1.append("\n");
jTextArea1.append("b");
答案 0 :(得分:2)
JTable
可能是柱状数据的更好选择。
答案 1 :(得分:1)
虽然听起来setColumns
设置了JTextArea
上的文本列数,但实际上它确定了用于计算文本区域首选大小的单字符列数。例如。如果指定setColumns(80)
,则首选大小计算为至少80个字符宽(如果我记得正确计算为'm'的八十倍)。
如果您想要多个文本列,可以使用JTable
作为trashgod建议使用或并排使用两个textareas(如果您将两者放在滚动窗格中,它们也会同时滚动)。
答案 2 :(得分: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);
}
}