Web App (使用JavaScript, HTML, CSS)fmtc.cn
HTML (结构):
html
Hello, World!
Click Me CSS (样式):css
body {
font-family: Arial, sans-serif;
text-align: center;
padding-top: 20vh;
}
button {
padding: 10px 20px;
font-size: 16px;
}
JavaScript (逻辑):
javascript
function sayHello() {
alert(“Hello, World!”);
}
移动App (使用Flutter, Dart)
Flutter 是一个用于构建跨平台移动应用的框架,使用 Dart 语言。
Flutter Dart 代码示例 (一个简单的按钮,点击后显示一个消息):
dart
import ‘package:flutter/material.dart’;
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
title: ‘Flutter Demo’,
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: MyHomePage(title: ‘Flutter Demo Home Page’),
);
}
}
class MyHomePage extends StatefulWidget {
MyHomePage({Key? key, required this.title}) : super(key: key);
final String title;
@override
_MyHomePageState createState() => _MyHomePageState();
}
class _MyHomePageState extends State {
void _showMessage() {
showDialog(
context: context,
builder: (BuildContext context) {
return AlertDialog(
title: new Text(“Hello”),
content: new Text(“Hello, World!”),
actions: [
new TextButton(
child: new Text(“OK”),
onPressed: () { Navigator.of(context).pop(); },
),
],
);
},
);
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text(widget.title),
),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Text(
‘You have pushed the button this many times:’,
),
ElevatedButton(
child: Text(‘Show message’),
onPressed: _showMessage,
),
],
),
),
);
}
}
桌面App (使用Python, Tkinter)
这是一个简单的Python Tkinter桌面应用示例:
python
import tkinter as tk
from tkinter import messagebox
def say_hello():
messagebox.showinfo(“Hello”, “Hello, World!”)
root = tk.Tk()
root.title(“My Desktop App”)
button = tk.Button(root, text=“Click Me”, command=say_hello)
button.pack(pady=20)
root.mainloop()
由于生成一个完整的App代码涉及多个层面(如前端、后端、数据库、界面设计等),而且每种电脑语言通常有其特定的应用场景(例如,用于Web开发、移动应用开发、桌面应用开发等),我将为你提供几个不同场景下的简单示例代码。
Web App (使用JavaScript, HTML, CSS)
HTML (结构):
html
Hello, World!
Click Me CSS (样式):css
body {
font-family: Arial, sans-serif;
text-align: center;
padding-top: 20vh;
}
button {
padding: 10px 20px;
font-size: 16px;
}
JavaScript (逻辑):
javascript
function sayHello() {
alert(“Hello, World!”);
}
移动App (使用Flutter, Dart)
Flutter 是一个用于构建跨平台移动应用的框架,使用 Dart 语言。
Flutter Dart 代码示例 (一个简单的按钮,点击后显示一个消息):
dart
import ‘package:flutter/material.dart’;
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
title: ‘Flutter Demo’,
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: MyHomePage(title: ‘Flutter Demo Home Page’),
);
}
}
class MyHomePage extends StatefulWidget {
MyHomePage({Key? key, required this.title}) : super(key: key);
final String title;
@override
_MyHomePageState createState() => _MyHomePageState();
}
class _MyHomePageState extends State {
void _showMessage() {
showDialog(
context: context,
builder: (BuildContext context) {
return AlertDialog(
title: new Text(“Hello”),
content: new Text(“Hello, World!”),
actions: [
new TextButton(
child: new Text(“OK”),
onPressed: () { Navigator.of(context).pop(); },
),
],
);
},
);
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text(widget.title),
),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Text(
‘You have pushed the button this many times:’,
),
ElevatedButton(
child: Text(‘Show message’),
onPressed: _showMessage,
),
],
),
),
);
}
}
桌面App (使用Python, Tkinter)
这是一个简单的Python Tkinter桌面应用示例:
python
import tkinter as tk
from tkinter import messagebox
def say_hello():
messagebox.showinfo(“Hello”, “Hello, World!”)
root = tk.Tk()
root.title(“My Desktop App”)
button = tk.Button(root, text=“Click Me”, command=say_hello)
button.pack(pady=20)
root.mainloop()