使用javaGUI编写检测是否有网

说明:

  • 启动后会置顶出现一弹窗
  • 弹窗为黑色
    • 一直为黑色表明没有网络
    • 黑红交替为有网络
  • 鼠标点击可以移动图标
  • 三击结束任务

生成的jar包

使用javaGUI编写检测是否有网 simplejava.jar

生成的exe文件

使用javaGUI编写检测是否有网 网络检测.exe


代码如下

import java.awt.Color;
import java.awt.Cursor;
import java.awt.Point;
import java.awt.event.MouseEvent;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

import javax.swing.JLabel;
import javax.swing.JWindow;
import javax.swing.event.MouseInputListener;

/**
 * 检测是否存在网络
 *
 * @author Jeby Sun
 */
public class MyFrame extends JWindow {

    private static final long serialVersionUID = 1L;

    JLabel titleLbl;

    public MyFrame() throws IOException {
        int waH = 50;
        //设置背景颜色不能直接调用其setBackground方法,而要设置其ContentPane的背景颜色。
        this.getContentPane().setBackground(Color.black);
        this.setBounds(10, 10, waH, waH);
        this.setLayout(null);

        titleLbl = new JLabel();
        titleLbl.setOpaque(true);
        titleLbl.setBackground(Color.BLACK);
        titleLbl.setBounds(0, 0, waH, waH);
        this.add(titleLbl);
        //鼠标事件处理类
        MouseEventListener mouseListener = new MouseEventListener(this);
        titleLbl.addMouseListener(mouseListener);
        titleLbl.addMouseMotionListener(mouseListener);
        this.setAlwaysOnTop(true);
        this.setVisible(true);
        Boolean flag = true;
        while (true){
            Process process = Runtime.getRuntime().exec("ping www.baidu.com");
            BufferedReader bufferedReader = new BufferedReader(
                    new InputStreamReader(process.getInputStream(), "GBK"));
            String line = null;
            while((line=bufferedReader.readLine()) != null) {
                if (line!=null&&line.contains("的回复")){
                    flag = !flag;
                    if (flag){
                        titleLbl.setBackground(Color.RED);
                    }else {
                        titleLbl.setBackground(Color.BLACK);
                    }
                }
            }
            flag = true;
            titleLbl.setBackground(Color.BLACK);
        }
    }

    /**
     * 鼠标事件处理
     *
     * @author Jeby Sun
     */
    class MouseEventListener implements MouseInputListener {

        Point origin;
        //鼠标拖拽想要移动的目标组件
        MyFrame frame;

        public MouseEventListener(MyFrame frame) {
            this.frame = frame;
            origin = new Point();
        }

        @Override
        public void mouseClicked(MouseEvent e) {
            int clickTimes = e.getClickCount();
            if (clickTimes == 3) {
                System.exit(0);
            }
        }

        /**
         * 记录鼠标按下时的点
         */
        @Override
        public void mousePressed(MouseEvent e) {
            origin.x = e.getX();
            origin.y = e.getY();
        }

        @Override
        public void mouseReleased(MouseEvent e) {
        }

        /**
         * 鼠标移进标题栏时,设置鼠标图标为移动图标
         */
        @Override
        public void mouseEntered(MouseEvent e) {
            this.frame.setCursor(Cursor.getPredefinedCursor(Cursor.MOVE_CURSOR));
        }

        /**
         * 鼠标移出标题栏时,设置鼠标图标为默认指针
         */
        @Override
        public void mouseExited(MouseEvent e) {
            this.frame.setCursor(Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR));
        }

        /**
         * 鼠标在标题栏拖拽时,设置窗口的坐标位置
         * 窗口新的坐标位置  = 移动前坐标位置+(鼠标指针当前坐标-鼠标按下时指针的位置)
         */
        @Override
        public void mouseDragged(MouseEvent e) {
            Point p = this.frame.getLocation();
            this.frame.setLocation(
                    p.x + (e.getX() - origin.x),
                    p.y + (e.getY() - origin.y));
        }

        @Override
        public void mouseMoved(MouseEvent e) {
        }

    }


    public static void main(String[] args) throws IOException {
        new MyFrame();
    }

}
好的,下面是一个简单的ATM界面的Java代码示例: ```java import javax.swing.*; import java.awt.event.*; public class ATM implements ActionListener { JFrame frame; JLabel welcomeLabel, cardLabel, pinLabel; JTextField cardField, pinField; JButton enterButton, clearButton; public ATM() { frame = new JFrame("ATM"); frame.setSize(400, 300); frame.setLayout(null); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); welcomeLabel = new JLabel("Welcome to the ATM"); welcomeLabel.setBounds(140, 20, 200, 20); frame.add(welcomeLabel); cardLabel = new JLabel("Card Number:"); cardLabel.setBounds(50, 80, 100, 20); frame.add(cardLabel); cardField = new JTextField(); cardField.setBounds(150, 80, 150, 20); frame.add(cardField); pinLabel = new JLabel("PIN:"); pinLabel.setBounds(50, 120, 100, 20); frame.add(pinLabel); pinField = new JPasswordField(); pinField.setBounds(150, 120, 150, 20); frame.add(pinField); enterButton = new JButton("Enter"); enterButton.setBounds(100, 180, 80, 20); enterButton.addActionListener(this); frame.add(enterButton); clearButton = new JButton("Clear"); clearButton.setBounds(200, 180, 80, 20); clearButton.addActionListener(this); frame.add(clearButton); frame.setVisible(true); } public void actionPerformed(ActionEvent e) { if (e.getSource() == enterButton) { String cardNumber = cardField.getText(); String pin = pinField.getText(); // TODO: 进行ATM操作,比如检查卡号和PIN是否正确,显示余额等等 JOptionPane.showMessageDialog(frame, "ATM operation complete"); } else if (e.getSource() == clearButton) { cardField.setText(""); pinField.setText(""); } } public static void main(String[] args) { new ATM(); } } ``` 这个ATM界面使用Java Swing库来构建。它包括一个欢迎标签、一个卡号文本框、一个PIN码密码框和两个按钮:输入和清除。 当用户单击“输入”按钮时,将获取卡号和PIN字段的值,并执行ATM操作。在这个示例中,我只是显示了一个简单的消息框,但在实际应用中,你可以进行各种操作,例如检查PIN是否正确,显示余额,提取现金等等。 当用户单击“清除”按钮时,将重置卡号和PIN字段。 注意:这个示例只是一个非常简单的ATM界面的实现,实际上ATM系统需要更复杂的逻辑来实现。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值