- Python (使用Tkinter库创建简单的GUI应用)gzjiazheng.cn
python
import tkinter as tk
def hello_world():
print(“Hello, World!”)
root = tk.Tk()
button = tk.Button(root, text=“Click me!”, command=hello_world)
button.pack()
root.mainloop()
2. Java (使用Swing库创建简单的GUI应用)
java
import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class HelloWorldApp {
public static void main(String[] args) {
JButton button = new JButton(“Click me!”);
button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
System.out.println(“Hello, World!”);
}
});
JFrame frame = new JFrame("Hello World");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(300, 200);
frame.getContentPane().add(button);
frame.setVisible(true);
}
}
3. JavaScript (使用HTML和Node.js创建简单的Web应用)
HTML:
html
javascript
function helloWorld() {
console.log(“Hello, World!”);
}
4. Swift (iOS应用)
由于Swift需要Xcode和iOS SDK来完整编写和运行,我将提供一个简化的代码片段:
swift
import UIKit
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
let button = UIButton(type: .system)
button.setTitle("Click me!", for: .normal)
button.addTarget(self, action: #selector(helloWorld), for: .touchUpInside)
view.addSubview(button)
}
@objc func helloWorld() {
print("Hello, World!")
}
}
请注意,这些代码只是应用程序的一小部分,并且可能需要额外的设置和依赖项才能完全运行。对于更复杂的应用程序,你可能需要学习更多关于特定语言、框架和平台的知识。由于直接编写一个完整的应用程序的代码太长并且会因应用类型、功能和目标平台(如Android、iOS、桌面应用等)的不同而有所不同,我将为你提供一个简化版的概念和代码片段来展示如何用不同语言来编写一个应用程序的一部分。
- Python (使用Tkinter库创建简单的GUI应用)
python
import tkinter as tk
def hello_world():
print(“Hello, World!”)
root = tk.Tk()
button = tk.Button(root, text=“Click me!”, command=hello_world)
button.pack()
root.mainloop()
2. Java (使用Swing库创建简单的GUI应用)
java
import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class HelloWorldApp {
public static void main(String[] args) {
JButton button = new JButton(“Click me!”);
button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
System.out.println(“Hello, World!”);
}
});
JFrame frame = new JFrame("Hello World");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(300, 200);
frame.getContentPane().add(button);
frame.setVisible(true);
}
}
3. JavaScript (使用HTML和Node.js创建简单的Web应用)
HTML:
html
javascript
function helloWorld() {
console.log(“Hello, World!”);
}
4. Swift (iOS应用)
由于Swift需要Xcode和iOS SDK来完整编写和运行,我将提供一个简化的代码片段:
swift
import UIKit
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
let button = UIButton(type: .system)
button.setTitle("Click me!", for: .normal)
button.addTarget(self, action: #selector(helloWorld), for: .touchUpInside)
view.addSubview(button)
}
@objc func helloWorld() {
print("Hello, World!")
}
}
请注意,这些代码只是应用程序的一小部分,并且可能需要额外的设置和依赖项才能完全运行。对于更复杂的应用程序,你可能需要学习更多关于特定语言、框架和平台的知识。