写一个Java界面化代码
时间: 2023-12-13 08:03:58 浏览: 31
下面是一个简单的Java界面化代码,使用Swing库实现:
```java
import javax.swing.*;
public class MyGUI {
public static void main(String[] args) {
// 创建一个 JFrame 对象
JFrame frame = new JFrame("My GUI");
// 设置窗口的大小和位置
frame.setSize(300, 200);
frame.setLocationRelativeTo(null); // 居中显示
// 创建一个标签
JLabel label = new JLabel("Hello, World!");
// 设置标签的位置和大小
label.setBounds(50, 50, 200, 30);
// 将标签添加到窗口中
frame.add(label);
// 设置窗口可见
frame.setVisible(true);
// 设置窗口关闭时退出程序
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
```
这段代码创建了一个简单的窗口,并在窗口中添加了一个标签。你可以根据需要修改窗口的大小、标签的文本等属性。
相关问题
用java写一个图形化界面
好的,以下是一个简单的Java图形化界面示例代码:
```java
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
public class MyGUI {
public static void main(String[] args) {
JFrame frame = new JFrame("My GUI");
frame.setSize(300, 200);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel panel = new JPanel();
JLabel label = new JLabel("Hello, world!");
panel.add(label);
frame.add(panel);
frame.setVisible(true);
}
}
```
这个示例创建了一个包含一个标签的面板,并将面板添加到框架中。然后将框架设置为可见。你可以根据需求添加更多的组件和功能。
用java语言写一个图形界面化计算器
好的,以下是一个简单的Java图形界面计算器的代码:
```
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class Calculator extends JFrame implements ActionListener {
private static final long serialVersionUID = 1L;
private JTextField displayField;
private JButton[] buttons;
private String[] buttonLabels = { "7", "8", "9", "/", "4", "5", "6", "*", "1", "2", "3", "-", "0", ".", "=", "+" };
private JPanel buttonPanel;
private double num1, num2;
private String operator;
public Calculator() {
super("Calculator");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
displayField = new JTextField(30);
displayField.setEditable(false);
buttons = new JButton[buttonLabels.length];
buttonPanel = new JPanel(new GridLayout(4, 4));
for (int i = 0; i < buttonLabels.length; i++) {
buttons[i] = new JButton(buttonLabels[i]);
buttons[i].addActionListener(this);
buttonPanel.add(buttons[i]);
}
getContentPane().add(displayField, BorderLayout.NORTH);
getContentPane().add(buttonPanel, BorderLayout.CENTER);
pack();
setVisible(true);
}
public void actionPerformed(ActionEvent e) {
String command = e.getActionCommand();
if (command.matches("[0-9]+")) {
displayField.setText(displayField.getText() + command);
} else if (command.equals(".")) {
if (!displayField.getText().contains(".")) {
displayField.setText(displayField.getText() + ".");
}
} else if (command.matches("[+\\-*/=]")) {
if (operator == null) {
num1 = Double.parseDouble(displayField.getText());
operator = command;
displayField.setText("");
} else {
num2 = Double.parseDouble(displayField.getText());
switch (operator) {
case "+":
num1 += num2;
break;
case "-":
num1 -= num2;
break;
case "*":
num1 *= num2;
break;
case "/":
num1 /= num2;
break;
}
if (command.equals("=")) {
displayField.setText("" + num1);
operator = null;
} else {
operator = command;
displayField.setText("");
}
}
}
}
public static void main(String[] args) {
new Calculator();
}
}
```
希望这个代码能够帮到你!