窗口、面板
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);
总结
三套“算法宝典”
算法刷题LeetCode中文版(为例)
人与人存在很大的不同,我们都拥有各自的目标,在一线城市漂泊的我偶尔也会羡慕在老家踏踏实实开开心心养老的人,但是我深刻知道自己想要的是一年比一年有进步。
最后,我想说的是,无论你现在什么年龄,位于什么城市,拥有什么背景或学历,跟你比较的人永远都是你自己,所以明年的你看看与今年的你是否有差距,不想做咸鱼的人,只能用尽全力去跳跃。祝愿,明年的你会更好!