展开全部

效果图

Java判断选择的下滑列表里面的内容 java实现下拉列表_java

参考代码

import java.awt.event.*;
import java.util.Vector;
import javax.swing.*;
public class Demo extends JFrame implements ActionListener {
JButton btn;
Vector vector=new Vector();
JComboBox jcb;
public Demo() {
vector.add("公斤");
vector.add("每个");
vector.add("每盒");
JLabel jl = new JLabel("单位");
jcb = new JComboBox(vector);//可以传入一个数组或者一个Vector链表,对下拉框进行初始化62616964757a686964616fe4b893e5b19e31333431356133
btn=new JButton("获取下拉框的文字");
btn.addActionListener(this);
JPanel jp = new JPanel();
jp.add(jl);
jp.add(jcb);
jp.add(btn);
add(jp);
setSize(300, 200);
setLocationRelativeTo(null);
setTitle("下拉框Demo");
setDefaultCloseOperation(EXIT_ON_CLOSE);
}
public static void main(String[] args) {
new Demo().setVisible(true);
}
@Override
public void actionPerformed(ActionEvent e) {
JOptionPane.showMessageDialog(this, (String)jcb.getSelectedItem());
}
}