目录
java编程题(每日一练):
1.设计一个用户注册的界面,包括用户名、密码输入框,性别单选按钮,表示爱好的 篮球、足球、乒乓球复选框,表示学历的下拉列表框,“注册”按钮和“取消”按钮。
2.设计一个求三角形面积的图形界面程序,要求通过3个输入框输入3个边长,当单 击“计算”按钮时,能够计算以输入的3个边长所形成的三角形的面积。要求能够判断输 入的3个边长是否是数字,三个边长是否能够构成合法的三角形
问题一的答案代码如下:
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class Main {
public static void main(String[] args) {
JFrame frame = new JFrame("User Registration");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(400, 300);
frame.setLayout(new GridLayout(6, 2));
JLabel nameLabel = new JLabel("Username:");
JTextField nameField = new JTextField();
JLabel passwordLabel = new JLabel("Password:");
JPasswordField passwordField = new JPasswordField();
JLabel genderLabel = new JLabel("Gender:");
JRadioButton maleButton = new JRadioButton("Male");
JRadioButton femaleButton = new JRadioButton("Female");
ButtonGroup genderGroup = new ButtonGroup();
genderGroup.add(maleButton);
genderGroup.add(femaleButton);
JPanel genderPanel = new JPanel();
genderPanel.add(maleButton);
genderPanel.add(femaleButton);
JLabel hobbyLabel = new JLabel("Hobbies:");
JCheckBox basketballBox = new JCheckBox("Basketball");
JCheckBox footballBox = new JCheckBox("Football");
JCheckBox pingpongBox = new JCheckBox("Ping Pong");
JPanel hobbyPanel = new JPanel();
hobbyPanel.add(basketballBox);
hobbyPanel.add(footballBox);
hobbyPanel.add(pingpongBox);
JLabel educationLabel = new JLabel("Education:");
String[] educationLevels = {"High School", "Associate", "Bachelor", "Master", "Doctor"};
JComboBox<String> educationComboBox = new JComboBox<>(educationLevels);
JButton registerButton = new JButton("Register");
JButton cancelButton = new JButton("Cancel");
frame.add(nameLabel);
frame.add(nameField);
frame.add(passwordLabel);
frame.add(passwordField);
frame.add(genderLabel);
frame.add(genderPanel);
frame.add(hobbyLabel);
frame.add(hobbyPanel);
frame.add(educationLabel);
frame.add(educationComboBox);
frame.add(registerButton);
frame.add(cancelButton);
registerButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
String username = nameField.getText();
String password = new String(passwordField.getPassword());
String gender = maleButton.isSelected() ? "Male" : "Female";
String hobbies = "";
if (basketballBox.isSelected()) hobbies += "Basketball ";
if (footballBox.isSelected()) hobbies += "Football ";
if (pingpongBox.isSelected()) hobbies += "Ping Pong ";
String education = (String) educationComboBox.getSelectedItem();
JOptionPane.showMessageDialog(frame, "Username: " + username + "\nPassword: " + password + "\nGender: " + gender + "\nHobbies: " + hobbies + "\nEducation: " + education);
}
});
cancelButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
nameField.setText("");
passwordField.setText("");
genderGroup.clearSelection();
basketballBox.setSelected(false);
footballBox.setSelected(false);
pingpongBox.setSelected(false);
educationComboBox.setSelectedIndex(0);
}
});
frame.setVisible(true);
}
}
问题一的运行截图如下:
问题二的答案代码如下:
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class TriangleAreaCalculator {
public static void main(String[] args) {
JFrame frame = new JFrame("Triangle Area Calculator");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(400, 200);
frame.setLayout(new GridLayout(5, 2));
JLabel sideALabel = new JLabel("Side A:");
JTextField sideAField = new JTextField();
JLabel sideBLabel = new JLabel("Side B:");
JTextField sideBField = new JTextField();
JLabel sideCLabel = new JLabel("Side C:");
JTextField sideCField = new JTextField();
JButton calculateButton = new JButton("Calculate");
JLabel resultLabel = new JLabel("Area:");
frame.add(sideALabel);
frame.add(sideAField);
frame.add(sideBLabel);
frame.add(sideBField);
frame.add(sideCLabel);
frame.add(sideCField);
frame.add(calculateButton);
frame.add(resultLabel);
calculateButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
try {
double a = Double.parseDouble(sideAField.getText());
double b = Double.parseDouble(sideBField.getText());
double c = Double.parseDouble(sideCField.getText());
if (isValidTriangle(a, b, c)) {
double s = (a + b + c) / 2;
double area = Math.sqrt(s * (s - a) * (s - b) * (s - c));
resultLabel.setText("Area: " + area);
} else {
resultLabel.setText("Invalid Triangle");
}
} catch (NumberFormatException ex) {
resultLabel.setText("Please enter valid numbers");
}
}
});
frame.setVisible(true);
}
public static boolean isValidTriangle(double a, double b, double c) {
return a + b > c && a + c > b && b + c > a;
}
}
问题二的运行截图如下:
这两个程序分别实现了用户注册和三角形面积计算的图形界面。第一个程序包含用户名、密码输入框,性别单选按钮,爱好复选框,学历下拉列表框,以及注册和取消按钮。第二个程序通过三个输入框输入三角形的三条边长,并在点击“计算”按钮时计算三角形的面积,并判断输入的边长是否合法。