这种要并排放置的情况就把一个HBox对象设为Scene的root,然后再把两个panel放到HBox上即可。由于你只是要界面,计算器的计算逻辑我就不实现了,具体代码如下(运行环境:jdk8或以上):
import javafx.application.Application
import javafx.geometry.Pos
import javafx.scene.Scene
import javafx.scene.control.Button
import javafx.scene.control.Label
import javafx.scene.layout.GridPane
import javafx.scene.layout.HBox
import javafx.scene.layout.Priority
import javafx.stage.Stage
public class TwoPane extends Application {
final static private int BUTTON_WIDTH = 40
final static private int BUTTON_HEIGHT = 40
public static void main(String[] args) {
launch(args)
}
@Override
public void start(Stage primaryStage) throws Exception {
primaryStage.setTitle("简易计算器")
Label label = new Label("")
label.setAlignment(Pos.CENTER)
label.setMinWidth(100)
HBox expresssionPanel = new HBox(label)
expresssionPanel.setAlignment(Pos.CENTER)
GridPane keyboardPanel = new GridPane()
for(int i = 1i <= 9++i){
Button btn = new Button(String.valueOf(i))
btn.setPrefSize(BUTTON_WIDTH, BUTTON_HEIGHT)
btn.setOnAction(e ->label.setText(label.getText() + btn.getText()))
keyboardPanel.add(btn, (i - 1) % 3, i >3 ? (i >6 ? 2 : 1) : 0)
}
Button zero = new Button(String.valueOf("0"))
zero.setPrefSize(BUTTON_WIDTH, BUTTON_HEIGHT)
zero.setOnAction(e ->label.setText(label.getText() + zero.getText()))
Button plus = new Button(String.valueOf("+"))
plus.setPrefSize(BUTTON_WIDTH, BUTTON_HEIGHT)
plus.setOnAction(e ->label.setText(label.getText() + plus.getText()))
Button minus = new Button(String.valueOf("-"))
minus.setPrefSize(BUTTON_WIDTH, BUTTON_HEIGHT)
minus.setOnAction(e ->label.setText(label.getText() + minus.getText()))
Button time = new Button(String.valueOf("*"))
time.setPrefSize(BUTTON_WIDTH, BUTTON_HEIGHT)
time.setOnAction(e ->label.setText(label.getText() + time.getText()))
Button divide = new Button(String.valueOf("/"))
divide.setPrefSize(BUTTON_WIDTH, BUTTON_HEIGHT)
divide.setOnAction(e ->label.setText(label.getText() + divide.getText()))
Button calc = new Button(String.valueOf("="))
calc.setPrefSize(BUTTON_WIDTH, BUTTON_HEIGHT)
calc.setOnAction(e ->label.setText(label.getText() + calc.getText())/*TODO 计算逻辑代码待完成*/)
keyboardPanel.add(zero, 0, 3)
keyboardPanel.add(plus, 1, 3)
keyboardPanel.add(minus, 2, 3)
keyboardPanel.add(time, 0, 4)
keyboardPanel.add(divide, 1, 4)
keyboardPanel.add(calc, 2, 4)
HBox root = new HBox(expresssionPanel, keyboardPanel)
HBox.setHgrow(expresssionPanel, Priority.ALWAYS)
primaryStage.setScene(new Scene(root))
primaryStage.show()
}
}
-------------效果如下----------------
这是个JavaFX的布局问题 ,GridPane 是非常灵活的布局. 我觉得比较好用, 问题主要出在有的文本框要跨3列,也就是从0列开始,一直跨3列,
TextField tf = new TextField()gridPane.add(tf,0,0,3,1)//跨三列,跨一行
方法说明
public void add(Node child,int columnIndex,
int rowIndex,
int colspan,
int rowspan)
不跨行不跨列的时候,也使用add方法
public void add(Node child,int columnIndex,
int rowIndex)
完整的参考代码如下[计算器布局参考代码]
import javafx.application.Applicationimport javafx.geometry.Insets
import javafx.geometry.Pos
import javafx.scene.Scene
import javafx.scene.control.Button
import javafx.scene.control.TextField
import javafx.scene.layout.GridPane
import javafx.scene.text.Font
import javafx.scene.text.FontWeight
import javafx.stage.Stage
public class GridPaneDemo extends Application{
private static final String str="123+456-789×C0.÷"
public static void main(String[] args) {
launch(args)
}
@Override
public void start(Stage primaryStage) throws Exception {
GridPane root = new GridPane()
root.setAlignment(Pos.CENTER)
root.setPadding(new Insets(15))
root.setHgap(10)
root.setVgap(10)
Scene scene = new Scene(root,300,350)
TextField tf = new TextField()
tf.setPrefHeight(50)//设置文本框的 最佳高度为50
tf.setFont(Font.font(java.awt.Font.MONOSPACED, FontWeight.BOLD,18))//字体设置
root.add(tf,0,0,3, 1)//跨三列,跨一行
Button btn1 = new Button("=")
btn1.setPrefSize(50, 50)//设置按钮的最佳宽 高
root.add(btn1, 3, 0)
for (int i = 0 i < str.length() i++) {
Button btn = new Button(str.charAt(i)+"")
btn.setPrefSize(50, 50)
root.add(btn, i%4, i/4+1)//计算出列和行,并添加到GridPane上去
}
primaryStage.setScene(scene)
primaryStage.setTitle("Calculator")
primaryStage.show()
}
}
public final class FileChooserSample extends Application {
private Desktop desktop = Desktop.getDesktop()
@Override
public void start(final Stage stage) {
stage.setTitle("File Chooser Sample")
final FileChooser fileChooser = new FileChooser()
final Button openButton = new Button("Open a Picture...")
final Button openMultipleButton = new Button("Open Pictures...")
openButton.setOnAction(
new EventHandler<ActionEvent>() {
@Override
public void handle(final ActionEvent e) {
File file = fileChooser.showOpenDialog(stage)
if (file != null) {
openFile(file)
}
}
})
openMultipleButton.setOnAction(
new EventHandler<ActionEvent>() {
@Override
public void handle(final ActionEvent e) {
List<File>list =
fileChooser.showOpenMultipleDialog(stage)
if (list != null) {
for (File file : list) {
openFile(file)
}
}
}
})
final GridPane inputGridPane = new GridPane()
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)