您的位置:首页 > 编程语言 > Java开发

学习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();
}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: