2013-03-27 61 views
1

AnaPencere.java的Java面板的BufferedImage不工作

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

public class AnaPencere { 
    JFrame anaPencere; 
    BufferedImage bImageLEFT; 
    BufferedImage bImageRIGHT; 
    public static void main(String[] args){ 
     AnaPencere apencere = new AnaPencere(); 
    } 

    public AnaPencere() { 
      anaPencere = new JFrame("Main Window"); 
      anaPencere.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
      anaPencere.setSize(613, 253); 
      anaPencere.setLocationRelativeTo(null); 
      anaPencere.setVisible(true); 
      anaPencere.add(new left()); 
      anaPencere.add(new right()); 

    } 

    private class left extends JPanel{ 
     private left() { 
      this.setBounds(0, 0, 313, 253); 
      this.setFocusable(true); 
      bImageLEFT = new BufferedImage(313, 253, BufferedImage.TYPE_INT_ARGB); 
      paintComponent(bImageLEFT.createGraphics()); 
     } 

     public void paintComponent(Graphics g){ 
      Graphics2D g2 = (Graphics2D)g; 
      g2.drawString("> LEFT <", Font.BOLD, 13); 
     } 
    } 

    private class right extends JPanel{ 
     private right() { 
      this.setBounds(313, 0, 300, 253); 
      this.setFocusable(true); 
      bImageRIGHT = new BufferedImage(313, 253, BufferedImage.TYPE_INT_ARGB); 
      paintComponent(bImageRIGHT.createGraphics()); 
     } 

     public void paintComponent(Graphics g){ 
      Graphics2D g2 = (Graphics2D)g; 
      g2.drawString("> RIGHT <", Font.BOLD, 13); 
     } 
    } 
} 

(我設置jpanels cordinates左,右類) 當我運行我的代碼,我只是表明>左<文字,我可以」 t saw > RIGHT < text。 我該如何解決這個問題?

索裏我的英文不好..

回答

2

這裏我已經在你的代碼的變化。並對有關地點發表意見。看看它:

enter image description here

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

public class AnaPencere { 
    JFrame anaPencere; 
    BufferedImage bImageLEFT; 
    BufferedImage bImageRIGHT; 
    public static void main(String[] args){ 
     AnaPencere apencere = new AnaPencere(); 
    } 

    public AnaPencere() { 
      anaPencere = new JFrame("Main Window"); 
      anaPencere.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
      anaPencere.setSize(613, 253); 
      anaPencere.setLocationRelativeTo(null); 
      JPanel pLeft = new left(); 
      JPanel pRight = new right(); 
      anaPencere.getContentPane().add(pLeft,BorderLayout.WEST); 
      anaPencere.getContentPane().add(pRight,BorderLayout.EAST); 
      anaPencere.setVisible(true);//Set visible true after all components are added 
    } 

    private class left extends JPanel{ 
     private left() { 
      //this.setBounds(0, 0, 313, 253);//Don't set Bounds. Use Layouts provided by Swing API 
      setPreferredSize(new Dimension(150,200));//Set the preferredSize 
      this.setFocusable(true); 
      bImageLEFT = new BufferedImage(313, 253, BufferedImage.TYPE_INT_ARGB); 
      //paintComponent(bImageLEFT.createGraphics());// No need to call it explicitly. Swing will do it for itself. 
     } 

    public void paintComponent(Graphics g){ 
     super.paintComponent(g);//call the paintComponent method of JPanel class 
     Graphics2D g2 = (Graphics2D)g; 
     g2.setFont(new Font(g.getFont().getName(),Font.BOLD,13));//set Font in this way 
     g2.drawString("> LEFT <", 10, 20);//10 is x coordinate , 20 is y cordinate 
    } 
    } 

    private class right extends JPanel{ 
     private right() { 
      //this.setBounds(313, 0, 300, 253);/Don't set Bounds. Use Layouts provided by Swing API 
      setPreferredSize(new Dimension(150,200));//Set the preferredSize 
      this.setFocusable(true); 
      bImageRIGHT = new BufferedImage(313, 253, BufferedImage.TYPE_INT_ARGB); 
      // paintComponent(bImageRIGHT.createGraphics());//No need to call it explicitly. It is called by swing components itself. 
     } 

     public void paintComponent(Graphics g){ 
      super.paintComponent(g);//call the paintComponent method of JPanel class 
      Graphics2D g2 = (Graphics2D)g; 
      g2.setFont(new Font(g.getFont().getName(),Font.BOLD,13));//set Font in this way 
      g2.drawString("> RIGHT <", 10, 20);//10 is x coordinate , 20 is y cordinate 
     } 
    } 
} 
+0

感謝您的答覆,但沒有再工作,我只看到了>左<文本 – kibar 2013-03-27 14:07:48

+0

@ user1429570你怎麼想顯示你的GUI? – 2013-03-27 14:26:50

+0

我使用默認的gui,我只想要一個框架,但創建兩個jpanel類左和右與bufferedimage我使用兩個paintComponent並繪製左和右字符串 – kibar 2013-03-27 14:34:40