我希望在javaFX中对齐复选框。

huangapple go评论77阅读模式
英文:

I wish to align checkboxes in javaFX

问题

我需要创建一个带有对齐复选框的GUI,就像这张图片中的一样:
我希望在javaFX中对齐复选框。

我尝试的代码导致以下图片:
我希望在javaFX中对齐复选框。

我使用了3个HBoxes,每个HBox包含两个配料复选框。然后我将这些HBoxes添加到了ToppingsOptionPane中。

我需要做什么来对齐复选框?

我尝试使用2个包含三个配料的VBoxes,第一个VBox位于屏幕中间左侧,第二个VBox位于屏幕中间右侧。但第二个VBox最终显示在第一个VBox下方,而不是在其旁边。所以那个方法不起作用。

import javafx.event.ActionEvent;
import javafx.geometry.Pos;
import javafx.scene.control.CheckBox;
import javafx.scene.layout.HBox;
import javafx.scene.layout.VBox;
import javafx.scene.text.Text;
import javafx.scene.text.Font;
import java.text.NumberFormat;

//************************************************************************
// ToppingsOptionPane.java
// 展示了复选框的使用。
//************************************************************************

public class ToppingsOptionPane extends VBox{
    private Text phrase;
    private double totalPrice;
    private CheckBox pepperoni, olives, bacon, ham, mushroom, meatballs;
    NumberFormat fmt1 = NumberFormat.getCurrencyInstance();

    //--------------------------------------------------------------------
    // 构造函数。设置了包含文本对象和确定比萨价格的复选框的面板。
    //--------------------------------------------------------------------
    public ToppingsOptionPane(){

        totalPrice = 10;
        phrase = new Text("比萨成本: " + fmt1.format(totalPrice));
        phrase.setFont(new Font("Helvetica", 20));

        pepperoni = new CheckBox("意大利辣香肠");
        pepperoni.setOnAction(this::processCheckBoxAction);

        olives = new CheckBox("橄榄");
        olives.setOnAction(this::processCheckBoxAction);

        HBox toppings1 = new HBox(pepperoni,olives);
        toppings1.setAlignment(Pos.CENTER);
        toppings1.setSpacing(20);

        bacon = new CheckBox("培根");
        bacon.setOnAction(this::processCheckBoxAction);

        ham = new CheckBox("火腿");
        ham.setOnAction(this::processCheckBoxAction);

        HBox toppings2 = new HBox(bacon,ham);
        toppings2.setAlignment(Pos.CENTER);
        toppings2.setSpacing(20);

        mushroom = new CheckBox("蘑菇");
        mushroom.setOnAction(this::processCheckBoxAction);

        meatballs = new CheckBox("肉丸");
        meatballs.setOnAction(this::processCheckBoxAction);

        HBox toppings3 = new HBox(mushroom,meatballs);
        toppings3.setAlignment(Pos.CENTER);
        toppings3.setSpacing(20);

        setSpacing(20); // 文本和复选框之间的间距
        getChildren().addAll(toppings1,toppings2,toppings3,phrase);
    }

    //--------------------------------------------------------------------
    // 事件处理程序。更新比萨价格。
    //--------------------------------------------------------------------
    public void processCheckBoxAction(ActionEvent event){

        int sum = 0;
        double toppings = 0;

        if (pepperoni.isSelected())
            sum+=1;
        if (olives.isSelected())
            sum+=1;
        if (bacon.isSelected())
            sum+=1;
        if (ham.isSelected())
            sum+=1;
        if (mushroom.isSelected())
            sum+=1;
        if (meatballs.isSelected())
            sum+=1;

        toppings = sum * 0.50;
        phrase.setText("比萨成本: " + fmt1.format(totalPrice + toppings));
    }
}
import javafx.application.Application;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.stage.Stage;

//************************************************************************
// PizzaToppings.java
// 展示了复选框的使用。
//************************************************************************
public class PizzaToppings extends Application {

    //--------------------------------------------------------------------
    // 创建并显示程序窗口
    //--------------------------------------------------------------------
    public void start(Stage primaryStage){

        ToppingsOptionPane pane = new ToppingsOptionPane();
        pane.setAlignment(Pos.CENTER);
        pane.setStyle("-fx-background-color: wheat");

        Scene scene = new Scene(pane,400,250);

        primaryStage.setTitle("比萨成本");
        primaryStage.setScene(scene);
        primaryStage.show();
    }

    //--------------------------------------------------------------------
    // 主方法。调用launch方法。
    //--------------------------------------------------------------------
    public static void main(String[] args){
        launch(args);
    }
}
英文:

I need to create a GUI with aligned checkboxes like in this image:
我希望在javaFX中对齐复选框。

My attempted code results in the following image:
我希望在javaFX中对齐复选框。

I used 3 HBoxes, with each HBox containing two toppings. I then added the HBoxes to the ToppingsOptionPane.

What do I need to do to align the checkboxes?

I tried using 2 VBoxes containing three topping each, with the first VBox positioned center left and the second VBox center right. The second VBox ended up displayed under the first VBox, and not beside it. So that didn't work.

import javafx.event.ActionEvent;
import javafx.geometry.Pos;
import javafx.scene.control.CheckBox;
import javafx.scene.layout.HBox;
import javafx.scene.layout.VBox;
import javafx.scene.text.Text;
import javafx.scene.text.Font;
import java.text.NumberFormat;
//************************************************************************
// ToppingsOptionPane.java
// Shows use of check boxes.
//************************************************************************
public class ToppingsOptionPane extends VBox{
private Text phrase;
private double totalPrice;
private CheckBox pepperoni, olives, bacon, ham, mushroom, meatballs;
NumberFormat fmt1 = NumberFormat.getCurrencyInstance();
//--------------------------------------------------------------------
// Constructor. Sets up pane with Text object and check boxes
// that determine the price of the pizza.
//--------------------------------------------------------------------
public ToppingsOptionPane(){
totalPrice = 10;
phrase = new Text("Pizza Cost: " + fmt1.format(totalPrice));
phrase.setFont(new Font("Helvetica", 20));
pepperoni = new CheckBox("Pepperoni");
pepperoni.setOnAction(this::processCheckBoxAction);
olives = new CheckBox("Olives");
olives.setOnAction(this::processCheckBoxAction);
HBox toppings1 = new HBox(pepperoni,olives);
toppings1.setAlignment(Pos.CENTER);
toppings1.setSpacing(20);
bacon = new CheckBox("Bacon");
bacon.setOnAction(this::processCheckBoxAction);
ham = new CheckBox("Ham");
ham.setOnAction(this::processCheckBoxAction);
HBox toppings2 = new HBox(bacon,ham);
toppings2.setAlignment(Pos.CENTER);
toppings2.setSpacing(20);
mushroom = new CheckBox("Mushroom");
mushroom.setOnAction(this::processCheckBoxAction);
meatballs = new CheckBox("Meatballs");
meatballs.setOnAction(this::processCheckBoxAction);
HBox toppings3 = new HBox(mushroom,meatballs);
toppings3.setAlignment(Pos.CENTER);
toppings3.setSpacing(20);
setSpacing(20); // between text and checkboxes
getChildren().addAll(toppings1,toppings2,toppings3,phrase);
}
//--------------------------------------------------------------------
// Event handler. Updates price of the pizza.
//--------------------------------------------------------------------
public void processCheckBoxAction(ActionEvent event){
int sum = 0;
double toppings = 0;
if (pepperoni.isSelected())
sum+=1;
if (olives.isSelected())
sum+=1;
if (bacon.isSelected())
sum+=1;
if (ham.isSelected())
sum+=1;
if (mushroom.isSelected())
sum+=1;
if (meatballs.isSelected())
sum+=1;
toppings = sum * 0.50;
phrase.setText("Pizza Cost: " + fmt1.format(totalPrice + toppings));
}
}
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.stage.Stage;
//************************************************************************
// PizzaToppings.java
// Shows use of check boxes.
//************************************************************************
public class PizzaToppings extends Application {
//--------------------------------------------------------------------
// Creates and shows program window
//--------------------------------------------------------------------
public void start(Stage primaryStage){
ToppingsOptionPane pane = new ToppingsOptionPane();
pane.setAlignment(Pos.CENTER);
pane.setStyle("-fx-background-color: wheat");
Scene scene = new Scene(pane,400,250);
primaryStage.setTitle("Pizza Cost");
primaryStage.setScene(scene);
primaryStage.show();
}
//--------------------------------------------------------------------
// Main method. Calls launch method.
//--------------------------------------------------------------------
public static void main(String[] args){
launch(args);
}
}

答案1

得分: 2

首先,如果有机会的话,我真的建议你使用FXML,它对解决这样的问题非常有帮助。实际上,解决你的问题非常简单,试着将JavaFX视为一个大型的俄罗斯方块游戏,我所做的是使用两个垂直布局(VBox),每个布局包含3个元素,然后将这两个垂直布局放入一个水平布局(HBox)中。

VBox toppings1V = new VBox(pepperoni, bacon, mushroom);
toppings1V.setSpacing(20);
VBox toppings2V = new VBox(olives, ham, meatballs);
toppings2V.setSpacing(20);
HBox hBox = new HBox(toppings1V, toppings2V);
hBox.setSpacing(30);
hBox.setAlignment(Pos.CENTER);

我希望在javaFX中对齐复选框。

英文:

First of all, I would really recommend you to use FXML if you have the chance, it really helps with problems like this. The solution to your problem is pretty easy actually, try to see JavaFX as a big Tetris game, what I did was using two Vboxes each one containing 3 elements, then putting the Vboxes inside an HBox.

VBox toppings1V = new VBox(pepperoni, bacon, mushroom);
toppings1V.setSpacing(20);
VBox toppings2V = new VBox(olives, ham, meatballs);
toppings2V.setSpacing(20);
HBox hBox = new HBox(toppings1V, toppings2V);
hBox.setSpacing(30);
hBox.setAlignment(Pos.CENTER);

我希望在javaFX中对齐复选框。

huangapple
  • 本文由 发表于 2020年9月22日 12:01:24
  • 转载请务必保留本文链接:https://go.coder-hub.com/64002900.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定