重庆分公司,新征程启航
为企业提供网站建设、域名注册、服务器等服务
//核心代码
玛沁网站建设公司创新互联,玛沁网站设计制作,有大型网站制作公司丰富经验。已为玛沁上1000家提供企业网站建设服务。企业网站搭建\外贸网站制作要多少钱,请找那个售后服务好的玛沁做网站的公司定做!
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();//创建窗口
}
}
代码如下:
import java.awt.Color;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.event.ItemEvent;
import javax.swing.*;
public class App extends JFrame {
private JComboBoxString comboBox;
private JListString list;
public App() {
this.setSize(300, 200);
this.setDefaultCloseOperation(DISPOSE_ON_CLOSE); // 设置关闭方式
this.setLayout(new FlowLayout());
// 定义一些选项
String[] options = { "红", "黄", "蓝", "绿" };
// 创建下拉列表
comboBox = new JComboBox(options);
// 添加选择事件监听器,选择时触发
comboBox.addItemListener(e - {
if (e.getStateChange() == ItemEvent.SELECTED)
setBackground((String)e.getItem());
});
this.add(comboBox);
// 创建列表框
list = new JList(options);
list.setPreferredSize(new Dimension(50, 50));
// 添加选择事件监听器,选择时触发
list.addListSelectionListener(e - setBackground(options[list.getSelectedIndex()]));
this.add(new JScrollPane(list));
}
// 设置窗口背景
private void setBackground(String color) {
switch(color) {
case "红":
getContentPane().setBackground(Color.RED);
break;
case "黄":
getContentPane().setBackground(Color.YELLOW);
break;
case "蓝":
getContentPane().setBackground(Color.BLUE);
break;
case "绿":
getContentPane().setBackground(Color.GREEN);
break;
}
}
public static void main(String[] args) {
new App().setVisible(true);
}
}
运行结果:
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"); //在第一行第一个单元格,插入下拉框 HSSFRow row = sheet.createRow(0); HSSFCell cell = row.createCell(0); //普通写入操作 cell.setCellValue("请选择");//这是实验 //生成下拉列表 //只对(0,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"); } }责任编辑:linshi1
第一个下拉框绑定一个onchange事件,第二个下拉框保持为没有选项,当选中第一个下框某项时在动态获取第二个下拉框的内容