




版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
第Java有趣好玩的图形界面开发八个案例实现目录1.复选框和单选框按钮组2.文本编辑组件和滚动窗格3.多个选项卡设置4.在框架窗口中加入面板5.在窗口中加入标签6.框架中加入指定大小的标签7.在框架窗口中加入按钮8.框架窗口的创建总结虽然GUI技术没有很大的市场,甚至很多初学者放弃学习GUI,但是学习GUI编程的过程对于提高编程兴趣,深入理解Java编程有很大的作用。效果图如下,加油吧!!
1.复选框和单选框按钮组
在框架窗口中加入复选框和单选框按钮组
importjavax.swing.*;
publicclassAppextendsJFrame{
staticJFramejFrame=newJFrame("复选框和单选组按钮选取框");
staticJCheckBoxjCheckBox1=newJCheckBox("粗体",true);
staticJCheckBoxjCheckBox2=newJCheckBox("斜体");
staticJCheckBoxjCheckBox3=newJCheckBox("下划线");
staticJRadioButtonjRadioButton1=newJRadioButton("红色",true);
staticJRadioButtonjRadioButton2=newJRadioButton("绿色",true);
staticJRadioButtonjRadioButton3=newJRadioButton("蓝色");
publicstaticvoidmain(String[]args){
ButtonGroupbuttonGroup=newButtonGroup();
jFrame.setLocation(200,150);
jFrame.setSize(300,220);
jFrame.setLayout(null);
jCheckBox1.setBounds(20,20,50,20);
jCheckBox2.setBounds(20,40,50,20);
jCheckBox3.setBounds(20,60,70,20);
jRadioButton1.setBounds(40,100,50,20);
jRadioButton2.setBounds(40,120,50,20);
jRadioButton3.setBounds(40,140,50,20);
jFrame.add(jCheckBox1);
jFrame.add(jCheckBox2);
jFrame.add(jCheckBox3);
buttonGroup.add(jRadioButton1);
buttonGroup.add(jRadioButton2);
buttonGroup.add(jRadioButton3);
jFrame.add(jRadioButton1);
jFrame.add(jRadioButton2);
jFrame.add(jRadioButton3);
jFrame.setDefaultCloseOperation(EXIT_ON_CLOSE);
jFrame.setVisible(true);
2.文本编辑组件和滚动窗格
设置文本编辑组件和滚动窗格
importjavax.swing.*;
publicclassAppextendsJFrame{
JTextFieldjTextField=newJTextField("该文本框不可编辑",30);
staticJPasswordFieldjPasswordField=newJPasswordField("HelloWorld",30);
publicApp(Stringstr){
super(str);
jTextField.setBounds(20,40,140,20);
jTextField.setEditable(false);
add(jTextField);
publicstaticvoidmain(String[]args){
AppjFrame=newApp("文本编辑功能窗口");
JTextAreajTextArea=newJTextArea("你好",10,30);
JScrollPanejScrollPane=newJScrollPane(jTextArea);
jFrame.setLocation(200,150);
jFrame.setSize(240,220);
jFrame.setLayout(null);
jScrollPane.setBounds(20,70,160,100);
jPasswordField.setBounds(20,10,140,10);
jFrame.add(jPasswordField);
jFrame.add(jScrollPane);
char[]passWorld=jPasswordField.getPassword();
Stringstr=newString(passWorld);
System.out.println("密码是:"+passWorld+"转换后"+str);
jFrame.setVisible(true);
jFrame.setResizable(false);
jFrame.setDefaultCloseOperation(EXIT_ON_CLOSE);
输出结果:密码是:[C@370736d9转换后HelloWorld
3.多个选项卡设置
在窗口中放一个选项卡窗格,并在选项卡窗格中加入若干选项卡,每个选项卡中放置一个带图像的标签组件。
importjavax.swing.*;
publicclassAppextendsJFrame{
publicApp(){
JLabel[]jLabels=newJLabel[6];
Iconpic;
Stringtitle;
for(inti=1;ii++){
pic=newImageIcon("images\\t"+i+".png");
jLabels[i]=newJLabel();
jLabels[i].setIcon(pic);
title="第"+i+"页";
jTabbedPane.add(title,jLabels[i]);
this.add(jTabbedPane);
JTabbedPanejTabbedPane=newJTabbedPane(JTabbedPane.TOP);
publicstaticvoidmain(String[]args){
AppjFrame=newApp();
jFrame.setTitle("选项卡的应用");
jFrame.setSize(300,300);
jFrame.setDefaultCloseOperation(EXIT_ON_CLOSE);
jFrame.setVisible(true);
4.在框架窗口中加入面板
importjavax.swing.*;
importjavax.swing.border.TitledBorder;
publicclassApp{
publicstaticvoidmain(String[]args){
JFramejFrame=newJFrame("我的框架");
jFrame.setSize(210,180);
jFrame.setLocation(500,400);
JPaneljPanel=newJPanel();
jPanel.setSize(120,90);
jPanel.setLocation(40,30);
JButtonjButton=newJButton("点击我");
jButton.setSize(80,20);
jButton.setLocation(20,30);
jFrame.setLayout(null);
jPanel.setLayout(null);
jPanel.add(jButton);
jPanel.setBorder(newTitledBorder("面板区"));
jFrame.add(jPanel);
jFrame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
jFrame.setVisible(true);
5.在窗口中加入标签
在窗口中加入标签,并设置框架的背景色及标签上文字的颜色和字体。
importjavax.swing.*;
importjava.awt.*;
publicclassApp{
publicstaticvoidmain(String[]args){
JFramejFrame=newJFrame("标签类窗口");
JLabeljLabel=newJLabel("我是一个标签",JLabel.CENTER);//创建标签类对象
jFrame.setLayout(null);//取消默认布局管理器
jFrame.setSize(300,200);//设置窗口的大小
Containerc=jFrame.getContentPane();//获取内容窗格
c.setBackground(Color.CYAN);//设置窗口的背景色
jLabel.setOpaque(true);//设置标签为不透明
jLabel.setBackground(Color.RED);//设置标签的背景色
jLabel.setForeground(Color.YELLOW);//设置标签的前景色
jLabel.setLocation(80,60);
jLabel.setSize(130,30);
Fontfont=newFont("楷体",Font.PLAIN,20);//创建字体对象
jLabel.setFont(font);//设置标签上的字体
jFrame.add(jLabel);
jFrame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
jFrame.setVisible(true);
6.框架中加入指定大小的标签
在框架中加入指定大小的标签,并设置当鼠标悬停在标签上时给出相应的提示信息。
importjavax.swing.*;
importjava.awt.*;
publicclassApp{
publicstaticvoidmain(String[]args){
JFramejFrame=newJFrame("标签类窗口");
JLabeljLabel=newJLabel("我是一个标签",JLabel.CENTER);//创建标签类对象
jFrame.setLayout(null);//取消默认布局管理器
jFrame.setSize(300,200);//设置窗口的大小
Containerc=jFrame.getContentPane();//获取内容窗格
c.setBackground(Color.CYAN);//设置窗口的背景色
jLabel.setOpaque(true);//设置标签为不透明
jLabel.setBackground(Color.RED);//设置标签的背景色
jLabel.setForeground(Color.YELLOW);//设置标签的前景色
jLabel.setLocation(80,60);
jLabel.setSize(130,30);
jLabel.setToolTipText("我被设置为不透明");
Fontfont=newFont("楷体",Font.PLAIN,20);//创建字体对象
jLabel.setFont(font);//设置标签上的字体
jFrame.add(jLabel);
jFrame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
jFrame.setVisible(true);
7.在框架窗口中加入按钮
importjavax.swing.*;
importjava.awt.*;
publicclassAppextendsJFrame{
publicstaticvoidmain(String[]args){
AppjFrame=newApp();
jFrame.setDefaultCloseOperation(EXIT_ON_CLOSE);
ImageIconicon=newImageIcon("images\\java.png");
JButtonjButton=newJButton();
jButton.setText("选择");
jButton.setIcon(icon);
jFrame.setLayout(null);
jFrame.setSize(200,180);
jFrame.setTitle("按钮类窗口");
jButton.setBounds(50,45,100,40);
jButton.setToolTipText("我是按钮");
jFrame.add(jButton);
jFrame.setVisible(true);
8.框架窗口的创建
importjavax.swing.*;
importjava.awt.*;
publicclassApp{
staticJFramejFrame=newJFrame("这是一个Swing程序");//创建静态框架并设置标题
publicstaticvoidmain(String[]args){
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 2025年广东省中考生物试卷真题及答案详解(精校打印版)
- 抗生素不当使用考核试卷
- 2025年中国90#汽油数据监测研究报告
- 2025年中国2,3丁二酮数据监测研究报告
- 2025至2030年中国高筒绅士防滑靴市场分析及竞争策略研究报告
- 2025至2030年中国防火防盗门窗市场分析及竞争策略研究报告
- 2025至2030年中国钢制平托盘市场分析及竞争策略研究报告
- 2025至2030年中国贵族酒水车市场分析及竞争策略研究报告
- 2025至2030年中国笔式绘图仪市场分析及竞争策略研究报告
- 2025至2030年中国皮毛匀染剂市场分析及竞争策略研究报告
- 2025年高考山东卷化学试题讲评及备考策略指导(课件)
- 2025年中国失重秤市场调查研究报告
- 学校展厅改造方案(3篇)
- 上海虹口区2024-2025学年下学期七年级期末考试英语试题(含答案无听力原文及音频)
- 2024年江苏省徐州市保安员证考试题库及答案()
- 2025年江西省中考数学试卷真题(含标准答案)
- 天台保安考试题及答案
- 2025年高考全国二卷英语高考真题含解析
- 2024年民族出版社招聘事业编制专业技术人员真题
- 2025年食品安全管理员考试试题及答案
- 2025-2030骨科植入器材产业市场深度分析及发展趋势与投资战略研究报告
评论
0/150
提交评论