Java 将文本设置为JTextPane无效

Java 将文本设置为JTextPane无效,java,swing,jtextpane,Java,Swing,Jtextpane,我正在尝试将Person类的toString()方法添加到GUI: public GUI_Persons() { setBounds(100, 100, 450, 300); contentPane = new JPanel(); contentPane.setBorder(new EmptyBorder(5, 5, 5, 5)); setContentPane(contentPane); contentPane.setLayout(null);

我正在尝试将Person类的toString()方法添加到GUI:

public GUI_Persons() {
    setBounds(100, 100, 450, 300);
    contentPane = new JPanel();
    contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
    setContentPane(contentPane);
    contentPane.setLayout(null);

    JTextPane textPane = new JTextPane();
    textPane.setBounds(0, 38, 434, 223);

    for(Person person : Access.getAllPersons()) {
        textPane.setText(textPane.getText() + person.toString() + "\n");
    }

    contentPane.add(textPane);

    JLabel lblAllPersons = new JLabel("All persons information");
    lblAllPersons.setBounds(10, 11, 188, 16);
    contentPane.add(lblAllPersons);
}
不幸的是,只显示一个空白文本字段。有人知道为什么文本不会设置为JTextPane组件吗?我还使用方法run,因为如果用户在另一个帧中点击“查看人员”按钮,则会显示此帧:

public static void run() {
    try {
        GUI_Persons frame = new GUI_Persons();
        frame.setVisible(true);
    } catch (Exception e) {
        e.printStackTrace();
    }
}
这是toString()方法:


提前感谢您提供的所有信息。

您好,svb您的代码运行良好,如果我更改了使用Person的循环,我没有的类,并将其放入真实的类中:

 public class GUI_Persons extends JFrame {
    public GUI_Persons() {
        setBounds(100, 100, 450, 300);
        JPanel contentPane = new JPanel();
        contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
        setContentPane(contentPane);
        contentPane.setLayout(null);

        JTextPane textPane = new JTextPane();
        textPane.setBounds(0, 38, 434, 223);

        for (int i = 0; i < 100; i++) {
            textPane.setText(textPane.getText() + personToString(i) + "\n");
        }

        contentPane.add(textPane);

        JLabel lblAllPersons = new JLabel("All persons information");
        lblAllPersons.setBounds(10, 11, 188, 16);
        contentPane.add(lblAllPersons);
    }

    public static String personToString(int i) {
        int id = i;
        String name = "name" + i;
        String forename = "forename" + i;
        String adr = "AAA";
        return "ID: " + id + ", Type: " + ", Name: " + name + ", Forename: "
                + forename + ", Adress: " + adr.toString();
    }
    public static void main(String[] args) {

        try {
            GUI_Persons frame = new GUI_Persons();
            frame.setVisible(true);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
公共类GUI\u扩展JFrame{
公众人士(){
立根(100100450300);
JPanel contentPane=新的JPanel();
setboorder(新的EmptyBorder(5,5,5,5));
setContentPane(contentPane);
contentPane.setLayout(null);
JTextPane textPane=新的JTextPane();
textPane.setBounds(0,38434223);
对于(int i=0;i<100;i++){
textPane.setText(textPane.getText()+personToString(i)+“\n”);
}
添加(文本窗格);
JLabel lblAllPersons=新JLabel(“所有人员信息”);
lblAllPersons.setBounds(10,11,188,16);
contentPane.add(lblAllPersons);
}
公共静态字符串personToString(int i){
int id=i;
String name=“name”+i;
字符串forename=“forename”+i;
字符串adr=“AAA”;
返回“ID:+ID+”,类型:“+”,名称:“+Name+”,名字:
+名字+”,地址:“+adr.toString();
}
公共静态void main(字符串[]args){
试一试{
GUI_Persons frame=新的GUI_Persons();
frame.setVisible(true);
}捕获(例外e){
e、 printStackTrace();
}
}
}
你可以自己试试,这样就不会有挥杆问题了。。。
很奇怪。。。只需验证代码的其他部分,或者请提供一些可执行文件。

1)Java GUI必须在不同的操作系统、屏幕大小、屏幕分辨率等上工作,在不同的地区使用不同的PLAF。因此,它们不利于像素完美布局。而是使用布局管理器,或与布局填充和边框一起使用。2) 为了更快地获得更好的帮助,请发布或。。。3) “在另一个框架中”请参见(Person-Person:Access.getAllPersons()){textPane.setText(textPane.getText()+Person.toString()+“\n”);}只有最后一个
Person才会出现。你能找出原因吗?请贴一张你从运行代码中得到的图片。
 public class GUI_Persons extends JFrame {
    public GUI_Persons() {
        setBounds(100, 100, 450, 300);
        JPanel contentPane = new JPanel();
        contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
        setContentPane(contentPane);
        contentPane.setLayout(null);

        JTextPane textPane = new JTextPane();
        textPane.setBounds(0, 38, 434, 223);

        for (int i = 0; i < 100; i++) {
            textPane.setText(textPane.getText() + personToString(i) + "\n");
        }

        contentPane.add(textPane);

        JLabel lblAllPersons = new JLabel("All persons information");
        lblAllPersons.setBounds(10, 11, 188, 16);
        contentPane.add(lblAllPersons);
    }

    public static String personToString(int i) {
        int id = i;
        String name = "name" + i;
        String forename = "forename" + i;
        String adr = "AAA";
        return "ID: " + id + ", Type: " + ", Name: " + name + ", Forename: "
                + forename + ", Adress: " + adr.toString();
    }
    public static void main(String[] args) {

        try {
            GUI_Persons frame = new GUI_Persons();
            frame.setVisible(true);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}