标签边框不对齐,不齐平
Label borders not lining up, not flush
这是我在 JavaFX 中的当前输出。
我希望所有标签和边框齐平。
我将所有 Insets 设置为相同,为什么一切都不对齐?
另一个问题是边框的双重重叠在边框上创建了一个较暗的图像,我不确定在为我的标签设置样式时如何解决这个问题。这张图下面是相关的代码。谢谢你。
public void buildUI() {
HBox hbox = new HBox(); // create Hbox
hbox.setPadding(new Insets(20,20,40,215) ); // centers month name over Grid
Label month = new Label ("October"); // create october label
//hbox.setVgap(50.0);
this.setTop(hbox); // set hbox to top of borderPane
hbox.getChildren().addAll(month); // add month to hbox
this.setCenter(grid); // add gridPane to center of BorderPane
grid.setPadding(new Insets(20,20,20,20) ); // add insets to gridPane for aesthetic
// below makes S-M-T-W-T-F and green fill
for (int i = 0; i <= weekDays.length - 1; i++) {
Label daysOfWeek = new Label(weekDays[i]);
grid.add(daysOfWeek, i + 1, 1);
daysOfWeek.setPadding(new Insets(25,30,25,30) );
daysOfWeek.setStyle("-fx-text-fill: white;" +
"-fx-background-color: green;" + "-fx-border-color: black;");
}// end for loop to cycle through days of week
//below are the days of the month
for (int i = 1; i <= FRAMES; i++) {
int yPos = ((i - 1) / 7) + 2;
int xPos = i - (7 * (yPos -2) );
if (i < 32) {
Label monthDays = new Label(String.valueOf(i) );
grid.add(monthDays, xPos, yPos);
monthDays.setPadding(new Insets(25,30,25,30) );
monthDays.setStyle("-fx-border-color: black;");
} else if (i > 32) {
// below creating november labels days 1 -- 4
Label nov1 = new Label("1");
grid.add(nov1, 4, 6);
Label nov2 = new Label("2");
grid.add(nov2, 5, 6);
Label nov3 = new Label("3");
grid.add(nov3, 6, 6);
Label nov4 = new Label("4");
grid.add(nov4, 7, 6);
// below padding and making borders for labels
nov1.setPadding(new Insets(25,30,25,30) );
nov1.setStyle("-fx-text-fill: white;" + "-fx-border-color: black;" + "-fx-background-color: grey;");
nov2.setPadding(new Insets(25,30,25,30) );
nov2.setStyle("-fx-text-fill: white;" + "-fx-border-color: black;" + "-fx-background-color: grey;");
nov3.setPadding(new Insets(25,30,25,30) );
nov3.setStyle("-fx-text-fill: white;" + "-fx-border-color: black;" + "-fx-background-color: grey;");
nov4.setPadding(new Insets(25,30,25,30) );
nov4.setStyle("-fx-text-fill: white;" + "-fx-border-color: black;" + "-fx-background-color: grey;");
}
}// end for loop to cycle through days of month
}
Label
s 的最大大小与文本和填充所需的大小相匹配。 GridPane
不会将 Label
的大小调整为更大的大小,这是 1 位数字填充该列所需的大小。
只需将最大大小设置为无穷大即可解决此问题。
如果不绘制双边框,您可以将边框大小设置为顶部和右侧为 1,底部和左侧为 0,将 GridPane
周围的边框设置为底部和左侧的宽度为 1,顶部和右侧的宽度宽度为 0 并将网格包裹在 StackPane
中作为填充:
private static final Insets LABEL_PADDING = new Insets(25, 30, 25, 30);
private static Label createLabel(String text) {
Label label = new Label(text);
label.setMaxSize(Double.POSITIVE_INFINITY, Double.POSITIVE_INFINITY);
label.setPadding(LABEL_PADDING);
return label;
}
// below makes S-M-T-W-T-F and green fill
for (int i = 0; i <= weekDays.length - 1; i++) {
Label daysOfWeek = createLabel(weekDays[i]);
grid.add(daysOfWeek, i, 0);
daysOfWeek.setStyle("-fx-text-fill: white;"
+ "-fx-background-color: green;" + "-fx-border-color: black;" + "-fx-border-width: 1 1 0 0;");
}// end for loop to cycle through days of week
final String otherMonthStyle = "-fx-text-fill: white;-fx-border-color: black;-fx-background-color: grey;-fx-border-width: 1 1 0 0;";
//below are the days of the month
for (int i = 1; i <= FRAMES; i++) {
int yPos = ((i - 1) / 7) + 1;
int xPos = (i - 1) % 7;
if (i < 32) {
Label monthDays = createLabel(String.valueOf(i));
grid.add(monthDays, xPos, yPos);
monthDays.setStyle("-fx-border-color: black;-fx-border-width: 1 1 0 0;");
} else if (i > 32) {
// below creating november labels days 1 -- 4
Label nov1 = createLabel("1");
grid.add(nov1, 3, 5);
Label nov2 = createLabel("2");
grid.add(nov2, 4, 5);
Label nov3 = createLabel("3");
grid.add(nov3, 5, 5);
Label nov4 = createLabel("4");
grid.add(nov4, 6, 5);
// below padding and making borders for labels
nov1.setStyle(otherMonthStyle);
nov2.setStyle(otherMonthStyle);
nov3.setStyle(otherMonthStyle);
nov4.setStyle(otherMonthStyle);
}
}// end for loop to cycle through days of month
grid.setStyle("-fx-border-color: black;" + "-fx-border-width: 0 0 1 1;");
StackPane gridWrapper = new StackPane(grid);
gridWrapper.setPadding(new Insets(20));
这是我在 JavaFX 中的当前输出。
我希望所有标签和边框齐平。
我将所有 Insets 设置为相同,为什么一切都不对齐?
另一个问题是边框的双重重叠在边框上创建了一个较暗的图像,我不确定在为我的标签设置样式时如何解决这个问题。这张图下面是相关的代码。谢谢你。
public void buildUI() {
HBox hbox = new HBox(); // create Hbox
hbox.setPadding(new Insets(20,20,40,215) ); // centers month name over Grid
Label month = new Label ("October"); // create october label
//hbox.setVgap(50.0);
this.setTop(hbox); // set hbox to top of borderPane
hbox.getChildren().addAll(month); // add month to hbox
this.setCenter(grid); // add gridPane to center of BorderPane
grid.setPadding(new Insets(20,20,20,20) ); // add insets to gridPane for aesthetic
// below makes S-M-T-W-T-F and green fill
for (int i = 0; i <= weekDays.length - 1; i++) {
Label daysOfWeek = new Label(weekDays[i]);
grid.add(daysOfWeek, i + 1, 1);
daysOfWeek.setPadding(new Insets(25,30,25,30) );
daysOfWeek.setStyle("-fx-text-fill: white;" +
"-fx-background-color: green;" + "-fx-border-color: black;");
}// end for loop to cycle through days of week
//below are the days of the month
for (int i = 1; i <= FRAMES; i++) {
int yPos = ((i - 1) / 7) + 2;
int xPos = i - (7 * (yPos -2) );
if (i < 32) {
Label monthDays = new Label(String.valueOf(i) );
grid.add(monthDays, xPos, yPos);
monthDays.setPadding(new Insets(25,30,25,30) );
monthDays.setStyle("-fx-border-color: black;");
} else if (i > 32) {
// below creating november labels days 1 -- 4
Label nov1 = new Label("1");
grid.add(nov1, 4, 6);
Label nov2 = new Label("2");
grid.add(nov2, 5, 6);
Label nov3 = new Label("3");
grid.add(nov3, 6, 6);
Label nov4 = new Label("4");
grid.add(nov4, 7, 6);
// below padding and making borders for labels
nov1.setPadding(new Insets(25,30,25,30) );
nov1.setStyle("-fx-text-fill: white;" + "-fx-border-color: black;" + "-fx-background-color: grey;");
nov2.setPadding(new Insets(25,30,25,30) );
nov2.setStyle("-fx-text-fill: white;" + "-fx-border-color: black;" + "-fx-background-color: grey;");
nov3.setPadding(new Insets(25,30,25,30) );
nov3.setStyle("-fx-text-fill: white;" + "-fx-border-color: black;" + "-fx-background-color: grey;");
nov4.setPadding(new Insets(25,30,25,30) );
nov4.setStyle("-fx-text-fill: white;" + "-fx-border-color: black;" + "-fx-background-color: grey;");
}
}// end for loop to cycle through days of month
}
Label
s 的最大大小与文本和填充所需的大小相匹配。 GridPane
不会将 Label
的大小调整为更大的大小,这是 1 位数字填充该列所需的大小。
只需将最大大小设置为无穷大即可解决此问题。
如果不绘制双边框,您可以将边框大小设置为顶部和右侧为 1,底部和左侧为 0,将 GridPane
周围的边框设置为底部和左侧的宽度为 1,顶部和右侧的宽度宽度为 0 并将网格包裹在 StackPane
中作为填充:
private static final Insets LABEL_PADDING = new Insets(25, 30, 25, 30);
private static Label createLabel(String text) {
Label label = new Label(text);
label.setMaxSize(Double.POSITIVE_INFINITY, Double.POSITIVE_INFINITY);
label.setPadding(LABEL_PADDING);
return label;
}
// below makes S-M-T-W-T-F and green fill
for (int i = 0; i <= weekDays.length - 1; i++) {
Label daysOfWeek = createLabel(weekDays[i]);
grid.add(daysOfWeek, i, 0);
daysOfWeek.setStyle("-fx-text-fill: white;"
+ "-fx-background-color: green;" + "-fx-border-color: black;" + "-fx-border-width: 1 1 0 0;");
}// end for loop to cycle through days of week
final String otherMonthStyle = "-fx-text-fill: white;-fx-border-color: black;-fx-background-color: grey;-fx-border-width: 1 1 0 0;";
//below are the days of the month
for (int i = 1; i <= FRAMES; i++) {
int yPos = ((i - 1) / 7) + 1;
int xPos = (i - 1) % 7;
if (i < 32) {
Label monthDays = createLabel(String.valueOf(i));
grid.add(monthDays, xPos, yPos);
monthDays.setStyle("-fx-border-color: black;-fx-border-width: 1 1 0 0;");
} else if (i > 32) {
// below creating november labels days 1 -- 4
Label nov1 = createLabel("1");
grid.add(nov1, 3, 5);
Label nov2 = createLabel("2");
grid.add(nov2, 4, 5);
Label nov3 = createLabel("3");
grid.add(nov3, 5, 5);
Label nov4 = createLabel("4");
grid.add(nov4, 6, 5);
// below padding and making borders for labels
nov1.setStyle(otherMonthStyle);
nov2.setStyle(otherMonthStyle);
nov3.setStyle(otherMonthStyle);
nov4.setStyle(otherMonthStyle);
}
}// end for loop to cycle through days of month
grid.setStyle("-fx-border-color: black;" + "-fx-border-width: 0 0 1 1;");
StackPane gridWrapper = new StackPane(grid);
gridWrapper.setPadding(new Insets(20));