在按钮中添加照片

发布于 2025-01-30 07:26:28 字数 1258 浏览 4 评论 0原文

class Actions implements ActionListener {
    public void actionPerformed(ActionEvent e) {
        // Get the button that was clicked
        JButton theButton = (JButton) e.getSource();

        // Set it's background color to white
        Color color = theButton.getBackground();

        if(upload.isSelected()){Icon i=new ImageIcon(upload.getText());
        }
        if(start.isSelected()){
            for(int i=0;i<buttonNum;i++){
                Color check=buttons[i].getBackground();
                if(check==Color.green){
                    buttons[i].setBackground(Color.white);
                }
            }
            theButton.setBackground(Color.green);}
        else if(end.isSelected()){
            for(int i=0;i<buttonNum;i++){
                Color check=buttons[i].getBackground();
                if(check==Color.red){
                    buttons[i].setBackground(Color.white);
                }
            }
            theButton.setBackground(Color.red);}
        else if (color==Color.black)
        {
            theButton.setBackground(Color.white);}
        else{theButton.setBackground(Color.black);
        }
    }
}

此功能将使用for循环创建一些按钮。我想添加一个函数,在选择上传按钮后,当我单击“循环创建的按钮”之一时,它将在按钮上放置照片。

class Actions implements ActionListener {
    public void actionPerformed(ActionEvent e) {
        // Get the button that was clicked
        JButton theButton = (JButton) e.getSource();

        // Set it's background color to white
        Color color = theButton.getBackground();

        if(upload.isSelected()){Icon i=new ImageIcon(upload.getText());
        }
        if(start.isSelected()){
            for(int i=0;i<buttonNum;i++){
                Color check=buttons[i].getBackground();
                if(check==Color.green){
                    buttons[i].setBackground(Color.white);
                }
            }
            theButton.setBackground(Color.green);}
        else if(end.isSelected()){
            for(int i=0;i<buttonNum;i++){
                Color check=buttons[i].getBackground();
                if(check==Color.red){
                    buttons[i].setBackground(Color.white);
                }
            }
            theButton.setBackground(Color.red);}
        else if (color==Color.black)
        {
            theButton.setBackground(Color.white);}
        else{theButton.setBackground(Color.black);
        }
    }
}

This function will create some buttons using a for loop. I want to add a function that after the upload button selected, when I clicked one of the buttons created by for loop, it will put a photo on the button.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文