-3

我的程序应该在 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

4

1 回答 1

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);
    }

}
于 2013-10-17T10:21:07.037 回答