大橙子网站建设,新征程启航
为企业提供网站建设、域名注册、服务器等服务
import java.awt.*;
网站的建设创新互联专注网站定制,经验丰富,不做模板,主营网站定制开发.小程序定制开发,H5页面制作!给你焕然一新的设计体验!已为成都服务器租用等企业提供专业服务。
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 Swing绘制下拉框JComboBox
第一步,双击打开MyEclipse开发软件,新建Java类SelectFrame,继承JFrame
第二步,声明一个JPanel和JComboBox,
private JComboBox selectBox = new JComboBox();
private JPanel panel = new JPanel();
第三步,给JComboBox和JPanel设置样式属性
panel.setBounds(150, 150, 400, 300);
panel.setToolTipText("下拉框");
panel.setLayout(new FlowLayout(FlowLayout.LEFT, 6, 6));
panel.setBackground(Color.LIGHT_GRAY);
panel.setVisible(true);
第四步,给JComboBox添加项目
selectBox.addItem("星期一");
selectBox.addItem("星期二");
selectBox.addItem("星期三");
selectBox.addItem("星期四");
selectBox.addItem("星期五");
selectBox.addItem("星期六");
selectBox.addItem("星期日");
selectBox.setVisible(true);
第五步,并将JLabel和JComboBox放入到JPanel上,在main方法内调用构造函数,
panel.add(label);
panel.add(selectBox);
第六步,设置JLabel样式属性
label.setText("星期");
label.setBounds(50, 50, 100, 30);
label.setFont(new Font("微软雅黑", Font.BOLD, 20));
label.setBackground(Color.BLUE);
label.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
//核心代码
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.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("每盒");
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());
}
}