GUI
Swing
窗口
用Swing写的窗口自带隐藏窗口
点击关闭不会关闭窗口,会让窗口隐藏
package com.yu.lesson04;
import javax.swing.*;
import java.awt.*;
public class JframeDemo {
//init();初始化
public void init(){
JFrame jframe = new JFrame("这是一个JFrame窗口");
jframe.setVisible(true);
jframe.setBounds(100,100,200,200);
jframe.setBackground(Color.BLACK);
JLabel jLabel = new JLabel("欢迎来到我的世界");
jframe.add(jLabel);
//关闭事件
jframe.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
//建立一个窗口
new JframeDemo().init();
}
}
标签文字居中
package com.yu.lesson04;
import javax.swing.*;
import java.awt.*;
public class JframeDemo02 {
public static void main(String[] args) {
new Myframe().init();
}
}
class Myframe extends JFrame{
public void init(){
//获得容器
Container contentPane = this.getContentPane();
contentPane.setBackground(Color.GREEN);
this.setVisible(true);
this.setBounds(100,100,200,200);
JLabel jLabel = new JLabel("欢迎来到我的世界");
this.add(jLabel);
//文本居中
jLabel.setHorizontalAlignment(SwingConstants.CENTER);
}
}
JDialog 弹窗
JDialog,用来被弹出,默认就有关闭事件!
package com.yu.lesson04;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
//主窗口
public class DialogDemo extends JFrame {
public DialogDemo(){
this.setVisible(true);
this.setBounds(100,200,300,400);
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
//JFrame 放东西,容器
Container contentPane = this.getContentPane();
//绝对布局
contentPane.setLayout(null);
//按钮
JButton jButton = new JButton("点击弹窗对话框");
jButton.setBounds(30,30,200,50);
//点击这个按钮填出一个弹窗
jButton.addActionListener(new ActionListener() {
//监听器
@Override
public void actionPerformed(ActionEvent e) {
//弹窗
new MyDialogDemo();
}
});
contentPane.add(jButton);
}
public static void main(String[] args) {
new DialogDemo();
}
}
//弹窗的窗口
class MyDialogDemo extends JDialog{
public MyDialogDemo(){
this.setVisible(true);
this.setBounds(100,100,500,500);
Container contentPane = this.getContentPane();
contentPane.setLayout(null);
contentPane.add(new Label("Java Swing 图形化"));
}
}
标签
label
new Label("XXXX");
new JLabel("XXXX");
图标 ICON
package com.yu.lesson04;
import javax.swing.*;
import java.awt.*;
//图标是一个接口需要实现类,Frame继承
public class IconDemo extends JFrame implements Icon {
private int width;
private int heigth;
public IconDemo(){}//无参构造
public IconDemo(int width,int heigth){
this.heigth = heigth;
this.width = width;
}
public void init(){
IconDemo iconDemo = new IconDemo(15, 15);
//图标放在标签上,也可以放在按钮上!
JLabel JLabel = new JLabel("icontest", iconDemo, SwingConstants.CENTER);
Container contentPane = getContentPane();
contentPane.add(JLabel);
this.setBounds(100,100,500,500);
this.setVisible(true);
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
new IconDemo().init();
}
@Override
public void paintIcon(Component c, Graphics g, int x, int y) {
g.fillOval(x,y,width,heigth);
}
@Override
public int getIconWidth() {
return this.width;
}
@Override
public int getIconHeight() {
return this.heigth;
}
}
图片图标 picture icon
package com.yu.lesson04;
import javax.swing.*;
import java.awt.*;
import java.net.URL;
public class ImageIconDemo extends JFrame {
public ImageIconDemo(){
//获取图片地址
JLabel jLabel = new JLabel("ImageIcon");
URL url = ImageIconDemo.class.getResource("mn.jpg");
ImageIcon imageIcon = new ImageIcon(url);//注意命名不要冲突,自己的和官方的
jLabel.setIcon(imageIcon);
jLabel.setHorizontalAlignment(SwingConstants.CENTER);
Container contentPane = getContentPane();
contentPane.add(jLabel);
setVisible(true);
setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
setBounds(100,100,200,200);
}
public static void main(String[] args) {
new ImageIconDemo();
}
}
面板
按钮
- 单选按钮
- 复选按钮
列表
文本框
密码框
文本域
文本框
也属于烂尾了吧有机会补充完整
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 【自荐】一款简洁、开源的在线白板工具 Drawnix
· 园子的第一款AI主题卫衣上架——"HELLO! HOW CAN I ASSIST YOU TODAY
· Docker 太简单,K8s 太复杂?w7panel 让容器管理更轻松!