在Ubuntu上設計Java圖形界面(GUI)有多種方法,以下是一些常用的工具和庫:
Swing是Java的一個圖形用戶界面(GUI)工具包,它是AWT(Abstract Window Toolkit)的擴展,提供了更豐富的組件和更好的跨平臺兼容性。
import javax.swing.*;
public class SimpleSwingApp {
public static void main(String[] args) {
JFrame frame = new JFrame("Simple Swing App");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(300, 200);
JLabel label = new JLabel("Hello, Swing!");
frame.add(label);
frame.setVisible(true);
}
}
JavaFX是Java的一個現代GUI工具包,提供了更豐富的圖形和媒體功能,支持CSS樣式和FXML布局。
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.scene.layout.StackPane;
import javafx.stage.Stage;
public class SimpleJavaFXApp extends Application {
@Override
public void start(Stage primaryStage) {
Label label = new Label("Hello, JavaFX!");
StackPane root = new StackPane();
root.getChildren().add(label);
Scene scene = new Scene(root, 300, 200);
primaryStage.setTitle("Simple JavaFX App");
primaryStage.setScene(scene);
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
}
Qt Jambi是一個基于Qt框架的Java綁定,提供了強大的GUI組件和布局管理功能。
import com.trolltech.qt.gui.*;
public class SimpleQtJambiApp {
public static void main(String[] args) {
QApplication.initialize(args);
QMainWindow mainWindow = new QMainWindow();
QLabel label = new QLabel("Hello, Qt Jambi!");
mainWindow.setCentralWidget(label);
mainWindow.show();
QApplication.exec();
}
}
Apache Pivot是一個開源的Java GUI框架,提供了豐富的組件和布局管理功能。
import org.apache.pivot.wtk.*;
public class SimplePivotApp implements EntryPoint {
public void startup(Display display, Map<String, String> properties) throws Exception {
Shell shell = new Shell(display);
shell.setText("Simple Pivot App");
Label label = new Label();
label.setText("Hello, Pivot!");
shell.getContent().add(label);
shell.pack();
shell.open(display.getEventQueue(), Shell.OPEN_MODE_DEFAULT);
}
public static void main(String[] args) {
Display display = new Display();
try {
new SimplePivotApp().startup(display, null);
} finally {
display.destroy();
}
}
}
選擇哪個工具取決于你的具體需求和偏好。對于大多數Java GUI開發,Swing和JavaFX是最常用的選擇。