创建一个窗口,里面设置一个文本标签,一个start按钮,一个end按钮。点击start时会在文本标签里显示“开始start”,当点击end时,会显示“结束end”
时间: 2024-01-21 18:16:07 浏览: 15
您可以使用Python的Tkinter库来创建窗口和控件。以下是一个示例代码:
```python
from tkinter import *
def start():
label.config(text="开始start")
def end():
label.config(text="结束end")
# 创建窗口
root = Tk()
# 创建标签
label = Label(root, text="这是一个标签", font=("Arial", 16))
label.pack(pady=20)
# 创建按钮
start_button = Button(root, text="Start", command=start)
start_button.pack(side=LEFT, padx=10)
end_button = Button(root, text="End", command=end)
end_button.pack(side=RIGHT, padx=10)
# 运行窗口
root.mainloop()
```
运行代码后,会出现一个窗口,里面有一个文本标签和两个按钮。当您点击“Start”按钮时,标签上会显示“开始start”,当您点击“End”按钮时,标签上会显示“结束end”。
相关问题
用JAVA创建一个窗口,里面设置一个文本标签,一个start按钮,一个end按钮,点击start时,会在文本标内显示“开始start”
以下是用Java Swing创建一个窗口,里面有一个文本标签和两个按钮,点击Start按钮会在文本标签内显示“开始start”:
```java
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class MyWindow extends JFrame implements ActionListener {
private JLabel label;
private JButton startButton, endButton;
public MyWindow() {
setTitle("My Window");
setSize(400, 300);
setLocationRelativeTo(null);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// 创建文本标签
label = new JLabel("Hello World", JLabel.CENTER);
add(label, BorderLayout.CENTER);
// 创建Start按钮
startButton = new JButton("Start");
startButton.addActionListener(this);
add(startButton, BorderLayout.WEST);
// 创建End按钮
endButton = new JButton("End");
endButton.addActionListener(this);
add(endButton, BorderLayout.EAST);
}
public void actionPerformed(ActionEvent e) {
if (e.getSource() == startButton) {
label.setText("开始start");
} else if (e.getSource() == endButton) {
label.setText("结束end");
}
}
public static void main(String[] args) {
MyWindow window = new MyWindow();
window.setVisible(true);
}
}
```
运行程序后,会弹出一个窗口,其中有一个文本标签和两个按钮,点击Start按钮会在文本标签内显示“开始start”。
用java写创建一个窗口,里面设置一个文本标签,一个start按钮,一个end按钮。点击start时会在文本标签里显示“开始start”,当点击end时,会显示“结束end”的代码
下面是用 Java Swing 实现的创建窗口,并在窗口中添加一个文本标签和两个按钮的代码:
```java
import javax.swing.*;
import java.awt.event.*;
public class MyWindow extends JFrame {
private JLabel label;
private JButton startButton;
private JButton endButton;
public MyWindow() {
super("My Window");
label = new JLabel("Hello World!");
startButton = new JButton("Start");
endButton = new JButton("End");
startButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
label.setText("开始start");
}
});
endButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
label.setText("结束end");
}
});
JPanel panel = new JPanel();
panel.add(label);
panel.add(startButton);
panel.add(endButton);
getContentPane().add(panel);
setSize(300, 100);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
}
public static void main(String[] args) {
MyWindow window = new MyWindow();
}
}
```
运行程序后,会弹出一个窗口,其中包含一个文本标签和两个按钮。当点击“Start”按钮时,文本标签会显示“开始start”,当点击“End”按钮时,文本标签会显示“结束end”。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)