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))
}
}
}
运行测试:
String className = "java.lang.String"Object obj = Class.forName(className).newInstance()
用这个方法要求该类必须有一个无参构造,如果没有,可以取得指定的构造法,然后newInstance():
String className = "java.lang.String"
Object obj = Class.forName(className).getConstructor(String.class).newInstance("abc")
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)