AWT
Frame窗口
package swing;
import java.awt.*;
public class TestFrame {
public static void main(String[] args) {
Frame frame = new Frame("第一个窗口");
frame.setVisible(true);
frame.setSize(400,400);
frame.setLocation(350,350);
frame.setBackground(Color.ORANGE);
frame.setResizable(false);
}
}

Panel面板
package swing;
import java.awt.*;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
public class TestPanel {
public static void main(String[] args) {
Frame frame = new Frame();
Panel panel = new Panel();
frame.setLayout(null);
frame.setBounds(300,300,500,500);
frame.setBackground(new Color(34, 144, 34));
frame.setVisible(true);
frame.setResizable(false);
panel.setBounds(100,100,300,300);
panel.setBackground(new Color(149, 24, 24));
frame.add(panel);
frame.addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
}
}

布局管理器
流式布局(FlowLayout)
package swing;
import java.awt.*;
public class TestFlowLayout {
public static void main(String[] args) {
Frame frame = new Frame();
frame.setSize(300, 300);
frame.setVisible(true);
Button button1 = new Button("button1");
Button button2 = new Button("button2");
Button button3 = new Button("button3");
Button button4 = new Button("button4");
frame.setLayout(new FlowLayout());
frame.add(button1);
frame.add(button2);
frame.add(button3);
frame.add(button4);
}
}

东西南北中布局(BorderLayout)
package swing;
import java.awt.*;
public class TestBorderLayout {
public static void main(String[] args) {
Frame frame = new Frame("TestBorderLayout");
Button east = new Button("east");
Button west = new Button("west");
Button south = new Button("south");
Button north = new Button("north");
Button center = new Button("center");
frame.add(east, BorderLayout.EAST);
frame.add(west, BorderLayout.WEST);
frame.add(south, BorderLayout.SOUTH);
frame.add(north, BorderLayout.NORTH);
frame.add(center, BorderLayout.CENTER);
frame.setVisible(true);
frame.setSize(300, 300);
}
}

表格布局(GridLayout)
package swing;
import java.awt.*;
public class TestGridLayout {
public static void main(String[] args) {
Frame frame = new Frame("TestGridLayout");
Button button1 = new Button("button1");
Button button2 = new Button("button2");
Button button3 = new Button("button3");
Button button4 = new Button("button4");
frame.setLayout(new GridLayout(2, 2));
frame.add(button1);
frame.add(button2);
frame.add(button3);
frame.add(button4);
frame.setVisible(true);
frame.setSize(300, 300);
}
}

按钮事件监听
package swing;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class TestActionEvent {
public static void main(String[] args) {
Frame frame = new Frame();
frame.setVisible(true);
frame.setSize(300,300);
Button button1 = new Button("button1");
Button button2 = new Button("button2");
frame.add(button1);
button1.addActionListener((ActionEvent e)->{
System.out.println(11);
});
MyAction action = new MyAction();
button2.addActionListener(action);
}
}
class MyAction implements ActionListener {
@Override
public void actionPerformed(ActionEvent e) {
}
}
输入框监听事件
package swing;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class TestTextField {
public static void main(String[] args) {
new MyFrame("文本框");
}
}
class MyFrame extends Frame {
public MyFrame() {
}
public MyFrame(String title) {
super(title);
TextField field = new TextField();
MyAction2 action = new MyAction2();
field.addActionListener(action);
field.setEchoChar('*');
add(field);
setVisible(true);
pack();
}
}
class MyAction2 implements ActionListener {
@Override
public void actionPerformed(ActionEvent e) {
TextField field = (TextField) e.getSource();
String text = field.getText();
System.out.println(text);
field.setText("");
}
}
简单的加法计算器
package swing;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
public class TestCalc {
public static void main(String[] args) {
new MyFrame3("计算器").start();
}
}
class MyFrame3 extends Frame {
public TextField num1;
public TextField num2;
public TextField num3;
public MyFrame3(String title) {
super(title);
}
public void start() {
setVisible(true);
setLayout(new FlowLayout());
num1 = new TextField(10);
num2 = new TextField(10);
num3 = new TextField(20);
Label label = new Label("+");
Button button = new Button("=");
add(num1);
add(label);
add(num2);
add(button);
add(num3);
MyAction3 action = new MyAction3();
button.addActionListener(action);
pack();
addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
}
private class MyAction3 implements ActionListener {
@Override
public void actionPerformed(ActionEvent e) {
double sum = Double.parseDouble(num1.getText()) + Double.parseDouble(num2.getText());
num3.setText(sum + "");
num1.setText("");
num2.setText("");
}
}
}

画笔 Paint
package swing;
import java.awt.*;
public class TestPaint {
public static void main(String[] args) {
new MyPaint().start();
}
}
class MyPaint extends Frame{
public void start(){
setVisible(true);
setBounds(300,200,600,600);
}
@Override
public void paint(Graphics g) {
g.setColor(Color.red);
g.fillOval(50,50,100,100);
}
}
窗口Frame监听事件
package swing;
import java.awt.*;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
public class TestWindow {
public static void main(String[] args) {
new MyFrame5().start();
}
}
class MyFrame5 extends Frame {
public void start() {
setVisible(true);
addWindowListener(new WindowAdapter() {
@Override
public void windowOpened(WindowEvent e) {
System.out.println("窗口打开了");
}
@Override
public void windowClosing(WindowEvent e) {
super.windowClosing(e);
System.exit(0);
}
@Override
public void windowClosed(WindowEvent e) {
super.windowClosed(e);
System.out.println("窗口正在关闭中");
}
@Override
public void windowActivated(WindowEvent e) {
super.windowActivated(e);
System.out.println("已选中窗口");
}
});
}
}
键盘监听事件
package swing;
import java.awt.*;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
public class TestKeyListener {
public static void main(String[] args) {
new MyFrame6().start();
}
}
class MyFrame6 extends Frame {
public void start() {
setVisible(true);
this.addKeyListener(new KeyAdapter() {
@Override
public void keyTyped(KeyEvent e) {
super.keyTyped(e);
}
@Override
public void keyPressed(KeyEvent e) {
super.keyPressed(e);
int keyCode = e.getKeyCode();
if (keyCode == KeyEvent.VK_UP) {
System.out.println("你按下了↑键");
}
}
@Override
public void keyReleased(KeyEvent e) {
super.keyReleased(e);
}
});
}
}
Swing
package swing;
import javax.swing.*;
import java.awt.*;
public class TestJFrame {
public static void main(String[] args) {
into();
}
public static void into(){
JFrame frame = new JFrame("标题");
frame.setBounds(200,200,300,300);
frame.setVisible(true);
JLabel label = new JLabel("文字标签");
label.setHorizontalAlignment(SwingConstants.CENTER);
frame.add(label);
Container contentPane = frame.getContentPane();
contentPane.setBackground(Color.BLUE);
frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
}
Swing 中的弹窗 JDialog
package swing;
import javax.swing.*;
import java.awt.*;
public class TestJDialog extends JFrame {
public TestJDialog(String title){
super(title);
setVisible(true);
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
Container contentPane = getContentPane();
contentPane.setLayout(null);
JButton button = new JButton("点击出现一个弹窗");
button.setBounds(30,30,200,50);
button.addActionListener((actionEvent)->{
new MyJDialog();
});
contentPane.add(button);
}
public static void main(String[] args) {
new TestJDialog("弹窗");
}
}
class MyJDialog extends JDialog{
public MyJDialog() {
setVisible(true);
setBounds(200,200,300,300);
Container contentPane = getContentPane();
contentPane.setLayout(null);
contentPane.add(new JLabel("哈哈哈"));
}
}
标签 JLabel
package swing;
import javax.swing.*;
import java.awt.*;
public class TestIcon extends JFrame implements Icon {
private int width;
private int height;
public TestIcon() {
}
public TestIcon(int width, int height) {
this.width = width;
this.height = height;
}
public void init(){
TestIcon testIcon = new TestIcon(15,15);
JLabel label = new JLabel("标签", testIcon, SwingConstants.CENTER);
Container contentPane = getContentPane();
contentPane.add(label);
this.setVisible(true);
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
this.setBounds(200,200,300,300);
}
public static void main(String[] args) {
new TestIcon().init();
}
@Override
public void paintIcon(Component c, Graphics g, int x, int y) {
g.fillOval(x, y, width, height);
}
@Override
public int getIconWidth() {
return width;
}
@Override
public int getIconHeight() {
return height;
}
}
图片 ImageIcon
package swing;
import javax.swing.*;
import java.net.URL;
public class TestImageIcon extends JFrame {
public TestImageIcon(){
JLabel label = new JLabel("标签");
URL url = TestImageIcon.class.getResource("sq.png");
ImageIcon icon = new ImageIcon(url);
this.setBounds(200,200,500,500);
label.setIcon(icon);
label.setHorizontalAlignment(SwingConstants.CENTER);
this.setVisible(true);
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
this.add(label);
}
public static void main(String[] args) {
new TestImageIcon();
}
}
package swing;
import javax.swing.*;
import java.awt.*;
public class TestJScroll extends JFrame {
public static void main(String[] args) {
new TestJScroll("标题");
}
public TestJScroll(String title){
super(title);
this.setVisible(true);
this.setBounds(200,200,300,300);
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
Container container = getContentPane();
JTextArea textArea = new JTextArea(20,50);
textArea.setText("这是一个文本域");
JScrollPane scrollPane = new JScrollPane(textArea);
container.add(scrollPane);
}
}
package swing;
import javax.swing.*;
import java.awt.*;
import java.net.URL;
public class TestJRadioButton extends JFrame {
public TestJRadioButton() {
this.setVisible(true);
this.setSize(500,500);
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
Container container = this.getContentPane();
JRadioButton button1 = new JRadioButton("男");
JRadioButton button2 = new JRadioButton("女");
JRadioButton button3 = new JRadioButton("人妖");
ButtonGroup group = new ButtonGroup();
group.add(button1);
group.add(button2);
group.add(button3);
container.add(button1,BorderLayout.CENTER);
container.add(button2,BorderLayout.NORTH);
container.add(button3,BorderLayout.SOUTH);
}
public static void main(String[] args) {
new TestJRadioButton();
}
}
复选按钮 JCheckBox
package swing;
import javax.swing.*;
import java.awt.*;
public class TestJCheckBox extends JFrame {
public TestJCheckBox() {
this.setVisible(true);
this.setSize(500,500);
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
Container container = this.getContentPane();
JCheckBox box1 = new JCheckBox("多选1");
JCheckBox box2 = new JCheckBox("多选2");
JCheckBox box3 = new JCheckBox("多选3");
container.add(box1,BorderLayout.CENTER);
container.add(box2,BorderLayout.NORTH);
container.add(box3,BorderLayout.SOUTH);
}
public static void main(String[] args) {
new TestJCheckBox();
}
}
下拉列表 JComboBox
package swing;
import javax.swing.*;
import java.awt.*;
public class TestJComboBox extends JFrame {
public TestJComboBox() {
this.setVisible(true);
this.setSize(150,100);
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
Container container = this.getContentPane();
JComboBox box = new JComboBox();
box.addItem("销量");
box.addItem("人数");
box.addItem("综合");
box.addItem("店铺");
container.add(box);
}
public static void main(String[] args) {
new TestJComboBox();
}
}
列表框
package swing;
import javax.swing.*;
import java.awt.*;
public class TestJComboBox extends JFrame {
public TestJComboBox() {
this.setVisible(true);
this.setSize(300,300);
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
Container container = this.getContentPane();
String[] str = {"1","2","3","5","8","9","7"};
JList list = new JList(str);
container.add(list);
}
public static void main(String[] args) {
new TestJComboBox();
}
}
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本
· Manus爆火,是硬核还是营销?
· 终于写完轮子一部分:tcp代理 了,记录一下
· 别再用vector<bool>了!Google高级工程师:这可能是STL最大的设计失误
· 单元测试从入门到精通