我在 GUI 方面遇到了困难。
基本上我想创建一个程序,可以从给定列表中选择和添加项目,并显示添加项目的总金额(项目的价格)。因此,当我从 leftlist
添加商品时,价格将显示在 rightlist
中,当我按下 totalbutton
时,总价将显示在 rightlist
中。所选项目将通过 JLabel
显示。示例为:
到目前为止,这就是我所做的:
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
public class MP2_1 extends JFrame{
private JList leftlist;
private JList rightlist;
private JButton addbutton;
private JButton totalbutton;
private static String[] items = {"item1", "item2", "item3", "item4", "item5"
, "item6", "item7", "item8", "item9", "item10"};
private JButton total;
public MP2_1() {
super("MP2_1");
setLayout(new FlowLayout());
leftlist = new JList(items);
leftlist.setVisibleRowCount(5);
leftlist.setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);
add(new JScrollPane(leftlist));
addbutton = new JButton("ADD");
addbutton.addActionListener(
new ActionListener(){
public void actionPerformed(ActionEvent event){
rightlist.setListData(leftlist.getSelectedValuesList().toArray());
}
}
);
add(addbutton);
rightlist = new JList();
rightlist.setVisibleRowCount(5);
rightlist.setFixedCellWidth(55);
rightlist.setFixedCellHeight(20);
add(new JScrollPane(rightlist));
totalbutton = new JButton("Total");
totalbutton.addActionListener(
new ActionListener(){
public void actionPerformed(ActionEvent event){
rightlist.setListData(leftlist.getSelectedValuesList().toArray());
}
}
);
add(totalbutton);
}
public static void main(String[] args) {
MP2_1 list = new MP2_1();
list.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
list.setSize(300,300);
list.setVisible(true);
}
}
我一定在结构中犯了一个错误,比如使用setListData
,但我想不出任何其他方式在那里传输商品的价格,现在它只传输商品本身。
最佳答案
基于“当我从左侧列表添加商品时,价格将显示在右侧列表中” 我可以想到几种方法来做到这一点,我将通过使用 ListCellRenderer 来演示它,它为您提供了很多自定义功能。 我已经指出了 Total value ,您可以将其与标签一起使用。
- 创建项目对象。
- 创建自定义 ListCellRenderer 并将其与列表一起使用。
封装公共(public)地板;
import java.awt.Color;
import java.awt.Component;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import javax.swing.*;
public class MP2_1 extends JFrame {
private JList leftlist;
private JList rightlist;
private final JButton addbutton;
private final JButton totalbutton;
private static final Item[] items = {new Item("Item 0", 100), new Item("Item 1", 150), new Item("Item 2", 200), new Item("Item 3", 300)};
private JButton bTotal;
public MP2_1() {
super("MP2_1");
setLayout(new FlowLayout());
leftlist = new JList(items);
leftlist.setCellRenderer(new ItemCellRenderer(ItemCellRenderer.ITEM_NAME));
leftlist.setVisibleRowCount(5);
leftlist.setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);
add(new JScrollPane(leftlist));
addbutton = new JButton("ADD");
addbutton.addActionListener((ActionEvent event) -> {
rightlist.setListData(leftlist.getSelectedValuesList().toArray());
});
add(addbutton);
rightlist = new JList();
rightlist.setCellRenderer(new ItemCellRenderer(ItemCellRenderer.ITEM_PRICE));
rightlist.setVisibleRowCount(5);
rightlist.setFixedCellWidth(55);
rightlist.setFixedCellHeight(20);
add(new JScrollPane(rightlist));
totalbutton = new JButton("Total");
totalbutton.addActionListener((ActionEvent event) -> {
int total = 0;
for (int i = 0; i < rightlist.getModel().getSize(); i++) {
Item item = (Item) rightlist.getModel().getElementAt(i);
int itemPrice = item.getItemPrice();
total += itemPrice;
}
System.out.println("Total " + total);
// set the total label here
});
add(totalbutton);
}
public static void main(String[] args) {
MP2_1 list = new MP2_1();
list.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
list.setSize(300, 300);
list.setVisible(true);
}
}
//Create an Item Object.
class Item {
private final String itemName;
private final int itemPrice;
Item(String itemName, int itemPrice) {
this.itemPrice = itemPrice;
this.itemName = itemName;
}
public String getItemName() {
return itemName;
}
public int getItemPrice() {
return itemPrice;
}
}
//Create a Custom CellRenderer and use it with the Lists.
class ItemCellRenderer extends JLabel implements ListCellRenderer<Object> {
static int ITEM_NAME = 0;
static int ITEM_PRICE = 1;
private final int type;
public ItemCellRenderer(int type) {
this.type = type;
setOpaque(true);
}
@Override
public Component getListCellRendererComponent(JList<?> list,
Object value,
int index,
boolean isSelected,
boolean cellHasFocus) {
Item item = (Item) value;
setText(type == ITEM_NAME ? item.getItemName() : "" + item.getItemPrice());
Color background;
Color foreground;
// check if this cell represents the current DnD drop location
JList.DropLocation dropLocation = list.getDropLocation();
if (dropLocation != null
&& !dropLocation.isInsert()
&& dropLocation.getIndex() == index) {
background = Color.BLUE;
foreground = Color.WHITE;
// check if this cell is selected
} else if (isSelected) {
background = Color.RED;
foreground = Color.WHITE;
// unselected, and not the DnD drop location
} else {
background = Color.WHITE;
foreground = Color.BLACK;
}
setBackground(background);
setForeground(foreground);
return this;
}
}
关于Java GUI多项选择程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27455775/