我们专注攀枝花网站设计 攀枝花网站制作 攀枝花网站建设
成都网站建设公司服务热线:400-028-6601

网站建设知识

十年网站开发经验 + 多家企业客户 + 靠谱的建站团队

量身定制 + 运营维护+专业推广+无忧售后,网站问题一站解决

JavaSwing编写样例-创新互联

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

源码:

创新互联是专业的尼金平网站建设公司,尼金平接单;提供成都网站设计、成都网站建设,网页设计,网站设计,建网站,PHP网站建设等专业做网站服务;采用PHP框架,可快速的进行尼金平网站开发网页制作和功能扩展;专业做搜索引擎喜爱的网站,专业的做网站团队,希望更多企业前来合作!
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();
        }
    }
}

你是否还在寻找稳定的海外服务器提供商?创新互联www.cdcxhl.cn海外机房具备T级流量清洗系统配攻击溯源,准确流量调度确保服务器高可用性,企业级服务器适合批量采购,新人活动首月15元起,快前往官网查看详情吧


网站标题:JavaSwing编写样例-创新互联
标题来源:http://mswzjz.cn/article/hpeho.html

其他资讯