1. 下拉框 JComboBox

//导入Java类
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class Demo extends JFrame{
public Demo(){
setBounds(100,100,200,100);
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); Container c=getContentPane();
c.setLayout(null); //下拉列表框的第一种方法
JComboBox comboBox=new JComboBox();//创建一个新的下拉框对象
comboBox.addItem("大白兔奶糖");//给下拉框对象添加条目元素
comboBox.addItem("牛排");
comboBox.addItem("烤鸭");
comboBox.addItem("鸡腿");
comboBox.addItem("汉堡"); // 第二种创建下拉列表的方法
String items[]={"大白兔奶糖","牛排","烤鸭","鸡腿","汉堡"};//创建条目字符串数组
JComboBox comboBox=new JComboBox(items);//将字符串数组名添加到下拉框列表内 //第三种创建下拉列表的方法
JComboBox comboBox=new JComboBox();
String item[]={"大白兔奶糖","牛排","烤鸭","鸡腿","汉堡"};
ComboBoxModel cm=new DefaultComboBoxModel(item);//创建一个新的下拉框列表模型
comboBox.setModel(cm);//设置模型将创建的下拉框列表加入其中 JButton btn=new JButton("选择");
btn.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
System.out.println("选中的索引"+comboBox.getSelectedIndex());
System.out.println("选中的值"+comboBox.getSelectedItem());
}
}); c.add(comboBox);
comboBox.setBounds(10,10,80,21); comboBox.setEditable(true);//可编辑内容 c.add(btn);
btn.setBounds(100,10,60,20); setVisible(true); }
public static void main(String[] args){
new Demo();
}
}

 2. 文本框JTextField

//导入java类
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class Demo extends JFrame {
public Demo(){
setBounds(100,100,200,100);
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); Container c=getContentPane();
c.setLayout(new FlowLayout()); JTextField jt=new JTextField();//创建新的文本框
jt.setColumns(20);//设置文本框的长度。20字符
jt.setFont(new Font("微软雅黑",Font.PLAIN,10));//设置文本字体,设置字体的样式,设置字体的大小。
jt.setText("w文本框");//设置文本框内容
c.add(jt);
            JButton btn=new JButton("选择");
btn.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
System.out.println("文本框的内容为:"+jt.getText());//获取文本内容
}
});
c.add(btn);
setVisible(true);
} public static void main(String[] args) {
new Demo();
}
}

下拉框 JComboBox,文本框JTextField的更多相关文章

  1. jQuery实现限制input框 textarea文本框输入字符数量的方法

    <!doctype html> <html> <head> <meta charset="utf-8"> <title> ...

  2. java swing 下拉框与文本框

    import java.awt.*; import javax.swing.*; import javax.swing.border.*; import java.awt.event.*; publi ...

  3. JAVA个人小程序GUI篇-收银(标签、按钮、复选框、下拉标、文本域、表格······)

    如果用eclipse需先装载windowsbuild //导入包 import java.awt.BorderLayout; import java.awt.EventQueue; import ja ...

  4. IOS系统下虚拟键盘遮挡文本框问题的解决

    最近在项目中发现同样的代码在Android端微信网页中点击文本框唤出的虚拟键盘不会遮挡文本框,但是在IOS端的微信网页中点击文本框唤出的键盘却在大部分情况下会遮挡文本框 经过高人指点,这个问题终于解决 ...

  5. yii框架中的下拉菜单和单选框

    yii中的下拉菜单: 第一种: <?= $form->field($model, 'parent_id')->dropDownList(ArrayHelper::map($data, ...

  6. jQuery获取及设置单选框、多选框、文本框内容

    获取一组radio被选中项的值 var item = $('input[@name=items][@checked]').val(); 获取select被选中项的文本 var item = $(&qu ...

  7. html-4, form 表单 输入、传文件、单选、多选、下拉菜单、文本描述、重置、submit、按钮限制输入

    <!-- form HTTP协议 action:提交的服务器网址 method:get(默认)| post(应用:登录注册.上传文件) 页面中的a img link 默认是get请求 input ...

  8. jQuery获取及设置单选框、多选框、文本框

    获取一组radio被选中项的值 var item = $("input[@name=items][@checked]").val(); 获取select被选中项的文本 var it ...

  9. html基础 下拉菜单和文本域的基本操作

    结构代码 所在城市: <select > <option selected>北京</option> <option>上海</option> ...

  10. IE中input标签密码框与文本框宽度不一样问题

    前言 在项目登录界面中有账户和密码的输入框,在Chrome中显示是正常的(本人使用的是Chrome浏览器,平时不用IE).等部署到客户的服务器上,访问时发现一个问题,在IE浏览器中文本框与密码框的宽度 ...

随机推荐

  1. 利用Python实现对Web服务器的目录探测

    今天是一篇提升技能的干货分享,操作性较强,适用于中级水平的小伙伴,文章阅读用时约3分钟. PART 1/Python Python是一种解释型.面向对象.动态数据类型的高级程序设计语言. Python ...

  2. Android OpenGL ES 开发(六): OpenGL ES 添加运动效果

    在屏幕上绘制图形只是OpenGL的相当基础的特点,你也可以用其他的Android图形框架类来实现这些,包括Canvas和Drawable对象.OpenGL ES为在三维空间中移动和变换提供了额外的功能 ...

  3. [Swift]LeetCode222. 完全二叉树的节点个数 | Count Complete Tree Nodes

    Given a complete binary tree, count the number of nodes. Note: Definition of a complete binary tree ...

  4. [Swift]LeetCode318. 最大单词长度乘积 | Maximum Product of Word Lengths

    Given a string array words, find the maximum value of length(word[i]) * length(word[j]) where the tw ...

  5. [Swift]LeetCode629. K个逆序对数组 | K Inverse Pairs Array

    Given two integers n and k, find how many different arrays consist of numbers from 1 to n such that ...

  6. [Swift]LeetCode878. 第 N 个神奇数字 | Nth Magical Number

    A positive integer is magical if it is divisible by either A or B. Return the N-th magical number.  ...

  7. Python内置函数(63)——super

    英文文档: super([type[, object-or-type]]) Return a proxy object that delegates method calls to a parent ...

  8. .NET Core实战项目之CMS 第六章 入门篇-Vue的快速入门及其使用

    写在前面 上面文章我给大家介绍了Dapper这个ORM框架的简单使用,大伙会用了嘛!本来今天这篇文章是要讲Vue的快速入门的,原因是想在后面的文章中使用Vue进行这个CMS系统的后台管理界面的实现.但 ...

  9. C++日志系统log4cxx使用总结

    原文地址:C++日志系统log4cxx使用总结作者:邵明 本文主要从log4cxx级别.layout.格式化.命名规则.Filter几个方面介绍.   一.log4cxx命名规则         Lo ...

  10. Eclipse导入别人的项目报错:Unable to load annotation processor factory 'xxxxx.jar' for project

    使用eclipse导入别人的项目时候,报错Unable to load annotation processor factory 'xxxxx.jar' for project. 解决方案 1.项目右 ...