私のプログラムは、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