情況:所以我調用我的paint方法繪製方法。但是,我只希望它在繪製轉換按鈕時單擊。如何告訴java不draw3Drectangle,除非(ae.getSource == convert)?我對GUI很陌生,你可能會說,請簡單回答。感謝任何幫助。如何在java中「禁用某個方法」並使其在actionPerformed方法中處於活動狀態
CODE:
public class simpgui extends Applet implements ActionListener
{
Button convert;
Label celsius;
Label farenheit;
TextField cels;
TextField fare;
String message = "";
public void init()
{
convert = new Button("Convert");
celsius = new Label("Celsius");
farenheit = new Label("Farenheit");
cels = new TextField(15);
fare = new TextField(15);
add(convert);
add(celsius);
add(cels);
add(farenheit);
add(fare);
convert.addActionListener(this);
}
public void paint(Graphics g)
{
fare.setLocation(160,50);
farenheit.setLocation(90,50);
convert.setLocation(310,5);
draw(g);
}
public void actionPerformed (ActionEvent ae)
{
if(ae.getSource() == convert)
{
int farenheit = (int) ((Double.parseDouble(cels.getText())) * (1.8)) + 32;
fare.setText(farenheit+"");
}
}
public static void draw(Graphics g)
{
g.setColor(Color.BLUE);
g.fill3DRect(0,0,400,100,true);
}
}
不要忘記調用'super.paint'在你做任何實際的繪畫之前... – MadProgrammer 2014-12-02 04:30:34
有趣的是什麼是super.paint的使用,我應該把它放在哪裏?謝謝。 @MadProgrammer – imgolden62 2014-12-02 13:10:28
Paint負責爲繪畫準備Graphics狀態,繪製組件的邊框,背景和子組件,並且應該先調用它,然後再執行任何自定義繪畫 – MadProgrammer 2014-12-02 18:53:04