GUI表输出

时间:2013-10-17 09:11:04

标签: java swing jtable

我的程序应该在GUI中提供以下输出

item |  profit  |   weight   |     Unit Price      |  Take weight
0         41.0       0.0         Infinity               1.0
1         43.0       0.0         Infinity               1.0
2         28.0       8.0         3.5                    1.0
3         46.0       52.0        0.8846153846153846     1.0
4         64.0       74.0        0.8648648648648649     0.6891891891891891
5         42.0       76.0        0.5526315789473685     0.0
6         45.0       100.0       0.45                   0.0
7         9.0        68.0        0.1323529411764706     0.0
8         9.0        75.0        0.12                   0.0
9         8.0        85.0        0.09411764705882353    0.0

(请忽略单价)

这是我的节目,但我的gui根本没有出现

import java.awt.FlowLayout;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;

public class GreedyKnapsack extends JPanel {
    DefaultTableModel model;
    JTable table;
    String col[] = {"item","profit", "weight", "  Take weight"}; 
    double[] profit;
    double[] weight;
    double[] take;

    public GreedyKnapsack(int n) {

        profit = new double[n];
        weight = new double[n];
        take = new double[n];
        for (int i = 0; i < n; i++) {
            profit[i] = (int) Math.round(Math.random() * 100);
            weight[i] = (int) Math.round(Math.random() * 100);
        }
    }

    public void unitPriceOrder() {
        for (int i = 0; i < profit.length; i++) {
            for (int j = 1; j < (profit.length - i); j++) {
                double x=profit[j - 1] / weight[j - 1];
                double y=profit[j] / weight[j];
                if (x <=y) {
                    double temp = profit[j - 1];
                    profit[j - 1] = profit[j];
                    profit[j] = temp;

                    double temp1 = weight[j - 1];
                    weight[j - 1] = weight[j];
                    weight[j] = temp1;
                }
            }
        }
    }

    public void Knapsack(int m) {
        unitPriceOrder();
        int j;
        for (j = 0; j < profit.length; j++) {
            take[j] = 0;
        }
        double total = m;
        for (j = 0; j < profit.length; j++) {
            if (weight[j] <= total) {
                take[j] = 1.00;
                total = total - weight[j];
            } else {
                break;// to exit the for-loop
            }
        }
        if (j < profit.length) {
            take[j] = (double)(total / weight[j]);
        }       
    }


    public void print(int x)
    {

         model = new DefaultTableModel(col,x); 
         table=new JTable(model){
            };
        JScrollPane pane = new JScrollPane(table);

        for (int i = 0; i < x ; i++)
        {
            table.setValueAt(i,i,0);
            table.setValueAt(profit[i],i,1);
            table.setValueAt(weight[i],i,2);
            table.setValueAt(take[i],i,3);
        }   
        add(pane);
        setVisible(true);
        setSize(500,400);
        setLayout(new FlowLayout());

    }

    public static void main(String args[]) {

        String q = JOptionPane.showInputDialog("Enter number of items");
    int x = Integer.parseInt(q);
        GreedyKnapsack G = new GreedyKnapsack(x);
        String m = JOptionPane.showInputDialog("Please enter bag size");
    int y = Integer.parseInt(m);
        G.Knapsack(y);
        G.print(x);

    }
}

这个程序是我从GUI获取帮助的地方,基本上我的gui输出是基于那里的工作

import java.awt.FlowLayout;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;

public class JTableUsage extends JFrame {

    DefaultTableModel model;
    JTable table;
    String col[] = {"Name", "Address", "Phone"};

    public static void main(String args[]) {
        new JTableUsage().start();
    }

    public void start() {
        model = new DefaultTableModel(col, 2);
        table = new JTable(model) {
            @Override
            public boolean isCellEditable(int arg0, int arg1) {

                return false;
            }
        };
        JScrollPane pane = new JScrollPane(table);
        table.setValueAt("csanuragjain", 0, 0);
        add(pane);
        setVisible(true);
        setSize(500, 400);
        setLayout(new FlowLayout());
        setDefaultCloseOperation(EXIT_ON_CLOSE);
    }
}

了解详情:http://mrbool.com/making-a-jtable-in-swing-using-java/24918#ixzz2hy5LM9ou

1 个答案:

答案 0 :(得分:1)

你实际上并没有展示任何东西。我已经重新设计了你的课程以证明它有效(不考虑你在unitPriceOrder()方法中使用冒泡排序这一事实?)。我也不会在您的main方法

中关注您的操作
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;

public class GreedyKnapsack {

    private JPanel panel;
    private JFrame frame;
    private DefaultTableModel model;
    private JTable table;
    private String col[] = { "item", "profit", "weight", "  Take weight" };
    private double[] profit;
    private double[] weight;
    private double[] take;

    public GreedyKnapsack(final int n) {
        profit = new double[n];
        weight = new double[n];
        take = new double[n];
        for (int i = 0; i < n; i++) {
            profit[i] = (int) Math.round(Math.random() * 100);
            weight[i] = (int) Math.round(Math.random() * 100);
        }
    }

    public void unitPriceOrder() {
        for (int i = 0; i < profit.length; i++) {
            for (int j = 1; j < (profit.length - i); j++) {
                double x = profit[j - 1] / weight[j - 1];
                double y = profit[j] / weight[j];
                if (x <= y) {
                    double temp = profit[j - 1];
                    profit[j - 1] = profit[j];
                    profit[j] = temp;
                    double temp1 = weight[j - 1];
                    weight[j - 1] = weight[j];
                    weight[j] = temp1;
                }
            }
        }
    }

    public void Knapsack(final int m) {
        unitPriceOrder();
        int j;
        for (j = 0; j < profit.length; j++) {
            take[j] = 0;
        }
        double total = m;
        for (j = 0; j < profit.length; j++) {
            if (weight[j] <= total) {
                take[j] = 1.00;
                total = total - weight[j];
            } else {
                break;
            }
        }
        if (j < profit.length) {
            take[j] = total / weight[j];
        }
    }

    public void displayTable(final int x) {
        this.model = new DefaultTableModel(col, x);
        this.table = new JTable(model);
        JScrollPane pane = new JScrollPane(table);
        for (int i = 0; i < x; i++) {
            table.setValueAt(i, i, 0);
            table.setValueAt(profit[i], i, 1);
            table.setValueAt(weight[i], i, 2);
            table.setValueAt(take[i], i, 3);
        }
        panel = new JPanel();
        panel.add(pane);
        frame = new JFrame();
        frame.setSize(500, 400);
        frame.setContentPane(panel);
        frame.pack();
        frame.setVisible(true);
    }

    public static void main(final String args[]) {
        int items = Integer.parseInt(JOptionPane.showInputDialog("Enter number of items"));
        GreedyKnapsack greedyKnapsack = new GreedyKnapsack(items);
        int packSize = Integer.parseInt(JOptionPane.showInputDialog("Please enter bag size"));
        greedyKnapsack.Knapsack(packSize);
        greedyKnapsack.displayTable(items);
    }

}