1
我們在課堂上做了這個JavaFX練習題,並且當我點擊「打我」按鈕時,它給我一個色輪。我查看了代碼,甚至讓我的教授這樣做,我看不到任何問題。這可能是Mac問題嗎?我的朋友的代碼在他的Windows機器上運行得很好。簡單的留言箱功課給了我色輪
package csc502_classexample_events_1;
import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.layout.HBox;
import javafx.stage.Stage;
import javax.swing.JOptionPane;
/**
*
* @author aufty
*/
public class CSC502_ClassExample_Events_1 extends Application
{
@Override
public void start(Stage stage)
{
// Single centered button in HBox
Button button = new Button("Hit me");
button.setOnAction(new ClickHandler());
HBox hBox = new HBox();
hBox.getChildren().add(button);
hBox.setAlignment(Pos.CENTER);
stage.setTitle("My Event Handler Example");
Scene scene = new Scene(hBox, 400, 80);
stage.setScene(scene);
stage.show();
}
public static void main(String [] args)
{
launch(args);
}
}
class ClickHandler implements EventHandler<ActionEvent>
{
@Override
public void handle(ActionEvent event)
{
JOptionPane.showMessageDialog(null, "Ouch");
}
}
的工作,但我有麻煩了解爲什麼我的原始代碼在Windows機器上爲我的教授和其他學生工作。我認爲這不會對Java產生影響。 – aufty 2014-09-23 14:25:29
它是獨立於平臺的,因爲您最初發布的代碼不正確,無論您是在Windows還是Mac上運行它。 (僅僅是因爲一段代碼碰巧在某些特定情況下工作並不能使其正確。)所有的UI工具包都需要幕後的本地實現;它可能恰好發生在特定版本的JVM的UI工具包的本地實現以及它們正在使用的特定版本的Windows處理這種情況的副作用。 – 2014-09-23 14:31:28
明白了。這意味着我的教授正在教我一些不好的做法。讓人驚訝。 – aufty 2014-09-23 14:32:40