Java Swing编写样例

董联
2023-12-01
  1. 编写满足以下要求的GUI程序。
    ① 顶部两个文本框只接受大于0小于11的整数。
    ② 文本框数字改变时,自动刷新下部网格区域的按钮。
    ③ 鼠标进入按钮时,在该按钮上显示“*”。
    ④ 鼠标移出按钮时,隐藏该按钮上的文字。

源码:

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

public class GUIProgram {
    public static void main(String[] args) {
        new CustomJFrame();
    }
}

class CustomJFrame extends JFrame {

    private JPanel root;
    private JPanel northOfRoot;
    private JPanel buttonsPanel;
    private JTextField rowTextField;
    private JTextField colTextField;

    public CustomJFrame() {

        setTitle("Custom JFrame Test");
        setSize(300, 400);
        // setLayout(null);

        init();

        setVisible(true);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    }

    void init() {
        root = new JPanel();
        root.setLayout(new BorderLayout());

        add(root);

        northOfRoot = new JPanel();
        northOfRoot.setLayout(new FlowLayout());
        root.add(northOfRoot, BorderLayout.NORTH);

        JLabel rowText = new JLabel("行数: ");
        northOfRoot.add(rowText);

        rowTextField = new JTextField(5);
        rowTextField.addActionListener(new CustomActionListener(e -> generate()));
        rowTextField.addKeyListener(new CustomLimitKeyInputListener());
        northOfRoot.add(rowTextField);

        JLabel colText = new JLabel("列数: ");
        northOfRoot.add(colText);

        colTextField = new JTextField(5);
        colTextField.addActionListener(new CustomActionListener(e -> generate()));
        colTextField.addKeyListener(new CustomLimitKeyInputListener());
        northOfRoot.add(colTextField);

        // JButton generateButton = new JButton("生成");
        // generateButton.addActionListener(new CustomActionListener(e -> {
        //
        // }));
        // northOfRoot.add(generateButton);



    }

    void generate() {
        String _rowText = rowTextField.getText();
        String _colText = colTextField.getText();
        try {
        	//将String转化为int类型
            int row = Integer.parseInt(_rowText);
            int col = Integer.parseInt(_colText);
            //判断数字是否超出要求
            row = isValidParams(row) ? row : 10;
            col = isValidParams(col) ? col : 10;
            rowTextField.setText(String.valueOf(row));
            colTextField.setText(String.valueOf(col));
            drawButtonPanel(row, col);
        } catch (NumberFormatException n) {
        	//这里其实无法到达,因为前面有CustomLimitKeyInputListener监听器限制了输入只能是数字,锁有不会出现这个异常,没删掉因为懒得删
            JOptionPane.showMessageDialog(root, "输入应为0-11", "warn", JOptionPane.WARNING_MESSAGE);
        }
    }

    boolean isValidParams (int param) {
        if (param <= 0 || param >= 11) {
            return false;
        }
        return true;
    }

    void drawButtonPanel(int row, int col) {

        if (buttonsPanel == null) {
            buttonsPanel = new JPanel();
            root.add(buttonsPanel, BorderLayout.CENTER);
        } else {
            buttonsPanel.removeAll();
        }
        buttonsPanel.setLayout(new GridLayout(row, col));
		
		//渲染按钮
        for (int i = 0; i < row; i++) {
            for (int j = 0; j < col; j++) {
                JButton jButton = new JButton();
                buttonsPanel.add(jButton);
                jButton.addMouseListener(new MouseAdapter() {
                    @Override
                    public void mouseEntered(MouseEvent e) {
                        jButton.setText("*");
                    }

                    @Override
                    public void mouseExited(MouseEvent e) {
                        jButton.setText("");
                    }
                });
            }
        }
        SwingUtilities.updateComponentTreeUI(buttonsPanel);

    }

}

@FunctionalInterface
interface DealHandler {
    void deal(Object something);
}

class CustomActionListener implements ActionListener {

    private DealHandler dealHandler;

    public CustomActionListener(DealHandler dealHandler) {
        this.dealHandler = dealHandler;
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        dealHandler.deal(e);
    }
}

class CustomLimitKeyInputListener extends KeyAdapter {
    @Override
    public void keyTyped(KeyEvent e) {
        if (!Character.isDigit(e.getKeyChar())) {
            //不是数字限制传递
            e.consume();
        }
    }
}


 类似资料: