2 u010259017 u010259017 于 2015.06.04 14:31 提问

有时间方便帮我看看java如何点击改变Label或JPanel背景图片么

我想通过点击一个单选按钮事件,然后更改左上角框框的背景图片,如和重绘,大神方便看看嘛,谢谢

图片说明

 import java.awt.Button;
import java.awt.Frame;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.Label;
import java.awt.Panel;
import java.awt.TextArea;

import javax.swing.ButtonGroup;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JRadioButton;

public class JFrameWindow {

    Frame frame_win ;
    Label scan_win;
    Panel scan_img;
    Button scan, res, calcul, next;
    JPanel fFace, uFace, lFace, rFace, bFace, dFace;
    JRadioButton fFaceBotton, uFaceBotton, lFaceBotton, rFaceBotton, dFaceBotton, bFaceBotton;
    ButtonGroup buttonGroup;
    Label in, ou;
    TextArea author;

    @SuppressWarnings("deprecation")
    void init(){
        final shibie sb = new shibie();

        frame_win = new JFrame("魔方自动扫描还原");
        scan_win = new Label("扫描窗口");
        scan_img = new Panel();

        bFace = new JPanel();
        uFace = new JPanel();
        fFace = new JPanel();
        dFace = new JPanel();
        rFace = new JPanel();
        lFace = new JPanel();

        scan = new Button("扫描");
        calcul = new Button("运算");
        next = new Button("下步");
        res = new Button("清除");

        buttonGroup = new ButtonGroup();
        fFaceBotton = new JRadioButton("正面");
        uFaceBotton = new JRadioButton("上面");
        lFaceBotton = new JRadioButton("左面");
        rFaceBotton = new JRadioButton("右面");
        dFaceBotton = new JRadioButton("底面");
        bFaceBotton = new JRadioButton("背面");
        buttonGroup.add(fFaceBotton);
        buttonGroup.add(uFaceBotton);
        buttonGroup.add(lFaceBotton);
        buttonGroup.add(rFaceBotton);
        buttonGroup.add(bFaceBotton);
        buttonGroup.add(dFaceBotton);

        in = new Label();
        ou = new Label();
        author = new TextArea("\n作者:Anke\n\n\n\nEmail:1261123807@qq.com\n版权所有", 6, 40, 3);

        frame_win.setBounds(300, 100, 750, 530);
        frame_win.setLayout(null);
        frame_win.setResizable(false);

        scan_win.setBounds(1, 1, 300, 300);
        scan_win.setBackground(new java.awt.Color(153, 204, 204));
        //scan_win.setVisible(false);

        scan_img.setBounds(340, 80, 320, 425);
        scan_img.setBackground(new java.awt.Color(153, 204, 204));
        scan_img.setLayout(null);
        //scan_img.setVisible(false);

        bFace.setBounds(110, 5, 100, 100);
        bFace.setBackground(new java.awt.Color(204, 204, 255));
        uFace.setBounds(110, 110, 100, 100);
        uFace.setBackground(new java.awt.Color(204, 204, 255));
        fFace.setBounds(110, 215, 101, 100);
        fFace.setBackground(new java.awt.Color(204, 204, 255));
        dFace.setBounds(110, 320, 100, 100);
        dFace.setBackground(new java.awt.Color(204, 204, 255));
        lFace.setBounds(5, 215, 100, 100);
        lFace.setBackground(new java.awt.Color(204, 204, 255));
        rFace.setBounds(215, 215, 100, 100);
        rFace.setBackground(new java.awt.Color(204, 204, 255));
        scan_img.add(fFace);
        scan_img.add(uFace);
        scan_img.add(dFace);
        scan_img.add(rFace);
        scan_img.add(lFace);
        scan_img.add(bFace);

        scan.setBounds(1, 310, 70, 20);
        calcul.setBounds(76,310,70,20);
        res.setBounds(226,310,70,20);
        next.setBounds(151,310,70,20);

        fFaceBotton.setBounds(680, 170, 80, 20);
        uFaceBotton.setBounds(680, 195, 80, 20);
        lFaceBotton.setBounds(680, 220, 80, 20);
        rFaceBotton.setBounds(680, 245, 80, 20);
        bFaceBotton.setBounds(680, 270, 80, 20);
        dFaceBotton.setBounds(680, 295, 80, 20);

        in.setText("输入:");
        in.setBounds(305, 5, 430, 30);
        in.setBackground(new java.awt.Color(153, 204, 204));
        ou.setText("输入:");
        ou.setBounds(305, 45, 430, 30);
        ou.setBackground(new java.awt.Color(153, 204, 204));
        //author.setText("作者:Anke\nEmail:1261123807@qq.com\n版权所有");
        author.setBounds(20, 380, 200, 100);
        author.disable();
        //author.setBackground(new java.awt.Color(153, 204, 204));

        frame_win.add(scan_win);
        frame_win.add(scan_img);
        frame_win.add(scan);
        frame_win.add(calcul);
        frame_win.add(next);
        frame_win.add(res);
        frame_win.add(fFaceBotton);
        frame_win.add(uFaceBotton);
        frame_win.add(lFaceBotton);
        frame_win.add(rFaceBotton);
        frame_win.add(bFaceBotton);
        frame_win.add(dFaceBotton);
        frame_win.add(in);
        frame_win.add(ou);
        frame_win.add(author);

        fFaceBotton.addActionListener(new java.awt.event.ActionListener() {        
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                fac = "F";
                sb.shibei(fac);         
                System.out.println("点击了"+fac);
                setBkground();          //更改背景
                scan_win.repaint();
            }
        });

        uFaceBotton.addActionListener(new java.awt.event.ActionListener() {        
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                fac = "u";
                sb.shibei(fac);
                System.out.println("点击了"+fac);
            }
        });

        lFaceBotton.addActionListener(new java.awt.event.ActionListener() {        
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                fac = "l";
                sb.shibei(fac);
                System.out.println("点击了"+fac);
            }
        });

        rFaceBotton.addActionListener(new java.awt.event.ActionListener() {        
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                fac = "r";
                sb.shibei(fac);
                System.out.println("点击了"+fac);
            }
        });

        bFaceBotton.addActionListener(new java.awt.event.ActionListener() {        
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                fac = "b";
                sb.shibei(fac);
                System.out.println("点击了"+fac);
            }
        });

        dFaceBotton.addActionListener(new java.awt.event.ActionListener() {        
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                fac = "d";
                sb.shibei(fac);
                System.out.println("点击了"+fac);
            }
        });

        frame_win.setVisible(true);
    }

        void setBkground(){
            scan_win.setBackground(new java.awt.Color(204, 204, 255));
            //if(true){
            Label scan_win = new Label("wocao") {
                    protected void paintComponent(Graphics g) {
                        ImageIcon icon = new ImageIcon("D:/test/java/MoFangView/face_img/"+fac+".jpg");
                        Image img = icon.getImage();
                        g.drawImage(img, 0, 0, icon.getIconWidth(), icon.getIconHeight(), icon.getImageObserver());
                        System.out.print("重绘"+fac);
                    }
                };
            //}
                frame_win.add(scan_win);
        }

    JFrameWindow(){
        init();
    }

    private String fac = null;

}

1个回答

tongyi55555
tongyi55555   2015.06.04 15:06
已采纳

两个问题,一个是不要使用Label对象,这个没有办法设置Icon;第二个更新图像的时候设置Icon即可,不需要再新建对象了。下面代码测试通过了,你试试。

import java.awt.Button;
import java.awt.Frame;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.Label;
import java.awt.Panel;
import java.awt.TextArea;

import javax.swing.ButtonGroup;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JRadioButton;

public class JFrameWindow extends JFrame {

    Frame frame_win;
    JLabel scan_win;
    Panel scan_img;
    Button scan, res, calcul, next;
    JPanel fFace, uFace, lFace, rFace, bFace, dFace;
    JRadioButton fFaceBotton, uFaceBotton, lFaceBotton, rFaceBotton,
            dFaceBotton, bFaceBotton;
    ButtonGroup buttonGroup;
    Label in, ou;
    TextArea author;

    @SuppressWarnings("deprecation")
    void init() {
        final shibie sb = new shibie();

        frame_win = new JFrame("魔方自动扫描还原");
        scan_win = new JLabel("扫描窗口");
        scan_img = new Panel();

        bFace = new JPanel();
        uFace = new JPanel();
        fFace = new JPanel();
        dFace = new JPanel();
        rFace = new JPanel();
        lFace = new JPanel();

        scan = new Button("扫描");
        calcul = new Button("运算");
        next = new Button("下步");
        res = new Button("清除");

        buttonGroup = new ButtonGroup();
        fFaceBotton = new JRadioButton("正面");
        uFaceBotton = new JRadioButton("上面");
        lFaceBotton = new JRadioButton("左面");
        rFaceBotton = new JRadioButton("右面");
        dFaceBotton = new JRadioButton("底面");
        bFaceBotton = new JRadioButton("背面");
        buttonGroup.add(fFaceBotton);
        buttonGroup.add(uFaceBotton);
        buttonGroup.add(lFaceBotton);
        buttonGroup.add(rFaceBotton);
        buttonGroup.add(bFaceBotton);
        buttonGroup.add(dFaceBotton);

        in = new Label();
        ou = new Label();
        author = new TextArea("\n作者:Anke\n\n\n\nEmail:1261123807@qq.com\n版权所有",
                6, 40, 3);

        frame_win.setBounds(300, 100, 750, 530);
        frame_win.setLayout(null);
        frame_win.setResizable(false);

        scan_win.setBounds(1, 1, 300, 300);
        scan_win.setOpaque(true);
        scan_win.setBackground(new java.awt.Color(153, 204, 204));
        // scan_win.setVisible(false);

        scan_img.setBounds(340, 80, 320, 425);
        scan_img.setBackground(new java.awt.Color(153, 204, 204));
        scan_img.setLayout(null);
        // scan_img.setVisible(false);

        bFace.setBounds(110, 5, 100, 100);
        bFace.setBackground(new java.awt.Color(204, 204, 255));
        uFace.setBounds(110, 110, 100, 100);
        uFace.setBackground(new java.awt.Color(204, 204, 255));
        fFace.setBounds(110, 215, 101, 100);
        fFace.setBackground(new java.awt.Color(204, 204, 255));
        dFace.setBounds(110, 320, 100, 100);
        dFace.setBackground(new java.awt.Color(204, 204, 255));
        lFace.setBounds(5, 215, 100, 100);
        lFace.setBackground(new java.awt.Color(204, 204, 255));
        rFace.setBounds(215, 215, 100, 100);
        rFace.setBackground(new java.awt.Color(204, 204, 255));
        scan_img.add(fFace);
        scan_img.add(uFace);
        scan_img.add(dFace);
        scan_img.add(rFace);
        scan_img.add(lFace);
        scan_img.add(bFace);

        scan.setBounds(1, 310, 70, 20);
        calcul.setBounds(76, 310, 70, 20);
        res.setBounds(226, 310, 70, 20);
        next.setBounds(151, 310, 70, 20);

        fFaceBotton.setBounds(680, 170, 80, 20);
        uFaceBotton.setBounds(680, 195, 80, 20);
        lFaceBotton.setBounds(680, 220, 80, 20);
        rFaceBotton.setBounds(680, 245, 80, 20);
        bFaceBotton.setBounds(680, 270, 80, 20);
        dFaceBotton.setBounds(680, 295, 80, 20);

        in.setText("输入:");
        in.setBounds(305, 5, 430, 30);
        in.setBackground(new java.awt.Color(153, 204, 204));
        ou.setText("输入:");
        ou.setBounds(305, 45, 430, 30);
        ou.setBackground(new java.awt.Color(153, 204, 204));
        // author.setText("作者:Anke\nEmail:1261123807@qq.com\n版权所有");
        author.setBounds(20, 380, 200, 100);
        author.disable();
        // author.setBackground(new java.awt.Color(153, 204, 204));

        frame_win.add(scan_win);
        frame_win.add(scan_img);
        frame_win.add(scan);
        frame_win.add(calcul);
        frame_win.add(next);
        frame_win.add(res);
        frame_win.add(fFaceBotton);
        frame_win.add(uFaceBotton);
        frame_win.add(lFaceBotton);
        frame_win.add(rFaceBotton);
        frame_win.add(bFaceBotton);
        frame_win.add(dFaceBotton);
        frame_win.add(in);
        frame_win.add(ou);
        frame_win.add(author);

        fFaceBotton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                fac = "F";
                sb.shibei(fac);
                System.out.println("点击了" + fac);
                setBkground(); // 更改背景
                scan_win.repaint();
            }
        });

        uFaceBotton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                fac = "u";
                sb.shibei(fac);
                System.out.println("点击了" + fac);
            }
        });

        lFaceBotton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                fac = "l";
                sb.shibei(fac);
                System.out.println("点击了" + fac);
            }
        });

        rFaceBotton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                fac = "r";
                sb.shibei(fac);
                System.out.println("点击了" + fac);
            }
        });

        bFaceBotton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                fac = "b";
                sb.shibei(fac);
                System.out.println("点击了" + fac);
            }
        });

        dFaceBotton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                fac = "d";
                sb.shibei(fac);
                System.out.println("点击了" + fac);
            }
        });

        frame_win.setVisible(true);
    }

    void setBkground() {
        scan_win.setBackground(new java.awt.Color(204, 204, 255));
        scan_win.setIcon(new ImageIcon(
                "D:/test/java/MoFangView/face_img/"+fac+".jpg"));
    }

    JFrameWindow() {
        init();
    }

    private String fac = null;

    public static void main(String[] args) {
        JFrameWindow frm = new JFrameWindow();
    }

}
u010259017
u010259017 回复那一抹代码: 恩呢,谢谢谢谢,嘿嘿
2 年多之前 回复
tongyi55555
tongyi55555 客气了。早来CSDN啊。有问题可以发私信给我。
2 年多之前 回复
u010259017
u010259017 万分感谢,我搞了几天,一直解决不了,真是谢谢!!!
2 年多之前 回复
Csdn user default icon
上传中...
上传图片
插入图片
准确详细的回答,更有利于被提问者采纳,从而获得C币。复制、灌水、广告等回答会被删除,是时候展现真正的技术了!