java列表框代码 java列表组件
这是一个JAVA中的Swing下拉列表框问题
代码如下:
创新互联公司专注于城子河网站建设服务及定制,我们拥有丰富的企业做网站经验。 热诚为您提供城子河营销型网站建设,城子河网站制作、城子河网页设计、城子河网站官网定制、小程序定制开发服务,打造城子河网络公司原创品牌,更为您提供城子河网站排名全网营销落地服务。
import java.awt.Color;
import java.awt.FlowLayout;
import javax.swing.JComboBox;
import javax.swing.JFrame;
class ColorItem {
private String name;
private Color color;
public ColorItem(String name, Color color) {
this.name = name;
this.color = color;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Color getColor() {
return color;
}
public void setColor(Color color) {
this.color = color;
}
@Override
public String toString() {
return getName();
}
}
public class App extends JFrame {
public App() {
this.setSize(500, 500);
this.setLocationRelativeTo(null);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setLayout(new FlowLayout());
JComboBoxColorItem cbxColor = new JComboBox();
cbxColor.addItem(new ColorItem("红", Color.RED));
cbxColor.addItem(new ColorItem("绿", Color.GREEN));
cbxColor.addItem(new ColorItem("蓝", Color.BLUE));
cbxColor.addItem(new ColorItem("黄", Color.YELLOW));
cbxColor.addItemListener(e - {
ColorItem item = (ColorItem)e.getItem();
getContentPane().setBackground(item.getColor());
});
this.add(cbxColor);
getContentPane().setBackground(cbxColor.getItemAt(0).getColor());
}
public static void main(String[] args) {
new App().setVisible(true);
}
}
运行结果:
做一个Java 含有两个下拉列表框 如下图所示
Java程序:
import java.awt.*;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import javax.swing.*;
public class Main extends JFrame implements ItemListener {
JComboBox cmbProvince, cmbCity;
JTextField txtProvince, txtCity;
String[] provinces = {"请选择省份", "北京市", "上海市", "河南省"};
String[][] cities = {{"东城区", "西城区", "海淀区", "丰台区"}, {"浦东区", "徐汇区", "崇明县"}, {"郑州市", "洛阳市", "开封市"}};
public Main() {
super("请选择省份/城市");
this.setSize(350, 200);
this.setVisible(true);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setLayout(new FlowLayout());
init();
}
public void init() {
cmbProvince = new JComboBox(provinces);
cmbCity = new JComboBox(new String[]{"请选择城市"});
txtProvince = new JTextField(15);
txtCity = new JTextField(15);
this.add(cmbProvince);
this.add(txtProvince);
this.add(cmbCity);
this.add(txtCity);
cmbProvince.addItemListener(this);
cmbCity.addItemListener(this);
}
public static void main(String[] args) {
new Main();
}
@Override
public void itemStateChanged(ItemEvent arg0) {
if(arg0.getStateChange() != ItemEvent.SELECTED){
return;
}
JComboBox cmb = (JComboBox) arg0.getSource();
int i;
int index;
if(cmb == cmbProvince) {
index = cmbProvince.getSelectedIndex();
if(index == 0) {
return;
}
cmbCity.removeAllItems();
cmbCity.addItem("请选择城市");
for(i=0; icities[index-1].length; i++) {
cmbCity.addItem(cities[index-1][i]);
}
txtProvince.setText("您选择的省份是:" + cmbProvince.getSelectedItem().toString());
}
else if(cmb == cmbCity) {
index = cmbCity.getSelectedIndex();
if(index == 0) {
return;
}
txtCity.setText("您选择的城市是:" + cmb.getSelectedItem().toString());
}
}
}
运行测试:
Java下拉列表框显示数据库中某列的值,代码怎么写啊
用ajax加载,然后显示在界面上
$.post("roomr!getMyReserves.s",function(data){
var rows = '';
var rooms = data.rows;
for(var i = 0; i data.length; i ++){
var startTime = data[i].starttime;
var endTime = data[i].endtime;
rows += "option value='"
+ data[i].id + "'"
+data[i].roomname + "/option" ;
}
$("#roomid").html(rows);
});
请大佬给我一个java swing,下拉列表框和下拉列表的例子尽量简单,需要较多注释谢谢。。
代码如下:
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列表框代码 java列表组件
文章网址:http://myzitong.com/article/doesosp.html