Swing GUI programming for java learning

Swing GUI programming for java learning

0x00 Preface

The previously used GUI is implemented based on AWT, but AWT is rarely used in real GUI writing.

Part I: AWT GUI programming for java learning

Part I: implementation of JSP dynamic and static killing free idea for Java security and server-side programming

0x01 swing overview

Difference between AWT and swing

When actually using java to develop graphical interface programs, AWT components are rarely used, and swing components are used most of the time. Swing is implemented in 100% pure Java and no longer depends on the GUI of the local platform, so it can maintain the same interface appearance on all platforms. Swing components independent of the local platform are called lightweight components; AWT components that depend on the local platform are called heavyweight components. Because all components of swing are implemented in Java and no longer call the GUI of the local platform, the display speed of swing graphical interface is slower than that of AWT graphical interface. However, compared with the rapidly developing hardware facilities, this small speed difference is not harmful.

Features of swing

模型(Model): 用于维护组件的各种状态;

视图(View): 是组件的可视化表现;

控制器(Controller):用于控制对于各种事件、组件做出响应 。

当模型发生改变时,它会通知所有依赖它的视图,视图会根据模型数据来更新自己。Swing使用UI代理来包装视图和控制器, 还有一个模型对象来维护该组件的状态。例如,按钮JButton有一个维护其状态信息的模型ButtonModel对象 。 Swing组件的模型是自动设置的,因此一般都使用JButton,而无须关心ButtonModel对象。

0x02 swing code implementation

First swing program

package com.test;

import javax.swing.*;

public class test {
    public static void main(String[] args) throws ClassNotFoundException,UnsupportedLookAndFeelException,InstantiationException,illegalaccessexception {
        JFrame jFrame = new JFrame("nwebshell");
        //设置外观风格
        UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
        //刷新jf容器及其内部组件的外观
        SwingUtilities.updateComponentTreeUI(jFrame);
        jFrame.setSize(1024,400);
        jFrame.setVisible(true);  
    }
}

Jcolorchooser and Jfilechooser

Swing provides two dialog boxes, jcolorchooser and Jfilechooser, which can easily select colors and local files.

JColorChooser

Jcolorchooser is used to create a color selector dialog box. The usage of this class is very simple. You can quickly generate a color selector dialog box by calling its static method

code:

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

public class JColorChooserDemo {

    JFrame jFrame = new JFrame("测试颜色选择器");

    JTextArea jta = new JTextArea("我爱中华",6,30);

    JButton button = new JButton(new AbstractAction("改变文本框的本景色"){

        @Override
        public void actionPerformed(ActionEvent e) {

            //弹出颜色选择器
            Color result = JColorChooser.showDialog(jFrame,"颜色选择器",Color.WHITE);
            jta.setBackground(result);
        }
    });

    public void init(){
        jFrame.add(jta);

        jFrame.add(button,BorderLayout.soUTH);

        jFrame.setDefaultCloSEOperation(JFrame.EXIT_ON_CLOSE);
        jFrame.pack();
        jFrame.setVisible(true);
    }

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

}

JFileChooser

The function of Jfilechooser is basically similar to that of FileDialog in AWT. It is also used to generate open file and save file dialog boxes. Unlike FileDialog, Jfilechooser does not need to rely on the GUI of the local platform. It is implemented by 100% pure Java, has exactly the same behavior on all platforms, and can have the same appearance style on all platforms.

Steps to use Jfilechooser:

JFileChooser chooser = new JFileChooser("D:\\a");//指定默认打开的本地磁盘路径
setSelectedFile(File file)/setSelectedFiles(File[] selectedFiles):设定默认选中的文件
setMultiSelectionEnabled(boolean b):设置是否允许多选,默认是单选
setFileSelectionMode(int mode):设置可以选择内容,例如文件、文件夹等,默认只能选择文件
showOpenDialog(Component parent):打开文件加载对话框,并指定父组件
showSaveDialog(Component parent):打开文件保存对话框,并指定父组件
File getSelectedFile():获取用户选择的一个文件
File[] getSelectedFiles():获取用户选择的多个文件

code:

import javax.imageio.ImageIO;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

public class JFileChooserDemo {

    //创建窗口对象
    JFrame jf = new JFrame("测试JFileChooser");

    //创建打开文件对话框
    JFileChooser chooser = new JFileChooser(".");

    //创建菜单条
    JMenuBar jmb = new JMenuBar();
    //创建菜单
    JMenu jMenu = new JMenu("文件");
    //创建菜单项
    JMenuItem open = new JMenuItem(new AbstractAction("打开"){

        @Override
        public void actionPerformed(ActionEvent e) {
            chooser.showOpenDialog(jf);
            File imageFile = chooser.getSelectedFile();
            try {
                image = ImageIO.read(imageFile);
                drawArea.repaint();
            } catch (IOException e1) {
                e1.printStackTrace();
            }
        }
    });

    JMenuItem save = new JMenuItem(new AbstractAction("另存为"){

        @Override
        public void actionPerformed(ActionEvent e) {
            chooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
            chooser.showSaveDialog(jf);
            File dir = chooser.getSelectedFile();
            try {
                ImageIO.write(image,"jpeg",new File(dir,"a.jpg"));
            } catch (Exception e1) {
                e1.printStackTrace();
            }
        }
    });

    //用来记录用户选择的图片
    BufferedImage image;

    //显示图片
    class MyCanvas extends JPanel{
        @Override
        public void paint(Graphics g) {
            if (image!=null){
                g.drawImage(image,null);
            }
        }
    }

    JPanel drawArea = new MyCanvas();

    public void init(){
        //设置图片显示区域大小
        drawArea.setPreferredSize(new Dimension(500,300));
        jf.add(drawArea);

        //组装并设置菜单条
        jMenu.add(open);
        jMenu.add(save);
        jmb.add(jMenu);
        jf.setJMenuBar(jmb);

        //显示jf
        jf.setDefaultCloSEOperation(JFrame.EXIT_ON_CLOSE);
        jf.pack();
        jf.setVisible(true);
    }

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

JOptionPane

It is very convenient to create some simple dialog boxes through joptionpane. Swing has added corresponding components to these dialog boxes without programmers adding components manually. Joptionpane provides the following four methods to create dialog boxes.

After the user interacts with the dialog box, the return values of different types of dialog boxes are as follows:

The dialog generated by showconfirmdialog has the following return values:

Four dialog box presentations

Message dialog box:

import cn.itcast.swing.util.ImagePathUtil;

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

public class MessageDialogTest {


    JFrame jf = new JFrame("测试消息对话框");

    JTextArea jta = new JTextArea(6,30);

    JButton btn = new JButton(new AbstractAction("弹出消息对话框") {

        @Override
        public void actionPerformed(ActionEvent e) {
            //JOptionPane.showMessageDialog(jf,jta.getText(),"消息对话框",JOptionPane.ERROR_MESSAGE);
            //JOptionPane.showMessageDialog(jf,JOptionPane.INFORMATION_MESSAGE);
            //JOptionPane.showMessageDialog(jf,JOptionPane.WARNING_MESSAGE);
            //JOptionPane.showMessageDialog(jf,JOptionPane.QUESTION_MESSAGE);
            //JOptionPane.showMessageDialog(jf,JOptionPane.PLAIN_MESSAGE);
            JOptionPane.showMessageDialog(jf,JOptionPane.WARNING_MESSAGE,new ImageIcon(ImagePathUtil.getRealPath("2\\female.png")));

        }
    });


    public void init(){
        jf.add(jta);
        jf.add(btn,BorderLayout.soUTH);

        jf.setDefaultCloSEOperation(JFrame.EXIT_ON_CLOSE);
        jf.pack();
        jf.setVisible(true);
    }

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

}

Confirmation dialog box:

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

public class ConfirmDialogTest {


    JFrame jf = new JFrame("测试确认对话框");

    JTextArea jta = new JTextArea(6,30);

    JButton btn = new JButton(new AbstractAction("弹出确认对话框") {

        @Override
        public void actionPerformed(ActionEvent e) {

            int result = JOptionPane.showConfirmDialog(jf,"确认对话框",JOptionPane.YES_NO_OPTION,JOptionPane.QUESTION_MESSAGE);
            if (result == JOptionPane.YES_OPTION){
                jta.append("\n用户点击了确定按钮");
            }

            if (result==JOptionPane.NO_OPTION){
                jta.append("\n用户点击了取消按钮");
            }

        }
    });


    public void init(){
        jf.add(jta);
        jf.add(btn,BorderLayout.soUTH);

        jf.setDefaultCloSEOperation(JFrame.EXIT_ON_CLOSE);
        jf.pack();
        jf.setVisible(true);
    }

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

}

Input dialog box:

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

public class InputDialogTest {


    JFrame jf = new JFrame("测试输入对话框");

    JTextArea jta = new JTextArea(6,30);

    JButton btn = new JButton(new AbstractAction("弹出输入对话框") {

        @Override
        public void actionPerformed(ActionEvent e) {


           /* String result = JOptionPane.showInputDialog(jf,"请填写您的银行账号:","输入对话框",JOptionPane.INFORMATION_MESSAGE);
            if(result!=null){
                jta.append(result.toString());
            }
            */

            Object result = JOptionPane.showInputDialog(jf,"",JOptionPane.DEFAULT_OPTION,null,new String[]{"柳岩","舒淇","龚玥菲"},"舒淇");
            if (result!=null){
                jta.append(result.toString());
            }
        }
    });


    public void init(){
        jf.add(jta);
        jf.add(btn,BorderLayout.soUTH);

        jf.setDefaultCloSEOperation(JFrame.EXIT_ON_CLOSE);
        jf.pack();
        jf.setVisible(true);
    }

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

}

Options dialog box:

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

public class OptionDialogTest {


    JFrame jf = new JFrame("测试选项对话框");

    JTextArea jta = new JTextArea(6,30);

    JButton btn = new JButton(new AbstractAction("弹出选项对话框") {

        @Override
        public void actionPerformed(ActionEvent e) {




            int result = JOptionPane.showOptionDialog(jf,"请选择尿不湿号码","选项对话框",JOptionPane.INFORMATION_MESSAGE,new String[]{"大号","中号","小号"},"中号");

            switch (result){
                case 0:
                    jta.setText("用户选择了大号");
                    break;
                case 1:
                    jta.setText("用户选择了中号");
                    break;
                case 2:
                    jta.setText("用户选择了小号");
                    break;
            }
        }
    });


    public void init(){
        jf.add(jta);
        jf.add(btn,BorderLayout.soUTH);

        jf.setDefaultCloSEOperation(JFrame.EXIT_ON_CLOSE);
        jf.pack();
        jf.setVisible(true);
    }

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

}

0x03 some ideas come true

In fact, when learning the GUI framework, I think the focus of the main development tools should not be on the design of various interface boxes, but on the event handling of some interfaces. As for multi-function, some dialog boxes are needed to realize function blocking. Therefore, when writing nweshell, NetBeans IDE is used to realize visual programming and automatically generate GUI box code. Then copy it to the idea to configure the event.

Unencrypted mode:

Unidirectional AES + hex encryption

The basic functions have been realized, but there are still many optimizations to be done. When two-way transmission encryption is included, the returned result is actually not typeset. There is also the storage of shell data, and the corresponding encrypted JSP Trojan horse is generated by one click.

Bidirectional AES + hex encryption:

It has realized one-way, two-way AES + hex, base64 encryption, dynamic key encryption, dynamic and static kill free and GUI interface.

In fact, the dynamic key mentioned above is not as big as what I said. In fact, (HHH) is just a problem of key acquisition and transmission. It is guaranteed that the same key is used. In addition, the next time it runs, it will generate a new key again. Once again, it will lead to jade without much repetition.

0x04 end

This tool has been written for about a week, learning and writing at the same time. During this period, some problems will be considered, such as why ice scorpion 3 is not used because it is so easy to use. In fact, it is difficult to detect ice scorpion 3. Why not transform based on ice scorpion 3? In fact, what I thought at that time was that the ice Scorpion was very easy to use. Although the strong features were difficult to detect, some weak features would be detected, and more and more people used it. Suppose we did another test based on version 3? If it is not updated, it may need to be transformed. If it is transformed, tools such as JD GUI must be used for reverse. There will be many errors in the reverse, and the code must be changed. Although some big guys have implemented it and transformed it into a loaded memory horse. However, the time cost is already relatively large, so we plan to carry out a reconstruction, so we can customize some encryption modes for convenience. But at present, it can only be regarded as a command execution tool for encrypted transmission. The later functions also want to conceive how to write. Teachers who have better ideas in this regard can put forward them for common exchange. For example, how to better hide or what kind of encryption is better? Or how to realize a function?

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>