Java有趣好玩的图形界面开发八个案例实现
作者:orangemilk_ 时间:2022-12-20 06:20:35
虽然GUI技术没有很大的市场,甚至很多初学者放弃学习GUI,但是学习GUI编程的过程对于提高编程兴趣,深入理解Java编程有很大的作用。效果图如下,加油吧!!
1.复选框和单选框按钮组
—在框架窗口中加入复选框和单选框按钮组
import javax.swing.*;
public class App extends JFrame{
static JFrame jFrame=new JFrame("复选框和单选组按钮选取框");
static JCheckBox jCheckBox1=new JCheckBox("粗体",true);
static JCheckBox jCheckBox2=new JCheckBox("斜体");
static JCheckBox jCheckBox3=new JCheckBox("下划线");
static JRadioButton jRadioButton1=new JRadioButton("红色",true);
static JRadioButton jRadioButton2=new JRadioButton("绿色",true);
static JRadioButton jRadioButton3=new JRadioButton("蓝色");
public static void main(String[] args) {
ButtonGroup buttonGroup=new ButtonGroup();
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.文本编辑组件和滚动窗格
—设置文本编辑组件和滚动窗格
import javax.swing.*;
public class App extends JFrame{
JTextField jTextField=new JTextField("该文本框不可编辑",30);
static JPasswordField jPasswordField=new JPasswordField("HelloWorld",30);
public App(String str){
super(str);
jTextField.setBounds(20,40,140,20);
jTextField.setEditable(false);
add(jTextField);
}
public static void main(String[] args) {
App jFrame=new App("文本编辑功能窗口");
JTextArea jTextArea=new JTextArea("你好",10,30);
JScrollPane jScrollPane=new JScrollPane(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();
String str=new String(passWorld);
System.out.println("密码是:"+passWorld+"转换后"+str);
jFrame.setVisible(true);
jFrame.setResizable(false);
jFrame.setDefaultCloseOperation(EXIT_ON_CLOSE);
}
}
输出结果:密码是:[C@370736d9转换后HelloWorld
3.多个选项卡设置
—在窗口中放一个选项卡窗格,并在选项卡窗格中加入若干选项卡,每个选项卡中放置一个带图像的标签组件。
import javax.swing.*;
public class App extends JFrame {
public App(){
JLabel[] jLabels=new JLabel[6];
Icon pic;
String title;
for(int i=1;i<=5;i++){
pic=new ImageIcon("images\\t"+i+".png");
jLabels[i]=new JLabel();
jLabels[i].setIcon(pic);
title="第"+i+"页";
jTabbedPane.add(title,jLabels[i]);
}
this.add(jTabbedPane);
}
JTabbedPane jTabbedPane=new JTabbedPane(JTabbedPane.TOP);
public static void main(String[] args) {
App jFrame=new App();
jFrame.setTitle("选项卡的应用");
jFrame.setSize(300,300);
jFrame.setDefaultCloseOperation(EXIT_ON_CLOSE);
jFrame.setVisible(true);
}
}
4.在框架窗口中加入面板
import javax.swing.*;
import javax.swing.border.TitledBorder;
public class App {
public static void main(String[] args) {
JFrame jFrame=new JFrame("我的框架");
jFrame.setSize(210,180);
jFrame.setLocation(500,400);
JPanel jPanel=new JPanel();
jPanel.setSize(120,90);
jPanel.setLocation(40,30);
JButton jButton=new JButton("点击我");
jButton.setSize(80,20);
jButton.setLocation(20,30);
jFrame.setLayout(null);
jPanel.setLayout(null);
jPanel.add(jButton);
jPanel.setBorder(new TitledBorder("面板区"));
jFrame.add(jPanel);
jFrame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
jFrame.setVisible(true);
}
}
5.在窗口中加入标签
—在窗口中加入标签,并设置框架的背景色及标签上文字的颜色和字体。
import javax.swing.*;
import java.awt.*;
public class App {
public static void main(String[] args) {
JFrame jFrame=new JFrame("标签类窗口");
JLabel jLabel=new JLabel("我是一个标签",JLabel.CENTER);//创建标签类对象
jFrame.setLayout(null);//取消默认布局管理器
jFrame.setSize(300,200);//设置窗口的大小
Container c=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);
Font font=new Font("楷体",Font.PLAIN,20);//创建字体对象
jLabel.setFont(font);//设置标签上的字体
jFrame.add(jLabel);
jFrame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
jFrame.setVisible(true);
}
}
6.框架中加入指定大小的标签
—在框架中加入指定大小的标签,并设置当鼠标悬停在标签上时给出相应的提示信息。
import javax.swing.*;
import java.awt.*;
public class App {
public static void main(String[] args) {
JFrame jFrame=new JFrame("标签类窗口");
JLabel jLabel=new JLabel("我是一个标签",JLabel.CENTER);//创建标签类对象
jFrame.setLayout(null);//取消默认布局管理器
jFrame.setSize(300,200);//设置窗口的大小
Container c=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("我被设置为不透明");
Font font=new Font("楷体",Font.PLAIN,20);//创建字体对象
jLabel.setFont(font);//设置标签上的字体
jFrame.add(jLabel);
jFrame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
jFrame.setVisible(true);
}
}
7.在框架窗口中加入按钮
import javax.swing.*;
import java.awt.*;
public class App extends JFrame {
public static void main(String[] args) {
App jFrame=new App();
jFrame.setDefaultCloseOperation(EXIT_ON_CLOSE);
ImageIcon icon=new ImageIcon("images\\java.png");
JButton jButton=new JButton();
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.框架窗口的创建
import javax.swing.*;
import java.awt.*;
public class App {
static JFrame jFrame = new JFrame("这是一个Swing程序");//创建静态框架并设置标题
public static void main(String[] args) {
JLabel label = new JLabel("我是一个标签");//创建一个标签对象
jFrame.setSize(400, 300);//设置框架的大小
Image image=(new ImageIcon("images\\java.jpg")).getImage();//创建图标对象
jFrame.setIconImage(image);//设置窗口的显示图标
jFrame.setLocationRelativeTo(null);//设置窗口的位置
jFrame.add(label);//将标签对象加入到窗口中
jFrame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);//单击窗口的关闭按钮,结束程序
jFrame.setVisible(true);//设置窗口可见
}
}
来源:https://blog.csdn.net/zhangxia_/article/details/124775518
标签:Java,图形界面,设计
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
SpringBoot中使用 RabbitMQ的教程详解
2023-10-22 01:02:42
![](https://img.aspxhome.com/file/2023/9/113409_0s.jpg)
openFeign服务之间调用保持请求头信息处理方式
2022-11-07 23:45:21
MyBatis配置的应用与对比jdbc的优势
2023-08-27 07:03:47
![](https://img.aspxhome.com/file/2023/1/58281_0s.png)
自定义对象作为HashMap的Key问题
2022-11-06 02:52:30
Android 遍历SDCARD的文件夹并显示目录信息
2021-06-01 21:49:04
Java 遍历list和map的方法
2023-02-06 22:23:40
MyBatis多对多关联映射创建示例
2023-08-09 06:40:10
![](https://img.aspxhome.com/file/2023/0/128910_0s.jpg)
java8中:: 用法示例(JDK8双冒号用法)
2023-11-25 06:21:21
SpringMVC静态资源访问问题如何解决
2021-06-04 15:58:55
![](https://img.aspxhome.com/file/2023/3/105933_0s.png)
Android返回键功能的实现方法
2021-10-01 12:54:53
Java实现批量向mysql写入数据的方法
2023-11-05 20:51:21
@Value如何获取yml和properties配置参数
2021-12-12 13:24:01
![](https://img.aspxhome.com/file/2023/3/76473_0s.png)
Android实现打开各种文件的intent方法小结
2022-03-12 18:39:20
ShardingSphere数据分片算法及测试实战
2023-11-28 02:23:03
![](https://img.aspxhome.com/file/2023/8/59768_0s.png)
springboot+gradle 构建多模块项目的步骤
2023-02-19 00:33:31
![](https://img.aspxhome.com/file/2023/0/61050_0s.jpg)
利用C语言实现一个最简单的飞机游戏
2022-12-11 15:17:22
![](https://img.aspxhome.com/file/2023/8/117768_0s.gif)
FileStream常用的属性与方法总结
2023-03-18 17:47:05
详解Java中while和do-while循环、break的使用
2022-10-24 13:37:04
![](https://img.aspxhome.com/file/2023/4/75684_0s.jpg)
Android7.0开发实现Launcher3去掉应用抽屉的方法详解
2021-07-24 12:31:38
![](https://img.aspxhome.com/file/2023/0/138670_0s.jpg)
Java用数组实现循环队列的示例
2022-06-26 15:09:48