情况:所以我从我的paint方法中调用draw方法。但是,我只希望它在单击转换按钮时进行绘制。我如何告诉java不要draw3Drectangle除非(ae.getSource==convert)?正如你可能知道的那样,我是 GUI 新手,所以请简单回答。感谢任何帮助。
代码:
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);
}
}
最佳答案
方法不能被“禁用”,但您可以使用 boolean 变量和 if 语句来实现相同的功能:
boolean isClicked = false;
public void paint(Graphics g)
{
super.paint();
fare.setLocation(160,50);
farenheit.setLocation(90,50);
convert.setLocation(310,5);
draw(g);
}
public void actionPerformed(ActionEvent ae)
{
if (ae.getSource() == convert)
{
isClicked = true;
}
}
public void draw(Graphics g)
{
if (isClicked)
{
g.setColor(Color.BLUE);
g.fill3DRect(0,0,400,100,true);
}
}
关于java - 如何在 java 中 "disable a method"并使其在 actionPerformed 方法中处于 Activity 状态(如果有任何意义),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27241517/