格式化JTextPane

时间:2017-12-21 22:10:39

标签: java swing

我试图让JFrame看起来不错但是我不能让它看起来不错。

我有以下代码

import javax.swing.*;

public class ReportGUI {
    private JFrame mainFrame;
    private JTextPane report;

    public static Product[] products = {
            new Product("Guardians of the Galaxy Vol. 2", "CD123455", 28), new Product("Kingsman 2: The Golden Circle", "CD545155", 13), new Product("Cars 3", "CD425245", 24), new Product("The Polar Express", "CD252454", 14), new Product("Harry Potter", "CD452412", 23)
    };

    public ReportGUI() {
        prepareGUI();
    }

    public void prepareGUI() {
        mainFrame = new JFrame("Report All Products");
        mainFrame.setSize(800, 500);
        report = new JTextPane();
        String txt = String.format("%50s%50s%5s\n", "Product Name", "Serial Number", "Price");
        for (Product product : products) {
            txt += String.format("%50s\t%50s\t$%10.2f\n", product.getName().trim(), product.getSerial().trim(), product.getPrice());
        }
        report.setText(txt);
        mainFrame.add(report);
        mainFrame.setVisible(true);
    }

    public static void main(String[] args) {
        new ReportGUI();
    }
}

这是Product.java

public class Product() {
    private String name, serial;
    private double price;

    public Product(String name, String serial, double price) {
        this.name = name;
        this.serial = serial;
        this.price = price;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getSerial() {
        return serial;
    }

    public void setSerial(String serial) {
        this.serial = serial;
    }

    public double getPrice() {
        return price;
    }

    public void setPrice(float price) {
        this.price = price;
    }

    @Override
    public String toString() {
        return "Product{" +
                "name='" + name + '\'' +
                ", serial='" + serial + '\'' +
                ", price=" + price +
                '}';
    }
}

我想看起来像个漂亮的桌子。

我怎么能让它工作?我一直试图让这看起来好几个小时,我无法理解。

任何帮助都会很棒。

1 个答案:

答案 0 :(得分:1)

改为使用Jtable

public void prepareGUI() {
        mainFrame = new JFrame("Report All Products");
        mainFrame.setSize(800, 500);

        // create column names
        String[] columnNames = {"Product Name", "Serial Number", "Price"};

        // create data
        Object[][] data = new Object[products.length][];
        for (int x = 0; x < products.length; x++){
            Product product = products[x];
            data[x] = new Object[]{product.getName(), product.getSerial(), product.getPrice()};
        }

        // initialize jtable with data and columnNames
        JTable table = new JTable(data, columnNames);

        // attach table to scrollpane
        JScrollPane scrollPane = new JScrollPane(table);
        table.setFillsViewportHeight(true);

        // add scrollpane to frame
        mainFrame.add(scrollPane);
        mainFrame.setVisible(true);
    }