按钮javafx不同的风格

时间:2016-10-19 12:40:52

标签: java javafx

当我点击它时,我试图让按钮改变它的样式,我可以安排自己从style1更改为style2,现在我无法弄清楚将它改为第三种样式(style3)。我使用了if else语句,现在我正在考虑将Switch-case用于第三个语句。

style = true;
    btn.setOnAction((ActionEvent event) ->
    {
    System.out.println("Hello!");
        if (style == true) {
    btn.getStyleClass().remove("button1");
    btn.getStyleClass().add("button2");
        }
        else {
    btn.getStyleClass().remove("button2");
    btn.getStyleClass().add("button1");
        }

        style=!style;
    });

1 个答案:

答案 0 :(得分:1)

可能有很多方法可以做到这一点。这是一个:

public class CustomActivity extends AppCompatActivity {

private static final String TAG = "CustomActivity";


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);


    TextView mymessage = (TextView) findViewById(R.id.mymessage);

    // If a notification message is tapped, any data accompanying the notification
    // message is available in the intent extras. In this sample the launcher
    // intent is fired when the notification is tapped, so any accompanying data would
    // be handled here. If you want a different intent fired, set the click_action
    // field of the notification message to the desired intent. The launcher intent
    // is used when no click_action is specified.
    //
    // Handle possible data accompanying notification message.
    // [START handle_data_extras]
    if (getIntent().getExtras() != null) {
        for (String key : getIntent().getExtras().keySet()) {
            String value = String.valueOf(getIntent().getExtras().get(key));
            Log.d(TAG, "Key: " + key + " Value: " + value);
            //
            Toast.makeText(getApplicationContext() , value , Toast.LENGTH_SHORT).show();


        }

        //Toast.makeText(getApplicationContext() , String.valueOf(getIntent().getExtras().get("message")) , Toast.LENGTH_SHORT).show();


    }
    // [END handle_data_extras]

    Button subscribeButton = (Button) findViewById(R.id.subscribeButton);
    subscribeButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            // [START subscribe_topics]
            FirebaseMessaging.getInstance().subscribeToTopic("news");
            // [END subscribe_topics]

            // Log and toast
            String msg = getString(R.string.msg_subscribed);
            Log.d(TAG, msg);
            Toast.makeText(CustomActivity.this, msg, Toast.LENGTH_SHORT).show();
        }
    });

    Button logTokenButton = (Button) findViewById(R.id.logTokenButton);
    logTokenButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            // Get token
            String token = FirebaseInstanceId.getInstance().getToken();

            // Log and toast
            String msg = getString(R.string.msg_token_fmt, token);
            Log.d(TAG, msg);
            Toast.makeText(CustomActivity.this, msg, Toast.LENGTH_SHORT).show();
        }
    });
}