2012-07-04 332 views
1

我想更改Jpanel的背景顏色及其點擊按鈕上的字體。按鈕點擊更改jpanel的背景和字體顏色

誰能告訴我我做錯了什麼? 我可以將JPanel背景設置爲透明嗎?如果是的話如何?

On button click 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; 
    } 
} 

回答

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. 下一次,發佈一個SSCCE與Applet(例如JFrame)不同的東西,除非您遇到的問題確實需要重新生成Applet。
+0

下次我會照顧所有這一點。我會嘗試你的建議並回復你 – khushi

相關問題