2012-04-13 17 views
0

我想從我的文本字段獲取數據並將其設置爲int h。並改變矩形即時圖的大小,但我不知道如何去從文本字段獲取數據,我厭倦了在actionperfomred中使用e.getsource,但它無法找到我的文本字段。我的代碼如下:我不知道如何從我的文本字段獲取數據

import java.awt.*; 
import java.awt.event.*; 
import java.awt.image.*; 
import java.io.*; 
import javax.imageio.*; 
import javax.swing.*; 
import java.net.*; 
import java.sql.*; 
import java.lang.Object; 
import java.awt.Graphics; 
import java.awt.Graphics2D; 

/** 
* This class demonstrates how to load an Image from an external file 
*/ 
public class test extends Component { 

    int x=77, y=441, w=23, h=10; 

    BufferedImage img = 
    new BufferedImage(100, 50, 
        BufferedImage.TYPE_INT_ARGB);  
    // BufferedImage img; 

    public void paint(Graphics g) { 
     g.drawImage(img, 0, 0, null); 
      // g.fillRect(10,10,10,10); 
    } 

    public test() { 
     try { 
      img = ImageIO.read(new File("sales-goal.png")); 
     } catch (IOException e) {} 


     Graphics2D g = img.createGraphics(); 
     Color myColor = Color.decode("#32004b"); 
     g.setColor(myColor); 
     g.fillRect(x,y,w,h); 
       //77,441,23,10 
    } 

    public Dimension getPreferredSize() { 
     if (img == null) { 
      return new Dimension(100,100); 
     } else { 
      //return new Dimension(img.getWidth(null), img.getHeight(null)); 
      return new Dimension(300,600); 
     } 
    } 

    public static void main(String[] args) { 

     JFrame f = new JFrame("Load Image Sample"); 
     JTextField textField=new JTextField(); 
     f.add(textField); 
     textField.setBounds(10,10,40,30); 
     textField.setVisible(true); 

     f.addWindowListener(new WindowAdapter(){ 
       public void windowClosing(WindowEvent e) { 
        System.exit(0); 
       } 
      }); 

     f.add(new test()); 
     f.pack(); 
     f.setVisible(true); 
    } 

    public void actionPerformed(ActionEvent e) { 
       // if (e.getSource() == textField) {} 

    } 
} 
+0

這是一個很好的問題:http://codereview.stackexchange.com/ – 2012-04-13 17:33:59

+0

不要AWT('Component')成分混合擺動('JFrame')。 'test'類應該擴展'JPanel'(並且應該正確地將其大寫爲'Test')。它需要重寫'paintComponent()'而不是'paint()'。 – 2012-04-13 17:49:52

+0

這樣做,我仍然不知道如何通過操作執行主要獲取數據。 – user1329836 2012-04-13 18:33:33

回答

1

變量textField是本地main。如果您想從actionPerformed訪問它,則需要將其更改爲實例變量。

+0

我該如何讓它成爲一個實例變量,然後我如何將它添加到j框架,因爲這也是主要框架。我應該把主要的東西都搬走嗎? – user1329836 2012-04-13 18:31:34

1

是的。我同意@jpm。您需要將其聲明爲實例變量。 執行以下操作: -

public class test extends Component { 
     //Declare the variable here. 
     private static JTextField textfield; 

    public static void main(String[] args) { 
     //Whenever you use the textfield use like this. Remove the keyword 'JTextField'. 
     textfield = new JTextField(); 
    } 
    } 
+0

我這樣做了,並將其添加到我的actionperformed中,但是我沒有獲取數據public void actionPerformed(ActionEvent e){g 0}圖G = getGraphics(); (e.getSource()== textField){ entry = Integer.parseInt(textField.getText()); g.drawString(「Test」,50,50); entry = h; } – user1329836 2012-04-13 18:59:11

+0

我不明白你在說什麼。簡要解釋。 – 2012-04-13 19:10:56

+0

檢查您是否使用了相同的變量名稱:private JTextField textField;和textField = new JTextField(); – 2012-04-13 19:19:55

相關問題