1.实现一个窗体,要求:(1)标题显示: GUI 实验;(2)窗体尺寸为:长400像素,宽300像素,窗体左上角坐标位置为(150,100),分别使用 Dimension 类和 Point 类实现; (3)窗体内通过标签控件显示国旗图标和文字内容:中华人民共和国 字体为:幼圆字体,20号,红色,加粗,倾斜。1.仿照 window 计算器,实现计算器界面。 2.使用绝对定位,实现注册和登录界面。
时间: 2023-10-06 22:10:52 浏览: 269
实现一个窗体:
```java
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Point;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.ImageIcon;
public class MyFrame extends JFrame {
public MyFrame() {
this.setTitle("GUI 实验");
this.setSize(new Dimension(400, 300));
this.setLocation(new Point(150, 100));
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JLabel label = new JLabel("中华人民共和国", new ImageIcon("china.png"), JLabel.CENTER);
label.setFont(new Font("幼圆", Font.BOLD + Font.ITALIC, 20));
label.setForeground(Color.RED);
this.add(label);
}
public static void main(String[] args) {
MyFrame frame = new MyFrame();
frame.setVisible(true);
}
}
```
实现一个计算器界面:
```java
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextField;
public class Calculator extends JFrame implements ActionListener {
private JTextField textField;
private double firstNumber, secondNumber;
private String operator;
public Calculator() {
this.setTitle("计算器");
this.setSize(300, 400);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel panel = new JPanel();
panel.setLayout(new GridLayout(4, 4, 5, 5));
String[] buttonLabels = { "7", "8", "9", "/", "4", "5", "6", "*", "1", "2", "3", "-", "0", ".", "=", "+" };
JButton[] buttons = new JButton[buttonLabels.length];
for (int i = 0; i < buttons.length; i++) {
buttons[i] = new JButton(buttonLabels[i]);
buttons[i].addActionListener(this);
panel.add(buttons[i]);
}
textField = new JTextField();
textField.setEditable(false);
textField.setBackground(Color.WHITE);
this.add(textField, BorderLayout.NORTH);
this.add(panel, BorderLayout.CENTER);
}
public void actionPerformed(ActionEvent e) {
String command = e.getActionCommand();
if (command.matches("\\d")) {
textField.setText(textField.getText() + command);
} else if (command.matches("[+\\-*/]")) {
firstNumber = Double.parseDouble(textField.getText());
operator = command;
textField.setText("");
} else if (command.equals(".")) {
if (!textField.getText().contains(".")) {
textField.setText(textField.getText() + command);
}
} else if (command.equals("=")) {
secondNumber = Double.parseDouble(textField.getText());
double result = 0;
switch (operator) {
case "+":
result = firstNumber + secondNumber;
break;
case "-":
result = firstNumber - secondNumber;
break;
case "*":
result = firstNumber * secondNumber;
break;
case "/":
result = firstNumber / secondNumber;
break;
}
textField.setText(String.valueOf(result));
} else if (command.equals("C")) {
textField.setText("");
}
}
public static void main(String[] args) {
Calculator calculator = new Calculator();
calculator.setVisible(true);
}
}
```
使用绝对定位,实现注册和登录界面:
```java
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;
public class Login extends JFrame implements ActionListener {
private static final int WIDTH = 400;
private static final int HEIGHT = 300;
private JTextField usernameField;
private JPasswordField passwordField;
public Login() {
this.setTitle("登录");
this.setSize(WIDTH, HEIGHT);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
int x = (screenSize.width - WIDTH) / 2;
int y = (screenSize.height - HEIGHT) / 2;
this.setLocation(x, y);
JPanel panel = new JPanel(null);
JLabel titleLabel = new JLabel("欢迎使用我的系统");
titleLabel.setFont(new Font("微软雅黑", Font.BOLD, 20));
titleLabel.setForeground(Color.BLUE);
titleLabel.setBounds(120, 30, 200, 30);
panel.add(titleLabel);
JLabel usernameLabel = new JLabel("用户名:");
usernameLabel.setFont(new Font("微软雅黑", Font.PLAIN, 16));
usernameLabel.setBounds(80, 80, 80, 30);
panel.add(usernameLabel);
usernameField = new JTextField();
usernameField.setBounds(150, 80, 150, 30);
panel.add(usernameField);
JLabel passwordLabel = new JLabel("密 码:");
passwordLabel.setFont(new Font("微软雅黑", Font.PLAIN, 16));
passwordLabel.setBounds(80, 120, 80, 30);
panel.add(passwordLabel);
passwordField = new JPasswordField();
passwordField.setBounds(150, 120, 150, 30);
panel.add(passwordField);
JButton loginButton = new JButton("登录");
loginButton.setBounds(100, 180, 80, 30);
loginButton.addActionListener(this);
panel.add(loginButton);
JButton registerButton = new JButton("注册");
registerButton.setBounds(220, 180, 80, 30);
registerButton.addActionListener(this);
panel.add(registerButton);
this.setContentPane(panel);
}
public void actionPerformed(ActionEvent e) {
String command = e.getActionCommand();
if (command.equals("登录")) {
String username = usernameField.getText();
String password = new String(passwordField.getPassword());
if (username.equals("admin") && password.equals("admin")) {
System.out.println("登录成功!");
} else {
System.out.println("用户名或密码错误!");
}
} else if (command.equals("注册")) {
System.out.println("进入注册页面!");
}
}
public static void main(String[] args) {
Login login = new Login();
login.setVisible(true);
}
}
```
阅读全文