/**
* @author cnzx219
* @email cnzx219@qq.com
* @date 2011-12-22
* All rights reserved.
* Copyright 2011 cnzx219
**/
package course.design10;
import java.awt.EventQueue;
import javax.swing.*;
import javax.swing.border.*;
import course.design10.algorithm2.*;
import course.design10.listener.AddGoodsDialog;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.util.Vector;
public class TestBpackWindow extends JFrame {
/**
*
*/
private TestBpackWindow mainWindow;
private static final long serialVersionUID = 636190675810780397L;
private JPanel contentPane;
private JButton btnTotal;
private JList listWeight;
private int totalWeight = 10;
private Vector<Goods> weight;
private Vector<Vector<Goods>> result;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
TestBpackWindow frame = new TestBpackWindow();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the frame.
*/
public TestBpackWindow() {
setTitle("\u80CC\u5305\u95EE\u9898\u7684\u6C42\u89E3");
mainWindow = this;
weight = new Vector<Goods>();
weight.add(new Goods("Keys", 1));
weight.add(new Goods("MacBook Air", 8));
weight.add(new Goods("Minor", 4));
weight.add(new Goods("Purse", 3));
weight.add(new Goods("Book", 5));
weight.add(new Goods("iPhone", 2));
result = new Vector<Vector<Goods>>();
setResizable(false);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 664, 389);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(null);
listWeight = new JList();
listWeight.setBorder(new BevelBorder(BevelBorder.LOWERED, null, null, null, null));
listWeight.setBounds(10, 10, 483, 341);
contentPane.add(listWeight);
listWeight.setListData(weight);
JButton btnAdd = new JButton("\u6DFB\u52A0");
btnAdd.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
AddGoodsDialog dialog = new AddGoodsDialog(mainWindow, true);
dialog.setVisible(true);
if (dialog.isOK) {
weight.add(new Goods(dialog.txtName.getText(),
Integer.parseInt(dialog.txtWeight.getText())));
listWeight.setListData(weight);
}
}
});
btnAdd.setBounds(503, 9, 145, 23);
contentPane.add(btnAdd);
JButton btnRemove = new JButton("\u79FB\u9664");
btnRemove.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if (listWeight.getSelectedIndex() != -1) {
weight.remove(listWeight.getSelectedIndex());
listWeight.setListData(weight);
}
}
});
btnRemove.setBounds(503, 42, 145, 23);
contentPane.add(btnRemove);
btnTotal = new JButton("\u603B\u91CD\u91CF = 10");
btnTotal.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String s = JOptionPane.showInputDialog(mainWindow, "请输入新的总重量:");
if (s != null && s.length() > 0) {
int n = 0;
try {
n = Integer.parseInt(s);
}
catch(Exception ex) {
JOptionPane.showMessageDialog(mainWindow, "请输入数字!");
return;
}
if (n > 0) {
totalWeight = n;
btnTotal.setText("\u603B\u91CD\u91CF = " + n);
}
else {
JOptionPane.showMessageDialog(mainWindow, "重量必须大于0!");
}
}
}
});
btnTotal.setBounds(503, 75, 145, 23);
contentPane.add(btnTotal);
JButton btnNewButton = new JButton("\u8BA1\u7B97");
btnNewButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
Backpack b = new Backpack();
b.totalWeight = totalWeight;
b.setWeight(weight);
result.clear();
b.find(new ResultListener() {
public void output(Vector<Goods> r) {
result.add(r);
}
});
listWeight.setListData(weight);
showResult();
}
});
btnNewButton.setBounds(503, 108, 145, 23);
contentPane.add(btnNewButton);
}
public void showResult() {
if (result.size() == 0) {
JOptionPane.showMessageDialog(mainWindow, "没有任何方案!");
return;
}
String s = "";
for(int i = 0; i < result.size(); i++) {
s += "第"+ (i + 1) +"组解:";
for(int j = 0; j < result.get(i).size(); j++) {
if (j > 0) s += "、";
s += result.get(i).get(j).toString();
}
s += ";\n";
}
JOptionPane.showMessageDialog(mainWindow, s);
}
}