帮我用javafx写一个歌手比赛系统
时间: 2023-12-31 21:04:42 浏览: 74
好的,以下是一个简单的歌手比赛系统的JavaFX代码示例:
Main.java
```
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Stage;
public class Main extends Application {
@Override
public void start(Stage primaryStage) throws Exception{
Parent root = FXMLLoader.load(getClass().getResource("sample.fxml"));
primaryStage.setTitle("Singer Contest System");
primaryStage.setScene(new Scene(root, 800, 600));
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
}
```
Controller.java
```
import javafx.fxml.FXML;
import javafx.scene.control.*;
import java.sql.*;
public class Controller {
@FXML
private TextField nameField;
@FXML
private ComboBox<String> genderBox;
@FXML
private TextField ageField;
@FXML
private Button addButton;
@FXML
private TableView<Singer> singerTable;
@FXML
private TableColumn<Singer, String> nameColumn;
@FXML
private TableColumn<Singer, String> genderColumn;
@FXML
private TableColumn<Singer, Integer> ageColumn;
private Connection connection;
private Statement statement;
public void initialize() {
genderBox.getItems().addAll("Male", "Female");
nameColumn.setCellValueFactory(cellData -> cellData.getValue().nameProperty());
genderColumn.setCellValueFactory(cellData -> cellData.getValue().genderProperty());
ageColumn.setCellValueFactory(cellData -> cellData.getValue().ageProperty().asObject());
try {
Class.forName("com.mysql.jdbc.Driver");
connection = DriverManager.getConnection("jdbc:mysql://localhost/singercontest", "root", "");
statement = connection.createStatement();
ResultSet rs = statement.executeQuery("SELECT * FROM singer");
while (rs.next()) {
Singer singer = new Singer(rs.getString("name"), rs.getString("gender"), rs.getInt("age"));
singerTable.getItems().add(singer);
}
} catch (ClassNotFoundException | SQLException e) {
e.printStackTrace();
}
}
public void addSinger() {
try {
String name = nameField.getText();
String gender = genderBox.getValue();
int age = Integer.parseInt(ageField.getText());
Singer singer = new Singer(name, gender, age);
singerTable.getItems().add(singer);
PreparedStatement ps = connection.prepareStatement("INSERT INTO singer(name, gender, age) VALUES (?, ?, ?)");
ps.setString(1, name);
ps.setString(2, gender);
ps.setInt(3, age);
ps.executeUpdate();
} catch (NumberFormatException | SQLException e) {
e.printStackTrace();
}
}
}
```
sample.fxml
```
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>
<?import javafx.scene.layout.AnchorPane?>
<?import javafx.scene.text.*?>
<AnchorPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="600.0" prefWidth="800.0" xmlns="http://javafx.com/javafx/11.0.2" xmlns:fx="http://javafx.com/fxml/1">
<children>
<Label layoutX="336.0" layoutY="51.0" text="Singer Contest System">
<font>
<Font size="24.0" />
</font>
</Label>
<Label layoutX="110.0" layoutY="120.0" text="Name:" />
<Label layoutX="110.0" layoutY="159.0" text="Gender:" />
<Label layoutX="110.0" layoutY="198.0" text="Age:" />
<TextField fx:id="nameField" layoutX="180.0" layoutY="117.0" />
<ComboBox fx:id="genderBox" layoutX="180.0" layoutY="156.0" prefWidth="150.0" />
<TextField fx:id="ageField" layoutX="180.0" layoutY="195.0" />
<Button fx:id="addButton" layoutX="180.0" layoutY="249.0" mnemonicParsing="false" onAction="#addSinger" text="Add Singer" />
<TableView fx:id="singerTable" layoutX="110.0" layoutY="315.0" prefHeight="200.0" prefWidth="580.0">
<columns>
<TableColumn fx:id="nameColumn" minWidth="100.0" prefWidth="200.0" text="Name" />
<TableColumn fx:id="genderColumn" minWidth="100.0" prefWidth="200.0" text="Gender" />
<TableColumn fx:id="ageColumn" minWidth="100.0" prefWidth="200.0" text="Age" />
</columns>
</TableView>
</children>
</AnchorPane>
```
上述代码实现了一个简单的歌手比赛系统,包括添加歌手和查看歌手列表的功能。可以根据需求进行扩展。注意需要在MySQL中创建一个名为singercontest的数据库,并且添加一个名为singer的表,包括name、gender和age三个字段。
阅读全文