我有一个购物车系统的代码,使用javax.swing.JList总是会导致问题。有没有办法通过改变一些东西来修复它?我应该用不同的方式写出来吗?
这是代码。 (我正在使用Dr.Java,我是否还应该使用任何其他编译器?)
import javax.swing.*;
import java.awt.event.*;
import java.awt.*;
import java.io.*;
public class ShoppingCartSystem extends JFrame
{
private JPanel sourceListPanel;
private JPanel shoppingCartPanel;
private JPanel buttonsPanel;
private JList sourceList;
private JList shoppingCart;
private JScrollPane scrollPane;
private JScrollPane scrollPane2;
private JButton addButton;
private JButton removeButton;
private JButton checkListButton;
private String []books;
private double []price;
private String []cart;
private int cartSize;
private double subTotal = 0.0,tax,total;
ShoppingCartSystem()
{
setTitle("Shopping Cart System");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLayout(new BorderLayout());
books = new String[25];
price = new double[25];
cart = new String[25];
cartSize =0;
readDataFromFile();
buildSourceListPanel();
buildShoppingCartPanel();
buildButtonsPanel();
add(sourceListPanel,BorderLayout.WEST);
add(shoppingCartPanel,BorderLayout.EAST);
add(buttonsPanel,BorderLayout.SOUTH);
pack();
setVisible(true);
}
private void readDataFromFile()
{
try{
DataInputStream dis = new DataInputStream(new FileInputStream("BookPrices.txt"));
BufferedReader br = new BufferedReader(new InputStreamReader(dis));
int i=0;
String line;
while((line = br.readLine()) != null){
String []arr = line.split(", ");
books[i] = arr[0];
price[i++] = Double.parseDouble(arr[1]);
}
br.close();
dis.close();
}
catch(Exception exp)
{
System.out.println(exp.toString());
}
}
private void buildSourceListPanel()
{
sourceListPanel = new JPanel();
sourceList = new JList(books);
sourceList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
sourceList.setVisibleRowCount(6);
scrollPane = new JScrollPane(sourceList);
sourceListPanel.add(scrollPane);
}
private void buildShoppingCartPanel()
{
shoppingCartPanel = new JPanel();
shoppingCart = new JList();
shoppingCart.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
shoppingCart.setVisibleRowCount(6);
scrollPane2 = new JScrollPane(shoppingCart);
shoppingCartPanel.add(scrollPane2);
}
private void buildButtonsPanel()
{
buttonsPanel =new JPanel();
addButton = new JButton("Add to shopping cart");
removeButton = new JButton("Remove from shopping cart");
checkListButton =new JButton("Check List");
buttonsPanel.setLayout(new FlowLayout(10));
buttonsPanel.add(addButton);
buttonsPanel.add(removeButton);
buttonsPanel.add(checkListButton);
addButton.addActionListener(new ButtonListener());
removeButton.addActionListener(new ButtonListener());
checkListButton.addActionListener(new ButtonListener());
}
private class ButtonListener implements ActionListener
{
public void actionPerformed(ActionEvent ae)
{
if(ae.getSource() == addButton)
{
cart[cartSize++] = (String) sourceList.getSelectedValue();
shoppingCart.setListData(cart);
subTotal += price[sourceList.getSelectedIndex()];
}
else if(ae.getSource() == removeButton)
{
for(int i=0;i<books.length;i++)
if(shoppingCart.getSelectedValue().equals(books[i]))
{
subTotal -= price[i];
break;
}
int selection = shoppingCart.getSelectedIndex();
for(int i=selection; i< cart.length-1;i++)
cart[i] = cart[i+1];
shoppingCart.setListData(cart);
}
else
{
java.text.DecimalFormat df = new java.text.DecimalFormat("#.##");
tax = subTotal * 0.06;
total = subTotal + tax;
StringBuffer sb = new StringBuffer();
sb.append("Sub Total: "+df.format(subTotal)+"\n");
sb.append("Tax: "+df.format(tax)+"\n");
sb.append("Total: "+df.format(total)+"\n");
JOptionPane.showMessageDialog(null, sb);
}
}
}
public static void main(String []args)
{
new ShoppingCartSystem();
}
}
答案 0 :(得分:0)
将JList
更改为JList<String>
或您在列表中输入的任何类型。
例如
sourceList = new JList<String>(books)
答案 1 :(得分:0)
是。您似乎想要包含JList
(s),因此您可以使用String
Generic Type 声明您的private JList<String> sourceList;
private JList<String> shoppingCart;
。像
sourceList = new JList<>();
// ...
shoppingCart = new JList<>();
然后初始化就像
一样private JList<String> sourceList = new JList<>();
private JList<String> shoppingCart = new JList<>();
或,您可以同时声明和初始化
<script>
nv.addGraph(function() {
var months = ["Jan","Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sept","Oct","Nov","Dec"];
var thisYear = [], lastYear = []
thisYear.push( {x: 2, y: 101034} );
thisYear.push( {x: 3, y: 229948} );
thisYear.push( {x: 4, y: 278940} );
thisYear.push( {x: 5, y: 370409} );
thisYear.push( {x: 6, y: 254532} );
thisYear.push( {x: 7, y: 201229} );
thisYear.push( {x: 8, y: 221323} );
thisYear.push( {x: 9, y: 210640} );
thisYear.push( {x: 10, y: 174958} );
thisYear.push( {x: 11, y: 172434} );
thisYear.push( {x: 12, y: 13527} );
lastYear.push( {x: 1, y: 0} );
lastYear.push( {x: 2, y: 380996} );
lastYear.push( {x: 3, y: 214687} );
lastYear.push( {x: 4, y: 123827} );
lastYear.push( {x: 5, y: 171242} );
lastYear.push( {x: 6, y: 155463} );
lastYear.push( {x: 7, y: 163326} );
lastYear.push( {x: 8, y: 209324} );
lastYear.push( {x: 9, y: 165603} );
lastYear.push( {x: 10, y: 147929} );
lastYear.push( {x: 11, y: 154803} );
lastYear.push( {x: 12, y: 85055} );
lastYear.push( {x: 1, y: 9005} );
var myData = [
{
values: thisYear,
key: 'This Year',
color: '#ff7f0e'
},
{
values: lastYear,
key: 'Last Year',
color: '#2ca02c'
}
];
var chart = nv.models.lineChart()
.margin({left: 100})
.useInteractiveGuideline(true)
.showLegend(true)
.showYAxis(true)
.showXAxis(true);
chart.xAxis
.axisLabel('Month of Year')
.tickValues([1,2,3,4,5,6,7,8,9,10,11,12])
.tickFormat(function(d){
return months[d]
});
chart.yAxis //Chart y-axis settings
.axisLabel('$ ex GST Turnover');
d3.select('#chart svg') //Select the <svg> element you want to render the chart in.
.datum(myData) //Populate the <svg> element with chart data...
.call(chart); //Finally, render the chart!
//Update the chart when window resizes.
nv.utils.windowResize(function() { chart.update() });
return chart;
});
</script>