学习Java从头开始(3)
2009-07-27 21:15
309 查看
package com.KeBen;
import java.util.Vector;
import javax.swing.*;
public class JText extends JFrame
{
public JText()
{
JTextArea txt1 = new JTextArea(); //文本域
txt1.append("一个文本域"); //文本域中显示的值
txt1.setEditable(false); //设为不可编辑,true为可编辑
txt1.setTabSize(10); //按下Tab键跳离的距离为10
txt1.setLineWrap(true); //设为自动换行, false为不自动换行
String[] strings = {"美国","中国","日本","英国","法国"}; //字符数组 用来装入组合框内容
JComboBox coBox1 = new JComboBox(strings); //创建组合框对象,采用字符串数组为参数的构造函数
coBox1.addItem("澳洲"); //向组合框对象中加入一个值
Vector<String> vector = new Vector<String>(); //字符串泛型的向量,里面装载组合框的内容
vector.addElement("1"); //向vector对象中加入值
vector.addElement("2");
vector.addElement("3");
vector.addElement("4");
vector.addElement("5");
vector.addElement("其它");
JComboBox coBox2 = new JComboBox(vector); //创建另一个组合框对象,采用Vector为参数的构造函数
JPanel pn = new JPanel(); //向面板中添加组合框对象
pn.add(coBox1);
pn.add(coBox2);
this.add(pn);
this.setSize(500,600);
this.setVisible(true);
//以下是选择类组件
//创建三个单选框对象
JRadioButton r1 = new JRadioButton("maidangnao");
JRadioButton r2 = new JRadioButton("kedeji");
JRadioButton r3 = new JRadioButton("21shiji");
//将单选框对象添加到面板上
pn.add(r1);
pn.add(r2);
pn.add(r3);
ButtonGroup bGroup = new ButtonGroup(); //创建按钮组对象
//将三个单选框对象添加到按钮组中,使他们在逻辑上成为一组,否则不能实现单选功能
bGroup.add(r1);
bGroup.add(r2);
bGroup.add(r3);
//添加面板到窗体上
this.add(pn);
this.pack();
this.setVisible(true);
//创建三个复选框对象
JCheckBox c1 = new JCheckBox("maidangnao");
JCheckBox c2 = new JCheckBox("kedeji");
JCheckBox c3 = new JCheckBox("21shiji");
//将复选框对象添加到面板上
pn.add(c1);
pn.add(c2);
pn.add(c3);
//添加面板到窗体上
this.add(pn);
this.pack();
this.setVisible(true);
//JList列表框
String[] strings2 = {"美国","日本","英国","中国","法国"};//创建字符串数组
//创建列表对象,并将字符串数组对象放入其中,做列表 的内容
JList list = new JList(strings2);
pn.add(list); //向面板对象中加入列表对象
this.add(pn);
this.pack();
}
}
import java.util.Vector;
import javax.swing.*;
public class JText extends JFrame
{
public JText()
{
JTextArea txt1 = new JTextArea(); //文本域
txt1.append("一个文本域"); //文本域中显示的值
txt1.setEditable(false); //设为不可编辑,true为可编辑
txt1.setTabSize(10); //按下Tab键跳离的距离为10
txt1.setLineWrap(true); //设为自动换行, false为不自动换行
String[] strings = {"美国","中国","日本","英国","法国"}; //字符数组 用来装入组合框内容
JComboBox coBox1 = new JComboBox(strings); //创建组合框对象,采用字符串数组为参数的构造函数
coBox1.addItem("澳洲"); //向组合框对象中加入一个值
Vector<String> vector = new Vector<String>(); //字符串泛型的向量,里面装载组合框的内容
vector.addElement("1"); //向vector对象中加入值
vector.addElement("2");
vector.addElement("3");
vector.addElement("4");
vector.addElement("5");
vector.addElement("其它");
JComboBox coBox2 = new JComboBox(vector); //创建另一个组合框对象,采用Vector为参数的构造函数
JPanel pn = new JPanel(); //向面板中添加组合框对象
pn.add(coBox1);
pn.add(coBox2);
this.add(pn);
this.setSize(500,600);
this.setVisible(true);
//以下是选择类组件
//创建三个单选框对象
JRadioButton r1 = new JRadioButton("maidangnao");
JRadioButton r2 = new JRadioButton("kedeji");
JRadioButton r3 = new JRadioButton("21shiji");
//将单选框对象添加到面板上
pn.add(r1);
pn.add(r2);
pn.add(r3);
ButtonGroup bGroup = new ButtonGroup(); //创建按钮组对象
//将三个单选框对象添加到按钮组中,使他们在逻辑上成为一组,否则不能实现单选功能
bGroup.add(r1);
bGroup.add(r2);
bGroup.add(r3);
//添加面板到窗体上
this.add(pn);
this.pack();
this.setVisible(true);
//创建三个复选框对象
JCheckBox c1 = new JCheckBox("maidangnao");
JCheckBox c2 = new JCheckBox("kedeji");
JCheckBox c3 = new JCheckBox("21shiji");
//将复选框对象添加到面板上
pn.add(c1);
pn.add(c2);
pn.add(c3);
//添加面板到窗体上
this.add(pn);
this.pack();
this.setVisible(true);
//JList列表框
String[] strings2 = {"美国","日本","英国","中国","法国"};//创建字符串数组
//创建列表对象,并将字符串数组对象放入其中,做列表 的内容
JList list = new JList(strings2);
pn.add(list); //向面板对象中加入列表对象
this.add(pn);
this.pack();
}
}
相关文章推荐
- 【三】java从头开始学习之赶紧搬家吧,CSDN太坑了
- 学习Java从头开始(2)
- 从头开始学习Java-JavaEE基础
- 从头开始学习JAVA 5--Spring MVC
- java-回头是岸(从头开始学习)
- 从头开始学习Java
- 从头开始学习JAVA
- 从头开始学习cocos2dx之笔记二(搭建ECLIPSE ANDROID JAVA环境)
- 从头开始学习JAVA 6--引入JQuery
- 从头开始学习JAVA 2--Tomcat
- java从头开始学习到选择c++ (三)控制语句需要注意的地方
- 从头开始,学习Java Web
- java从头开始学习到选择c++ (五)java中的== 和 equals 区别
- java从头开始学习到选择c++ (四)数组需要注意的地方
- 【一】java从头开始学习之两个jdk引发的血案
- 【二】java从头开始学习之Tomcat是只好猫
- 从头开始学习JAVA 3--MyEclipse
- 从头开始学习java
- 从头开始学习JAVA 4--创建Web程序
- java从头开始学习到选择c++ (一)java概述(1)