在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是最常用的选择。