重庆分公司,新征程启航
为企业提供网站建设、域名注册、服务器等服务
import java.awt.*;
我们提供的服务有:成都网站设计、网站建设、微信公众号开发、网站优化、网站认证、嘉定ssl等。为1000+企事业单位解决了网站和推广的问题。提供周到的售前咨询和贴心的售后服务,是有科学管理、有技术的嘉定网站制作公司
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);
}
}
效果图
参考代码
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());
}
}
代码如下:
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);
}
}
运行结果:
//核心代码
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();//创建窗口
}
}
通过JS实现,我只能告诉你思想,1,首先写一个添加的按钮,然后一个输入框,在输入值后。 点击“添加”调用js事件,js取得你写的值,通过js插入到下拉框中去(代码百度上找),这是不插入到数据库的 刷新就没有了, 2,添加后放到数据库中去,然后刷新的时候 自动加载到下拉框中去
用ajax技术实现的具体例子如下
如下是实现的代码,大家有需要的可以看下:
script type="text/javascript"
var xmlHttp; //ajax初始化对象
var arrOptions = new Array(); //初始化数组元素
var currentValueSelected = -1;//表示当前选中的某项
//判断输入的字符是否超过5个
function querybylength(){
var woId = document.getElementById("woId").value;
if(woId.length=5){
//判断做什么动作
var intKey = -1;
if(window.event){
intKey = event.keyCode;
}
//alert(intKey);
if(intKey == 38){//按向上键
//alert(currentValueSelected);
if(currentValueSelected != -1){ //保证当前有用到SPAN
MoveHighlight(-1);
return false;
}
}else if(intKey == 40){ //按向下键
if(currentValueSelected != -1){ //保证当前有用到SPAN
MoveHighlight(1);
return false;
}
}else {
ajaxTest(woId); //初始化SPAN
}
}else {
HideTheBox();
currentValueSelected = -1;
}
}
//AJAX查询工单资料
function ajaxTest(name){
create();
if (xmlHttp==null){
alert ("您的浏览器不支持AJAX!");
return;
}
var url = "/spnewmes/servlet/QueryWOId?woId="+name;
xmlHttp.open("post",url,true);
xmlHttp.onreadystatechange = stateChanged;
xmlHttp.send(null);
}
function create(){
if (window.XMLHttpRequest) {
this.xmlHttp = new XMLHttpRequest();
}else if (window.ActiveXObject) {
this.xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
}
}
function stateChanged(){ //运行它进行ajax调用
if (xmlHttp.readyState==4){
var msg = xmlHttp.responseText; //获取返回值
arrOptions = msg.split(";");
//alert(arrOptions.length+":"+arrOptions);
if(arrOptions[0]!="")
Bulid(arrOptions); //给SPAN赋值
else
HideTheBox(); //隐藏SPAN
}
}
//创建并赋值span标签
function creatSpan(){
var elemSpan = document.createElement("span");//在页面创建span标签
elemSpan.id = "spanOutput";
elemSpan.className = "spanTextDropdown";
document.body.appendChild(elemSpan); //将上面创建的elemSpan元素加入到BODY的尾部
}
function Bulid(arrOptions){ //给SPAN赋值
var inner="";
SetElementPosition();//设置下拉框出现的位置
for(var i=0; i arrOptions.length; i++){
//alert(i+":"+arrOptions[i]);
inner+="span id=arr_"+i+" class='spanNormalElement' onmouseover='SetHighColor(this)' onclick='SetText()'font color=red"+arrOptions[i]+"/font/spanbr";
}
document.getElementById("spanOutput").innerHTML = inner;
if(inner!=""){
//alert('init');
document.getElementById("arr_0").className ="spanHighElement";//设置第一个顶为默认选中
currentValueSelected=0;
}
}
function SetElementPosition(){ //设置下拉框出现的位置
var selectedPosX = 0;
var selectedPosY = 0;
var theElement = document.form1.woId;
if (!theElement) return;
var theElemHeight = theElement.offsetHeight;
var theElemWidth = theElement.offsetWidth;
while(theElement != null){
selectedPosX += theElement.offsetLeft;
selectedPosY += theElement.offsetTop;
theElement = theElement.offsetParent;
}
xPosElement = document.getElementById("spanOutput");
xPosElement.style.left = selectedPosX;
xPosElement.style.width = theElemWidth;
xPosElement.style.top = selectedPosY + theElemHeight
xPosElement.style.display = "block";
}
function HideTheBox(){//隐藏下拉框
document.getElementById("spanOutput").style.display = "none";
currentValueSelected = -1;
}
function SetHighColor(theTextBox){//当鼠标划过变为选中状态
document.getElementById('arr_' + currentValueSelected).className ='spanNormalElement';
if(theTextBox){
currentValueSelected = theTextBox.id.slice(theTextBox.id.indexOf("_")+1, theTextBox.id.length);
}
//alert('SetHighColor:'+currentValueSelected);
document.getElementById('arr_'+currentValueSelected).className = 'spanHighElement';
}
function SetText(){//选中下拉框中的某个值
var theTextBox = document.form1.woId;
theTextBox.value = arrOptions[currentValueSelected];
document.getElementById("woId").value = theTextBox.value;
HideTheBox();
}
function MoveHighlight(xDir){//设置上下移动键
var currnum=parseInt(parseInt(currentValueSelected)+parseInt(xDir));
//alert('MoveHighlight:'+currentValueSelected+'+'+xDir+'='+currnum);
if(currnum = 0 currnumarrOptions.length ){
document.getElementById("arr_"+currentValueSelected).className ="spanNormalElement";
document.getElementById("arr_"+currnum).className ="spanHighElement";
currentValueSelected=currnum;
}else if(currnum==arrOptions.length){
document.getElementById("arr_"+currentValueSelected+"").className ="spanNormalElement";
currentValueSelected=0;
document.getElementById("arr_"+currentValueSelected+"").className ="spanHighElement";
}else if(currnum==-1){
document.getElementById("arr_"+currentValueSelected+"").className ="spanNormalElement";
currentValueSelected=arrOptions.length-1;
document.getElementById("arr_"+currentValueSelected+"").className ="spanHighElement";
}
}
/script