按钮点击更改jpanel的背景和字体颜色

时间:2012-07-04 06:36:00

标签: java swing button applet awt

我想在点击按钮时更改Jpanel的背景颜色及其字体。

谁能告诉我我做错了什么? 我可以将JPanel背景设置为透明吗?如果是,如何?

在按钮上单击test4.action方法,我需要更改Jpanel背景颜色?

以下是代码:

import java.applet.*;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.awt.event.*;
import javax.swing.*;

public class test3 extends Applet {

    JPanel c;
    JScrollPane s;
    Button connect;
    Panel controls;
    Color back, fore;

    public void init() {
        back = Color.black;
        fore = Color.white;
        setBackground(Color.darkGray);
        setLayout(new BorderLayout());
        s = new JScrollPane(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
        //s.setSize(100, 100);
        add("Center", s);
        c = new myCanvas11(this);
        s.setOpaque(false);
        s.setViewportView(c);
        //s.add(c);
        c.setSize(1000, 16000);
        add("North", controls = new Panel());
        controls.setLayout(new FlowLayout());
        controls.add(connect = new Button("Change Color"));
    }

    public void start() {
        // s.setScrollPosition(100, 100);
    }

    public boolean action(Event e, Object arg) {
        back = Color.magenta;
        fore = Color.blue;
        //setBackground(back);
        //invalidate();
        //repaint();
        c.setBackground(back);
        c.repaint();
        //s.getViewport().setBackground(back);
        // s.getViewport().repaint();
        //c.repaint();
        c.setFocusable(true);
        return true;
    }
}

class myCanvas11 extends JPanel implements KeyListener {

    Image buffImage;
    Graphics offscreen;
    boolean initDone = false;
    int chw, chh; // size of a char (in pixels)
    int chd; // offset of char from baseline
    int width, height; // size of applet (in pixels)
    int w, h; // size of applet (in chars)
    Font fn;
    Graphics gr;
    int nh, nw;
    test3 owner;
    static int counter = 0;

    myCanvas11(test3 t) {
        super();
        owner = t;
        nh = 16000;
        nw = 1000;
        this.setOpaque(true);
        this.setFocusable(true);
        addKeyListener(this);
    }

    public void reshape(int nx, int ny, int nw1, int nh1) {
        if (nw1 != width || nh1 != height) {
            width = nw;
            height = nh;
            gr = getGraphics();
            fn = new Font("Courier", Font.PLAIN, 11);
            if (fn != null) {
                gr.setFont(fn);
            }
            FontMetrics fnm = gr.getFontMetrics();
            chw = fnm.getMaxAdvance();
            chh = fnm.getHeight();
            chd = fnm.getDescent();
            // kludge for Windows NT and others which have too big widths
            if (chw + 1 >= chh) {
                chw = (chw + 1) / 2;
            }
            // work out size of drawing area
            h = nh / chh;
            w = nw / chw;
            buffImage = this.createImage(nw, nh);
            offscreen = buffImage.getGraphics();
            //offscreen.setColor(Color.black);
            //offscreen.fillRect(0, 0, nw, nh);
            offscreen.setColor(Color.blue);
            offscreen.setFont(fn);
            if (initDone) {
                offscreen.drawString("Hello World!", 0, 50);
            } else {
                offscreen.drawString("khushbu", 2, 50);
            }
            initDone = true;
            offscreen.drawImage(buffImage, 0, 0, this);
        }
        super.reshape(nx, ny, nw, nh);
    }

    public void paint(Graphics g) {
//       if (!initDone)
//       initpaint(g);
//       else
        g.drawImage(buffImage, 0, 0, this);
        //g.drawImage(buffImage, 0, 0, owner.back, this);

    }

    public void update(Graphics g) {
        g.drawImage(buffImage, 0, 0, this);
        super.update(g);
        //g.drawImage(buffImage, 0, 0, owner.back, this);
    }

    public void initpaint(Graphics g) {
        try {
            nh = getHeight();
            nw = getWidth();
            gr = getGraphics();
            fn = new Font("Courier", Font.PLAIN, 11);
            if (fn != null) {
                gr.setFont(fn);
            }
            FontMetrics fnm = gr.getFontMetrics();
            chw = fnm.getMaxAdvance();
            chh = fnm.getHeight();
            chd = fnm.getDescent();
            // kludge for Windows NT and others which have too big widths
            if (chw + 1 >= chh) {
                chw = (chw + 1) / 2;
            }
            // work out size of drawing area
            h = nh / chh;
            w = nw / chw;
            buffImage = this.createImage(nw, nh);
            offscreen = buffImage.getGraphics();
            //offscreen.setColor(Color.black);
            //offscreen.fillRect(0, 0, nw, nh);
            offscreen.setColor(Color.white);
            offscreen.setFont(fn);
            if (initDone) {
                offscreen.drawString("Hello World!", 0, 50);
            } else {
                offscreen.drawString("khushbu", 2, 50);
            }
            initDone = true;
            g.drawImage(buffImage, 0, 0, this);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /** Handle the key typed event from the text field. */
    public void keyTyped(KeyEvent e) {
    }

    /** Handle the key pressed event from the text field. */
    public void keyPressed(KeyEvent e) {
        String s;
        offscreen.setColor(owner.fore);
        offscreen.setFont(fn);
        for (int i = counter; i < counter + 25; ++i) {
            s = Integer.toString(i);
            offscreen.drawString(s, 3, i * chh);
            offscreen.drawLine(10, i * chh, 160, i * chh);
        }
        //owner.s.setScrollPosition(0, counter * 16);
        counter = counter + 25;
        repaint();
    }

    /** Handle the key released event from the text field. */
    public void keyReleased(KeyEvent e) {
    }

    public boolean keyDown(Event e, int k) {
        String s;
        offscreen.setColor(owner.fore);
        offscreen.setFont(fn);
        for (int i = counter; i < counter + 25; ++i) {
            s = Integer.toString(i);
            offscreen.drawString(s, 3, i * chh);
            offscreen.drawLine(10, i * chh, 160, i * chh);
        }
        //owner.s.setScrollPosition(0, counter * 16);
        counter = counter + 25;
        repaint();
        return true;
    }
} 

1 个答案:

答案 0 :(得分:4)

  

我可以将JPanel背景设置为透明吗?如果是,如何?

是的,只需在该JPanel上调用setOpaque(false);即可。相反,如果您想要绘制背景,请在该面板上调用setOpaque(true);

补充说明:

  1. public boolean action(Event e, Object arg)已弃用。改为添加适当的侦听器。
  2. 遵循Java编码约定,并使用大写字母作为类的第一个字符
  3. 您几乎不应该在组件上调用getGraphics();
  4. 请勿覆盖public void paint(Graphics g),但protected void paintComponent(Graphics g)。如果您想要绘制背景,请不要忘记致电super.paintComponent
  5. 为什么要覆盖重塑?
  6. 为什么要创建内部缓冲区? Swing已经内置了双缓冲功能,并且可以做得更好。
  7. 如果您已经覆盖paint(),为什么还要覆盖update()?你两次做同样的工作。
  8. 删除gr类变量,它没有任何意义。当您使用Graphics对象时,请使用作为paintComponent方法的参数提供的对象,但不要保留对它的引用,因为它将在稍后处理。
  9. counter不一定是static。尽可能避免static
  10. 尽量避免课程之间如此多的耦合。它使您的代码难以维护且难以预测。
  11. 下次使用除Applet(例如JFrame)以外的其他内容发布SSCCE,除非您遇到的问题确实需要重播Applet。