首页 > 其他分享 >GUI-4及总结

GUI-4及总结

时间:2024-09-01 09:21:17浏览次数:4  
标签:总结 java GUI awt add import new public

Swing

标签

Icon标签

将绘制的图形赋给JLabel

package com.bao.lesson10;

import javax.swing.*;
import java.awt.*;

public class MyIcon extends JFrame implements Icon {
    //属性
    private int width;
    private int height;
    public MyIcon() {};//无参构造

    public MyIcon(int width,int height) {
        this.width = width;
        this.height = height;
    }
    public void init(){
        Container contentPane = getContentPane();

        MyIcon myIcon = new MyIcon(15,15);

        JLabel label = new JLabel("icontest", myIcon, SwingConstants.CENTER);

        contentPane.add(label);

        this.setVisible(true);
        this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE );

    }

    @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;
    }

    public static void main(String[] args) {
        new MyIcon().init();
    }
}

在这里插入图片描述

ImageIcon标签

将图片附在JLabel上

package com.bao.lesson10;

import javax.swing.*;
import java.awt.*;
import java.net.URL;

public class MyImageIcon extends JFrame {
    public MyImageIcon() {

        JLabel jLabel = new JLabel("Imageicon");
        //获取图片的地址
        URL url = MyImageIcon.class.getResource("icon_机票_nor.png");

        ImageIcon imageIcon = new ImageIcon(url);
        jLabel.setIcon(imageIcon);
        jLabel.setHorizontalAlignment(SwingConstants.CENTER);

        Container contentPane = getContentPane();
        contentPane.add(jLabel);

        setVisible(true);
        setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        setBounds(100,100,200,200);
    }

    public static void main(String[] args) {
        new MyImageIcon();
    }
}

在这里插入图片描述

[!IMPORTANT]

  • 可以通过 MyImageIcon.class.getResource(“icon_机票_nor.png”)这个方法来获取本包下的图片地址
  • JFrame的背景色,增加组件都需要实例化容器,在容器上进行才可以

面板 JScrollPane及文本域

相当于Frame中的Panel

package com.bao.lesson10;

import javax.swing.*;
import java.awt.*;

public class JScrollPanel extends JFrame {
    public JScrollPanel(){
        Container contentPane = this.getContentPane();

        //文本域
        JTextArea textArea = new JTextArea(20, 50);
        textArea.setText("hello everybody");

        //Scroll面板
        JScrollPane jScrollPane = new JScrollPane(textArea);
        contentPane.add(jScrollPane);


        this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        this.setVisible(true);
        this.setBounds(100, 100, 500, 500);

    }

    public static void main(String[] args) {
        new JScrollPanel();
    }
}

文本域在特定情况下会产生滚动条,可以进行滚动

在这里插入图片描述

按钮

图片按钮

按钮里面附着着图片

package com.bao.lesson10;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.net.URL;

public class ButtonDemo extends JFrame {
    public ButtonDemo(){
        Container container = this.getContentPane();//获得容器
        JButton press = new JButton("press");
        press.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                System.out.println("pressed");
            }
        });


        this.setVisible(true);
        this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        container.setBackground(Color.CYAN);
        this.setBounds(100,100,500,500);
        container.setBounds(100, 100, 500, 500);


        container.add(press);

        URL url = ButtonDemo.class.getResource("icon_机票_nor.png");
        ImageIcon icon = new ImageIcon(url);

        press.setIcon(icon);
        press.setHorizontalAlignment(SwingConstants.CENTER);
    }

    public static void main(String[] args) {
        new ButtonDemo();
    }
}

实现效果

在这里插入图片描述

单选框

package com.bao.lesson10;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.net.URL;

public class ButtonDemo1 extends JFrame {
    public ButtonDemo1(){
        Container container = this.getContentPane();//获得容器
        JRadioButton press1 = new JRadioButton("lalala1");
        JRadioButton press2 = new JRadioButton("lalala2");
        JRadioButton press3 = new JRadioButton("lalala3");

        //单选框,需要分组,不然会导致多个都可以选择 一个组中只能选一个
        ButtonGroup group = new ButtonGroup();
        group.add(press1);
        group.add(press2);
        group.add(press3);


        this.setVisible(true);
        this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        container.setBackground(Color.CYAN);



        container.setLayout(new GridLayout(1,3,10,10));
        container.add(press1);
        container.add(press2);
        container.add(press3);
        this.pack();
        this.setBounds(100,100,500,500);




    }

    public static void main(String[] args) {
        new ButtonDemo1();
    }
}

实现效果

在这里插入图片描述

补充:

单选框需要进行分组,因为每个组别里,多个单选框只能选一个

复选框

package com.bao.lesson10;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.net.URL;

public class ButtonDemo2 extends JFrame {
    public ButtonDemo2(){
        Container container = this.getContentPane();//获得容器
        JCheckBox box1 = new JCheckBox("la");
        JCheckBox box2 = new JCheckBox("la");
        JCheckBox box3 = new JCheckBox("la");

        //多选框不需要愤分组,因为没有限制选择数量


        this.setVisible(true);
        this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        container.setBackground(Color.CYAN);



        container.setLayout(new GridLayout(1,3,10,10));
        container.add(box1);
        container.add(box2);
        container.add(box3);
        this.pack();
        this.setSize(500,500);




    }

    public static void main(String[] args) {
        new ButtonDemo2();
    }
}

在这里插入图片描述

下拉框 列表框

下拉框

package com.bao.lessonn11;

import javafx.scene.control.ComboBox;

import javax.swing.*;
import java.awt.*;

public class Select extends JFrame {
    public Select() {

        Container contentPane = this.getContentPane();

        JComboBox jComboBox = new JComboBox();

        //加下拉后的选项
        jComboBox.addItem("你的名字");
        jComboBox.addItem("言叶之庭");
        jComboBox.addItem("天气之子");

        contentPane.add(jComboBox);


        this.setVisible(true);
        this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        this.setSize(500,300);
    }

    public static void main(String[] args) {
        new Select();
    }
}

在这里插入图片描述

列表框

package com.bao.lessonn11;

import javax.swing.*;
import java.awt.*;

public class ul extends JFrame {
    public ul() {
        Container pane = this.getContentPane();

        //生成列表的内容 数组
        String[] content = {"1","2","3"};

        //列表中放入内容
        JList jList = new JList(content);

        pane.add(jList);

        this.setVisible(true);
        this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        this.setSize(500,300);
    }

    public static void main(String[] args) {
        new ul();
    }
}

在这里插入图片描述

文本框 密码框 文本域

文本框

package com.bao.lessonn11;

import javafx.scene.control.ComboBox;

import javax.swing.*;
import java.awt.*;

public class TextField extends JFrame {
    public TextField() {

        Container contentPane = this.getContentPane();

        contentPane.setLayout(new GridLayout(1,2));
        JTextField textField = new JTextField("hello", 30);
        JTextField textField1 = new JTextField("bye", 30);

        contentPane.add(textField);
        contentPane.add(textField1);

        this.setVisible(true);
        this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        this.setSize(500,300);
    }

    public static void main(String[] args) {
        new TextField();
    }
}

在这里插入图片描述

密码框

package com.bao.lessonn11;

import javafx.scene.control.ComboBox;

import javax.swing.*;
import java.awt.*;

public class TextField1 extends JFrame {
    public TextField1() {

        Container contentPane = this.getContentPane();


        //不规范,尽量在面板中设置组件
        JPasswordField pass = new JPasswordField("",30);

        contentPane.add(pass);


        this.pack();
        this.setVisible(true);
        this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        this.setSize(500,300);
    }

    public static void main(String[] args) {
        new TextField1();
    }
}

在这里插入图片描述

文本域

之前写过了

总结

在这里插入图片描述

标签:总结,java,GUI,awt,add,import,new,public
From: https://blog.csdn.net/wozhaonue_w/article/details/141776340

相关文章

  • 2024年8月总结及随笔之逝
    1. 回头看日更坚持了609天。读《零信任网络:在不可信网络中构建安全系统》更新完成读《软件开发安全之道:概率、设计与实施》开更并持续更新2023年至2024年8月底累计码字1463007字,累计日均码字2402字。2024年8月码字109278字,同比增长177.6%,环比增长27.3%,日均码字数3525字,累......
  • 2024.8.31 总结(集训 考 DP)
    今天依然是上午考DP,三个小时四道题。我觉得今天的题目较昨天更简单。考场上就想出了四个题,但是我以为T1\(O(n^3)\)的做法是暴力,想了好久T1也没想出更好的做法,于是开写,然后造数据测了测,发现跑得比较快(极限数据应该也是能在我的位置上那台电脑里1s内过的)。结果出分是330,......
  • 十大时间序列模型最强总结(六)Prophet
    六、Prophet1.原理Prophet是由Facebook开发的时间序列预测模型,专为处理具有强季节性、趋势变化以及缺失值和异常值的时间序列数据设计。它的核心思想是将时间序列数据分解为趋势、季节性和假期效应三个部分。2.核心公式推导:3.优缺点1)优点:适用于具有季节性......
  • Redis常见问题总结
    Redis常见问题总结参考小林codingRedis常见问题总结Redis集群架构所产生的问题及如何处理Java全栈知识体系Redis集群搭建目录1.认识Redis2.Redis应用场景3.Redis持久化4.Redis设计认识RedisRedis是一种开源的内存数据结构存储工具,用作分布式内存中的键值数据库......
  • 单参总结——心电信号
    心电波形的滤波及心率计算心电测量原理心脏通过一系列电活动驱动其收缩和放松,这些电活动可以通过电极在身体表面检测到。心脏的电活动主要包括:去极化:心脏的电信号从心房开始,经过心室,导致心脏的肌肉收缩。复极化:心脏肌肉放松,准备下一次收缩。心电波形心电图(ECG)波形中的......
  • SQL注入总结
    一、万能密码:什么是万能密码?用户进行用户名和密码验证时,网站需要查询数据库。查询数据库就是执行SQL语句。用户登录时,后台执行的数据库查询操作(SQL语句)是:【Selectuser_id,user_type,emailFromusersWhereuser_id=’用户名’Andpassword=’密码’】这里我们......
  • 每周总结
    YARN(YetAnotherResourceNegotiator):YARN是Hadoop的资源管理和作业调度系统。本周你可能深入了解了YARN的架构及其组件,包括ResourceManager和NodeManager。ResourceManager负责全局资源调度和作业调度,而NodeManager负责单个节点的资源管理和监控。通过YARN,Hadoop能够高效地分配集......
  • 9 张图总结 MySQL 架构
    原文:9张图总结一下MySQL架构前言目前大部分的后端开发人员对MySQL的理解可能停留在一个黑盒子阶段。对MySQL基本使用没什么问题,比如建库、建表、建索引,执行各种增删改查。所有很多后端开发人员眼中的MySQL如下图所示:导致在实际工作中碰到MySQL中死锁异常、SQL性能太差......
  • 数据库的多表联合查询 后面有命令和格式的总结
    多表联合查询实践创建表:MySQL[company]>createtableemployee6(  emp_idintauto_incrementprimarykeynotnull,  emp_namevarchar(50),  ageint,  dept_idint);QueryOK,0rowsaffected(0.65sec)查看表结构:MySQL[company]>d......
  • C++创建GUI按钮\文本框(Windows)不用QT!!!
    一定要的:#include<windows.h>剩下的: 过程:LRESULTCALLBACKWndProc(HWNDhwnd,UINTMessage,WPARAMwParam,LPARAMlParam){switch(Message){ caseWM_CREATE:{ /*略*///HWND名称=CreateWindow(类型,显示的字,WS_CHILD|WS_VISIBLE,x,y,宽,......