Java程序:
import java.awt.FlowLayoutimport 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 i<cmbMenu_Selected.getItemCount() i++) {
if(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))
}
}
}
运行测试:
publicclass
student{
private
string
stuid
private
string
stuname
private
char
stusex
private
int
stuage
/**
*以下是各属性的getter/setter方法
*/
public
void
setstuid(string
stuid){
this.stuid=stuid
}
public
string
getstuid(){
return
stuid
}
public
void
setstuname(string
stuname){
this.stuname=stuname
}
public
string
getstuname(){
return
stuname
}
public
void
setstusex(char
stusex){
this.stusex=stusex
}
public
char
getstusex(){
return
stusex
}
public
void
setstuage(int
stuage){
this.stuage=stuage
}
public
int
getstuage(){
return
stuage
}
/**
*构造方法,构造学生信息
*/
public
student(string
stuid,string
stuname,char
stusex,int
stuage){
this.stuid=stuid
this.stuname=stuname
this.stusex=stusex
this.stuage=stuage
}
public
string
tostring(){//覆盖该类的tostring()方法
stringbuffer
buff=new
stringbuffer()
buff.append("学号:"+stuid)
buff.append("\n姓名:"+stuname)
buff.append("\n性别:"+stusex)
buff.append("\n年龄:"+stuage)
return
buff.tostring()
}
public
static
void
main(string[]
args){
student
stu=new
student("郑好纳袜碧1000","zhangsan",'男',18)
system.out.println
(stu)//打印学生信息
system.out.println
("--修改姓名结果--"喊没)
stu.setstuname("lisi")
system.out.println
(stu)
}
}
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)