GUI编程基础02Swing(了解)

窗口、面板

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(“JFrameDemo”);

jFrame.add(jLabel);

//文本居中

jLabel.setHorizontalAlignment(SwingConstants.CENTER);

//容器实例化

Container contentPane = jFrame.getContentPane();

contentPane.setBackground(Color.yellow);

//关闭事件

jFrame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);

}

public static void main(String[] args) {

//建立一个窗口

new JFrameDemo().init();

}

}

在这里插入图片描述

弹窗

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.setSize(700, 500);

this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);

//JFrame 放东西

Container container = this.getContentPane();

//绝对布局

container.setLayout(null);

//按钮

JButton jButton = new JButton(“点击弹出一个对话框”);

jButton.setBounds(30, 30, 200, 50);

//点击按钮的时候弹出弹窗

jButton.addActionListener(new ActionListener() {//监听器

@Override

public void actionPerformed(ActionEvent e) {

//弹窗

new MyDialogDemo();

}

});

container.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 container = this.getContentPane();

container.add(new JLabel(“警告⚠”));

}

}

在这里插入图片描述

标签

图标icon

import javax.swing.*;

import java.awt.*;

//图标

public class IconDemo extends JFrame implements Icon {

private int width;

private int height;

public IconDemo() {

//无参构造

}

public IconDemo(int width, int height) {

//有参构造

this.width = width;

this.height = height;

}

public void init() {

IconDemo iconDemo = new IconDemo(15, 15);

//图标放在标签上,也可以放在按钮上

JLabel jLabel = new JLabel(“icontest”, iconDemo, SwingConstants.CENTER);

Container container = getContentPane();

container.add(jLabel);

this.setVisible(true);

this.pack();

}

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, height);

}

@Override

public int getIconWidth() {

return this.width;

}

@Override

public int getIconHeight() {

return this.height;

}

}

在这里插入图片描述

图片icon

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(“tx.jpg”);

ImageIcon imageIcon = new ImageIcon(url);

jLabel.setIcon(imageIcon);

jLabel.setHorizontalAlignment(SwingConstants.CENTER);

Container container = getContentPane();

container.add(jLabel);

setVisible(true);

setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);

setBounds(100,100,500,500);

}

public static void main(String[] args) {

new ImageIconDemo();

}

}

在这里插入图片描述

面板

import javax.swing.*;

import java.awt.*;

public class JPanelDemo extends JFrame {

public JPanelDemo() {

Container container = this.getContentPane();

container.setLayout(new GridLayout(2,1,10,10));//10上下间距

JPanel jPanel1 = new JPanel(new GridLayout(1,3));

jPanel1.add(new JButton(“1”));

jPanel1.add(new JButton(“1”));

jPanel1.add(new JButton(“1”));

container.add(jPanel1);

this.setVisible(true);

this.setSize(500,500);

this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);

}

public static void main(String[] args) {

new JPanelDemo();

}

}

在这里插入图片描述

JScrollPanel(滚动条)

import javax.swing.*;

import java.awt.*;

public class JScrollDemo extends JFrame {

public JScrollDemo() {

Container container = this.getContentPane();

//文本域

JTextArea jTextArea = new JTextArea(20,50);

jTextArea.setText(“欢迎”);

JScrollPane jScrollPane = new JScrollPane(jTextArea);

container.add(jScrollPane);

this.setVisible(true);

this.setBounds(100,100,300,350);

this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);

}

public static void main(String[] args) {

new JScrollDemo();

}

}

在这里插入图片描述

按钮

图片按钮

import javax.swing.*;

import java.awt.*;

import java.net.URL;

public class JButtonDemo01 extends JFrame {

public JButtonDemo01() {

Container container = this.getContentPane();

//将一个图片变为图标

URL resource = JButtonDemo01.class.getResource(“tx.jpg”);

ImageIcon imageIcon = new ImageIcon(resource);

//把这个图标放在按钮上

JButton jButton = new JButton();

jButton.setIcon(imageIcon);//设置图标

jButton.setToolTipText(“这是一个图片按钮”);

container.add(jButton);

this.setVisible(true);

this.setSize(500,300);

this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);

}

public static void main(String[] args) {

new JButtonDemo01();

}

}

在这里插入图片描述

单选按钮

import javax.swing.*;

import java.awt.*;

import java.net.URL;

public class JButtonDemo02 extends JFrame {

public JButtonDemo02() {

Container container = this.getContentPane();

//将一个图片变为图标

URL resource = JButtonDemo01.class.getResource(“tx.jpg”);

ImageIcon imageIcon = new ImageIcon(resource);

//单选框

JRadioButton jRadioButton01 = new JRadioButton(“JRadioButton01”);

JRadioButton jRadioButton02 = new JRadioButton(“JRadioButton02”);

JRadioButton jRadioButton03 = new JRadioButton(“JRadioButton03”);

//单选框只能选择一个,要分组,一个组中只能选择一个

ButtonGroup buttonGroup = new ButtonGroup();

buttonGroup.add(jRadioButton01);

buttonGroup.add(jRadioButton02);

buttonGroup.add(jRadioButton03);

总结

三套“算法宝典”

28天读完349页,这份阿里面试通关手册,助我闯进字节跳动

算法刷题LeetCode中文版(为例)

人与人存在很大的不同,我们都拥有各自的目标,在一线城市漂泊的我偶尔也会羡慕在老家踏踏实实开开心心养老的人,但是我深刻知道自己想要的是一年比一年有进步。

最后,我想说的是,无论你现在什么年龄,位于什么城市,拥有什么背景或学历,跟你比较的人永远都是你自己,所以明年的你看看与今年的你是否有差距,不想做咸鱼的人,只能用尽全力去跳跃。祝愿,明年的你会更好!

由于篇幅有限,下篇的面试技术攻克篇只能够展示出部分的面试题,详细完整版以及答案解析,有需要的可以关注

  • 4
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值