GUI编程
AWT
第一个窗口
Frame frame = new Frame("我的图形窗口");
//设置可见性
frame.setVisible(true);
//设置拆窗口大小
frame.setSize(400,800);
//设置背景颜色
frame.setBackground(new Color(232, 67, 57));
//弹出初始位置
frame.setLocation(200,200);
//设置大小固定
frame.setResizable(false);
Panel解决关闭问题(panel无法单独显示,必须添加到某个容器中)
public static void main(String[] args) {
Frame frame = new Frame();
//布局
Panel panel = new Panel();
//设置布局
frame.setLayout(null);
//坐标
frame.setBounds(300,200,500,500);
frame.setBackground(new Color(9, 14, 232));
//panel设置坐标,相对与frame
panel.setBounds(50,50,400,400);
//设置颜色
panel.setBackground(new Color(85, 232, 47));
//添加
frame.add(panel);
//设置可见性
frame.setVisible(true);
//监听关闭窗口
//用到了适配器模式
frame.addWindowListener(new WindowAdapter() {
布局管理器
-
流式布局
public static void main(String[] args) {
Frame frame = new Frame();
//按钮组件
Button button1 = new Button("1");
Button button2 = new Button("2");
Button button3 = new Button("3");
//设置成流式布局
//frame.setLayout(new FlowLayout());
frame.setLayout(new FlowLayout(FlowLayout.LEADING));
frame.setSize(200,200);
//把按钮添加上去
frame.add(button1);
frame.add(button2);
frame.add(button3);
frame.setVisible(true);
} -
东西南北中布局
public static void main(String[] args) {
Frame frame = new Frame("东西南北中布局");
Button button1 = new Button("1");
Button button2 = new Button("2");
Button button3 = new Button("3");
Button button4 = new Button("4");
Button button5 = new Button("5");
frame.add(button1,BorderLayout.EAST);
frame.add(button2,BorderLayout.WEST);
frame.add(button3,BorderLayout.SOUTH);
frame.add(button4,BorderLayout.NORTH);
frame.add(button5,BorderLayout.CENTER);
frame.setSize(500,500);
frame.setVisible(true);
} -
表格式布局
public static void main(String[] args) {
Frame frame = new Frame();
Button button1 = new Button("1");
Button button2 = new Button("2");
Button button3 = new Button("3");
Button button4 = new Button("4");
frame.setLayout(new GridLayout(3,2));
frame.add(button1);
frame.add(button2);
frame.add(button3);
frame.add(button4);
frame.pack();//java函数,自动填充
frame.setVisible(true);
}
事件监听
public static void main(String[] args) {
//按下按钮,触发事件
Frame frame = new Frame();
Button button = new Button();
MyActionLisrener myActionLisrener = new MyActionLisrener();
button.addActionListener(myActionLisrener);
frame.add(button,BorderLayout.CENTER);
frame.pack();
frame.setVisible(true);
}
//事件监听
class MyActionLisrener implements ActionListener{
输入框
class MyFrame1 extends Frame{
public MyFrame1(){
TextField textField = new TextField();
add(textField);
//监听文本框
MyActionListener actionListener = new MyActionListener();
textField.addActionListener(actionListener);
setVisible(true);
pack();
}
}
class MyActionListener implements ActionListener{
简单计算机
//计算机
class Calculator extends Frame{
public Calculator(){
//3个文本框
TextField num1 = new TextField(10);
TextField num2 = new TextField(10);
TextField num3 = new TextField(20);
//1个按钮
Button button = new Button("=");
button.addActionListener(new MyCalculatorListener(num1,num2,num3));
//1个标签
Label label = new Label("+");
//流式布局
setLayout(new FlowLayout());
add(num1);
add(label);
add(num2);
add(button);
add(num3);
pack();
setVisible(true);
}
}
//监听器
class MyCalculatorListener implements ActionListener{
private TextField num1;
private TextField num2;
private TextField num3;
//获得三个变量
public MyCalculatorListener(TextField num1, TextField num2, TextField num3){
this.num1 = num1;
this.num2 = num2;
this.num3 = num3;
}
画笔
class MyPaint extends Frame{
public void loadFrame(){
setBounds(200,200,200,200);
setVisible(true);
}
//画笔
@Override
public void paint(Graphics graphics){
//super.paint(graphics);
//画笔颜色。可以画画
graphics.setColor(Color.blue);
graphics.drawOval(100,100,100,100);
graphics.fillOval(100,100,100,100);
graphics.setColor(Color.cyan);
graphics.fillRect(200,200,200,200);
}
}
画图鼠标监听事件
class MyFrame2 extends Frame{
//画画要画笔,监听鼠标,需要集合存储这个点
ArrayList points;
public MyFrame2(String name) {
super(name);
setBounds(200,200,200,200);
setSize(200,200);
//存储鼠标点
points = new ArrayList<>();
//鼠标监听器
this.addMouseListener(new MyMouseListener());
setVisible(true);
}
@Override
public void paint(Graphics graphics){
//画画监听鼠标事件
//官方推荐迭代器循环
Iterator iterator = points.iterator();
while (iterator.hasNext()){
Point point = (Point) iterator.next();
graphics.setColor(Color.magenta);
graphics.fillOval(point.x,point.y,10,10);
}
}
//将点添加到页面上
public void addPaint(Point point){
points.add(point);
}
//使用适配器模式
private class MyMouseListener extends MouseAdapter {
//鼠标有三个事件 按下,谈起,按住不放
@Override
public void mousePressed(MouseEvent e){
MyFrame2 myFrame2 = (MyFrame2) e.getSource();
//鼠标点的点
myFrame2.addPaint(new Point(e.getX(),e.getY()));
//每次点击鼠标都画一次
myFrame2.repaint();//每次都刷新一次页面
}
}
键盘监听
class KeyFrame extends Frame{
public KeyFrame(){
setBounds(1200,300,400,500);
setVisible(true);
this.addKeyListener(new KeyAdapter(){
public void keyPressed(KeyEvent e) {
//获得键盘按下的哪个键
int keyCode = e.getKeyCode();
if(keyCode == KeyEvent.VK_UP){
System.out.println("你按下上建");
}
}
});
}
}
Swing
第一个窗口
class MyJFrame extends JFrame{
public void init(){
//顶级窗口
JFrame jFrame = new JFrame("JFrame");
this.setBounds(200,200,200,200);
this.setVisible(true);
JLabel jLabel = new JLabel("欢迎欢迎欢迎欢迎欢迎欢迎");
add(jLabel);
//让文字居中
jLabel.setHorizontalAlignment(SwingConstants.CENTER);
//创建一个新的容器
Container contentPane = this.getContentPane();
contentPane.setBackground(Color.yellow);
//关闭事件
jFrame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
}
弹窗
public class DialogDemo extends JFrame {
public static void main(String[] args) {
new DialogDemo();
}
public DialogDemo(){
this.setVisible(true);
this.setSize(500,500);
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
//JFrame容器
Container contentPane = this.getContentPane();
//绝对布局
contentPane.setLayout(null);
//按钮
JButton button = new JButton("弹出框");
button.setBounds(30,30,300,300);
//点击按钮的时候弹出弹框
button.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
//弹窗
new MyDialogDemo();
}
});
contentPane.add(button);
}
}
//弹窗窗口
class MyDialogDemo extends JDialog{
public MyDialogDemo(){
this.setVisible(true);
this.setBounds(100,100,100,100);
Container contentPane = this.getContentPane();
contentPane.setLayout(null);
contentPane.add(new Label("java"));
}
}
面板
public class JPanelDemo extends JFrame {
public static void main(String[] args) {
new JPanelDemo();
}
public JPanelDemo() {
//创建一个面板
Container contentPane = this.getContentPane();
contentPane.setLayout(new GridLayout(2,1,10,10));
JPanel jPanel = new JPanel(new GridLayout(1,3));
jPanel.add(new JButton("1"));
jPanel.add(new JButton("2"));
jPanel.add(new JButton("3"));
contentPane.add(jPanel);
this.setSize(200,200);
this.setVisible(true);
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
}
滚动条
public class JScrollDemo extends JFrame {
public static void main(String[] args) {
new JScrollDemo();
}
public JScrollDemo() {
Container contentPane = this.getContentPane();
JTextArea jTextArea = new JTextArea(20, 50);
jTextArea.setText("你好你好你好你好你好你好你好");
//创建一个面板
JScrollPane jScrollPane = new JScrollPane(jTextArea);
contentPane.add(jScrollPane);
this.setSize(300,300);
this.setBounds(100,100,100,100);
this.setVisible(true);
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
}
按钮
将图片放到按钮上
public class ButtonDemo extends JFrame {
public ButtonDemo(){
Container contentPane = this.getContentPane();
//将一个图片编一个图标
URL typary = ButtonDemo.class.getResource("typary.png");
Icon icon = new ImageIcon(typary);
//将图标放到按钮上
JButton button = new JButton();
button.setIcon(icon);
//鼠标放上去提示问题,但是没显示
button.setToolTipText("图片按钮");
contentPane.add(button);
this.setVisible(true);
this.setSize(100,100);
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
new ButtonDemo();
}
}
单选按钮
public class ButtonDemo extends JFrame {
public ButtonDemo(){
Container contentPane = this.getContentPane();
//将一个图片编一个图标
URL typary = ButtonDemo.class.getResource("typary.png");
Icon icon = new ImageIcon(typary);
//将图标放到按钮上
JButton button = new JButton();
button.setIcon(icon);
//单选框
JRadioButton button1 = new JRadioButton("1");
JRadioButton button2 = new JRadioButton("2");
JRadioButton button3 = new JRadioButton("3");
//只能选一个,将按钮分组,一个组只能选一个。不分组可以多选
ButtonGroup buttonGroup = new ButtonGroup();
buttonGroup.add(button1);
buttonGroup.add(button2);
buttonGroup.add(button3);
contentPane.add(button1,BorderLayout.CENTER);
contentPane.add(button2,BorderLayout.NORTH);
contentPane.add(button3,BorderLayout.SOUTH);
this.setVisible(true);
this.setSize(100,100);
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
new ButtonDemo();
}
}
复选框
public class ButtonDemo extends JFrame {
public ButtonDemo(){
Container contentPane = this.getContentPane();
//将一个图片编一个图标
URL typary = ButtonDemo.class.getResource("typary.png");
Icon icon = new ImageIcon(typary);
//将图标放到按钮上
JButton button = new JButton();
button.setIcon(icon);
//多选框
JCheckBox jCheckBox1 = new JCheckBox("1");
JCheckBox jCheckBox2 = new JCheckBox("2");
contentPane.add(jCheckBox1);
contentPane.add(jCheckBox2);
contentPane.add(jCheckBox1,BorderLayout.SOUTH);
contentPane.add(jCheckBox2,BorderLayout.EAST);
this.setVisible(true);
this.setSize(100,100);
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
new ButtonDemo();
}
}
下拉框
public class TestComboboxDemo extends JFrame {
public static void main(String[] args) {
new TestComboboxDemo();
}
public TestComboboxDemo() {
Container contentPane = this.getContentPane();
JComboBox status = new JComboBox();
status.addItem(null);
status.addItem("上映");
status.addItem("下架");
contentPane.add(status);
this.setSize(300,300);
this.setVisible(true);
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
}
列表框
public class TestComboboxDemo extends JFrame {
public static void main(String[] args) {
new TestComboboxDemo();
}
public TestComboboxDemo() {
Container contentPane = this.getContentPane();
//生成列表内容
String[] contents = {"1","2","3"};
//列表放入内容
JList<Object> jList = new JList<>(contents);
contentPane.add(jList);
this.setSize(300,300);
this.setVisible(true);
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
}
文本框
public class TestComboboxDemo extends JFrame {
public static void main(String[] args) {
new TestComboboxDemo();
}
public TestComboboxDemo() {
Container contentPane = this.getContentPane();
JTextField jTextField1 = new JTextField("你好");
JTextField jTextField2 = new JTextField("Aseed",40);
contentPane.add(jTextField1,BorderLayout.EAST);
contentPane.add(jTextField2,BorderLayout.SOUTH);
this.setSize(300,300);
this.setVisible(true);
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
}
密码框
public class TestComboboxDemo extends JFrame {
public static void main(String[] args) {
new TestComboboxDemo();
}
public TestComboboxDemo() {
Container contentPane = this.getContentPane();
JPasswordField jPasswordField1 = new JPasswordField("你好");
JPasswordField jPasswordField2 = new JPasswordField("Aseed",40);
//将文本框中的字符转换成 *
jPasswordField1.setEchoChar('*');
jPasswordField2.setEchoChar('=');
contentPane.add(jPasswordField1,BorderLayout.EAST);
contentPane.add(jPasswordField2,BorderLayout.SOUTH);
this.setSize(300,300);
this.setVisible(true);
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
}
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 【译】Visual Studio 中新的强大生产力特性
· 【设计模式】告别冗长if-else语句:使用策略模式优化代码结构
· AI与.NET技术实操系列(六):基于图像分类模型对图像进行分类