Java程序:
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JTextField;
public class Main extends JFrame implements ActionListener {
JComboBox cmbMenu_All, cmbMenu_Selected;
JButton btnAdd, btnRemove;
JTextField txtNum;
String[] menus = {"羊肉火锅", "狗肉火锅", "鱼仔火锅", "剁椒鱼头", "水煮活鱼",
"柴火鱼", "酸辣鱼片", "茄子煲", "拨丝香蕉", "小炒肉",
"小鸡炖蘑菇", "水煮肉片", "白斩鸡", "海带炖排骨", "熘丝瓜",
"皮蛋煮苋菜", "醋溜土豆丝", "梅菜扣肉", "香芋地瓜丸", "凉拌藕尖"};
public Main() {
super("点菜");
this.setLayout(new FlowLayout());
cmbMenu_All = new JComboBox(menus);
cmbMenu_Selected = new JComboBox();
btnAdd = new JButton(">>");
btnRemove = new JButton("<<");
txtNum = new JTextField(10);
txtNum.setText("0");
txtNum.setAlignmentX(CENTER_ALIGNMENT);
this.add(cmbMenu_All);
this.add(btnAdd);
this.add(btnRemove);
this.add(cmbMenu_Selected);
this.add(txtNum);
btnAdd.addActionListener(this);
btnRemove.addActionListener(this);
this.setSize(400, 200);
this.setVisible(true);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
new Main();
}
@Override
public void actionPerformed(ActionEvent arg0) {
JButton btn = (JButton) arg0.getSource();
int num = Integer.parseInt(txtNum.getText().trim());
int i;
if(btn == btnAdd) {
for(i=0; iif(cmbMenu_Selected.getItemAt(i).toString().equals(cmbMenu_All.getSelectedItem().toString())) {
break;
}
}
if(i < cmbMenu_Selected.getItemCount()) {
return;
}
cmbMenu_Selected.addItem(cmbMenu_All.getSelectedItem());
num++;
txtNum.setText(String.valueOf(num));
}
else if(btn == btnRemove) {
cmbMenu_Selected.removeItemAt(cmbMenu_Selected.getSelectedIndex());
num--;
txtNum.setText(String.valueOf(num));
}
}
}
运行测试:
效果图
参考代码
import java.awt.*;
import javax.swing.*;
import javax.swing.event.*;
public class MenuFrame extends JFrame implements ListSelectionListener {
JTextField jtf;
JListjlist1, jlist2;
static final String[] CP = { "青椒土豆丝", "西红柿炒鸡蛋", "麻辣小龙虾", "鱼香肉丝", "西湖醋鱼", "红汤老火锅", "可乐", "茉莉花茶" };
DefaultListModeldlm;//用dlm给jlist2动态添加数据
public MenuFrame() {
JPanel jpCenter = new JPanel(new GridLayout(1, 2));
jlist1 = new JList(CP);
jlist1.addListSelectionListener(this);
JScrollPane jsp1 = new JScrollPane(jlist1);//滚动面板
jsp1.setBorder(BorderFactory.createTitledBorder("全部菜品"));//带标题的边框
jpCenter.add(jsp1);
jlist2 = new JList();
dlm = new DefaultListModel();
jlist2.setModel(dlm);
JScrollPane jsp2 = new JScrollPane(jlist2);
jsp2.setBorder(BorderFactory.createTitledBorder("已选菜品"));
jpCenter.add(jsp2);
add(jpCenter);
jtf = new JTextField(5);
jtf.setEnabled(false);
jtf.setText("0道");
JLabel jl = new JLabel("你已经选中的菜单数量是:");
JPanel jpSouth = new JPanel();
jpSouth.add(jl);
jpSouth.add(jtf);
add(jpSouth, BorderLayout.SOUTH);
setTitle("点菜");
setSize(300, 200);//窗口大小
setLocationRelativeTo(null);//窗口居中
setDefaultCloseOperation(EXIT_ON_CLOSE);
setVisible(true);
}
public static void main(String[] args) {
new MenuFrame();//启动窗口
}
public void valueChanged(ListSelectionEvent e) {
if (e.getValueIsAdjusting()) {
dlm.addElement(jlist1.getSelectedValue());//给dlm添加元素,更新jlist2的数据
jtf.setText(dlm.size()+"道");
}
}
}