下拉框java代码实现 javagui下拉框

如何用java实现下拉菜单//核心代码
String[] sg= {"苹果","香蕉","樱桃","山楂"};
JComboBoxString jcb = new JComboBoxString(sg);
完整代码
import java.awt.*;
import javax.swing.*;
public class MFrame extends JFrame{
public MFrame() {
String[] sg= {"苹果","香蕉","樱桃","山楂"};
JComboBoxString jcb = new JComboBoxString(sg);
add(jcb);
setLayout(new FlowLayout());//把JFrame设置成流式布局
setTitle("测试");//设置窗口标题
setSize(220, 100);//设置窗口大小
setLocationRelativeTo(null);//设置窗口居中
setDefaultCloseOperation(EXIT_ON_CLOSE);//设置点击关闭退出jvm虚拟机
setVisible(true);//设置窗口可见
}
public static void main(String[] args) {
new MFrame();//创建窗口
}
}
java程序下拉列表设计效果图
参考代码
import java.awt.event.*;
import java.util.Vector;
import javax.swing.*;
public class Demo extends JFrame implements ActionListener {
JButton btn;
VectorString vector=new VectorString();
JComboBoxString jcb;
public Demo() {
vector.add("公斤");
vector.add("每个");
vector.add("每盒");
【下拉框java代码实现 javagui下拉框】JLabel jl = new JLabel("单位");
jcb = new JComboBoxString(vector);//可以传入一个数组或者一个Vector链表,对下拉框进行初始化
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());
}
}
Java下拉框的整体代码?import java.awt.*;
import javax.swing.*;
public class DemoJframe extends JFrame {
private JPanel p1 = new JPanel(new BorderLayout());
private JComboBoxString jcb = new JComboBoxString();
public DemoJframe() {
jcb.insertItemAt("no.1", 0);
jcb.insertItemAt("no.2", 1);
jcb.insertItemAt("no.3", 2);
p1.add(jcb, BorderLayout.NORTH);
this.add(p1);
}
public static void main(String[] args) {
JFrame frame = new DemoJframe();
frame.setTitle("MyFrame");
frame.setSize(300, 300);
frame.setLocationRelativeTo(null);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
}
java中怎么实现多个下拉菜单的动态关联?1,所谓级联下拉菜单就是动态的下拉菜单,
当第一级选中某一个选项是,
第二级会跟着第一级的变化而变化,
比如:当“学院”这一栏选择“信息学院”那么“专业”这一栏只会出现属于信息学院的专业 。
2 , 级联下拉菜单的实现是运用itemStateChanged(ItemEvent )方法实现,首先在下拉菜单上注册ItemListener监视器,当下拉菜单选中时触发itemStateChanged(ItemEvent )事件重新给下一级下拉菜单赋值 。
public void itemStateChanged(ItemEvent e) {
if(e.getSource() == xueyuan) {//当点击学院下拉框时
try {
Statement stat2 = con.createStatement();
Stringsql = "select D_ID from xueyuanin WHERE
D_NAME='"+e.getItem()+"'"; //获取该学院的代号
ResultSet rs = stat2.executeQuery(sql);
rs.next();
Java源代码:POI操作Excel如何创建下拉框Java代码package poiexcel; import java.io.FileOutputStream; import java.util.Date; import org.apache.poi.hssf.usermodel.DVConstraint; import org.apache.poi.hssf.usermodel.HSSFCell; import org.apache.poi.hssf.usermodel.HSSFDataValidation; import org.apache.poi.hssf.usermodel.HSSFRow; import org.apache.poi.hssf.usermodel.HSSFSheet; import org.apache.poi.hssf.usermodel.HSSFWorkbook; import org.apache.poi.hssf.util.CellRangeAddressList; public class POI { public static void main(String [] args) { String [] list={"东软","华信","SAP","海辉"}; new POI().createListBox(list); return; } public void createListBox (String [] list) { //文件初始化 HSSFWorkbook wb = new HSSFWorkbook(); HSSFSheet sheet = wb.createSheet("new sheet"); //在第一行第一个单元格下拉框java代码实现,插入下拉框 HSSFRow row = sheet.createRow(0); HSSFCell cell = row.createCell(0); //普通写入操作 cell.setCellValue("请选择");//这是实验 //生成下拉列表 //只对(0下拉框java代码实现 , 0)单元格有效 CellRangeAddressList regions = new CellRangeAddressList(0,0,0,0); //生成下拉框内容 DVConstraint constraint = DVConstraint.createExplicitListConstraint(list); //绑定下拉框和作用区域 HSSFDataValidation data_validation = new HSSFDataValidation(regions,constraint); //对sheet页生效 sheet.addValidationData(data_validation); //写入文件 FileOutputStream fileOut; try { fileOut = new FileOutputStream("workbook.xls"); wb.write(fileOut); fileOut.close(); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } //结束 System.out.println("Over"); } }责任编辑下拉框java代码实现:linshi1

推荐阅读