JOptionPane需要的接口名字
时间: 2024-01-04 08:02:27 浏览: 74
JOptionPane是Java Swing库中的一个类,它提供了一些对话框的方法,用于与用户进行交互。如果你想在Java程序中使用JOptionPane,需要导入javax.swing包。在代码中,需要使用JOptionPane类的静态方法来创建对话框,例如:
```java
import javax.swing.JOptionPane;
// 创建一个简单的信息框
JOptionPane.showMessageDialog(null, "Hello, world!");
// 创建一个带有OK和取消按钮的确认框
int result = JOptionPane.showConfirmDialog(null, "Are you sure?");
if (result == JOptionPane.OK_OPTION) {
// 用户点击了OK按钮
} else {
// 用户点击了取消按钮或关闭了对话框
}
// 创建一个带有文本框的输入框
String input = JOptionPane.showInputDialog(null, "Please enter your name:");
if (input != null) {
// 用户输入了文本并点击了OK按钮
} else {
// 用户点击了取消按钮或关闭了对话框
}
```
这里的接口名字指的是Java Swing库中用于显示对话框的接口,它并不是JOptionPane所需要的接口名字。
相关问题
用Java编写以下题目 1.编写一个算数测试小软件,用来训练小学生的算数能力。程序由3个类组成,其中Teacher对象充当监视器,负责给出算术题目,并判断回答者的答案是否正确;ComputerFrame对象负责为算术题目提供视图,比如用户可以通过ComputerFrame对象提供的GUI界面看到题目,并通过该GUI界面给出题目的答案;MailClass是软件的主类。 2.编写一个Book类,该类至少有price成员变量。该类要实现Comparable接口,在接口的compartTo()方法中规定Book类的两个实例的大小关系为两者的price成员的大小关系。 编写一个主类SortSearchMainClass,在main()方法中将Book类的若干个对象存放在一个链表中,然后用Book类创建一个新的对象,并检查这个对象和链表中的哪些对象相等。 3.使用某种数据库管理系统,建立一个名字为Book的数据库,在数据库中建立bookList表,该表的字段为: ISBN(varchar),name(varchar),price(float),chubanDate(date) 其中ISBN为主键。 编程实现在bookList表中随机查询10条记录,并计算出这10条记录price字段值的平均值,即计算平均价格。
1. 算数测试小软件
Teacher类:
```
public class Teacher {
private int num1;
private int num2;
private int answer;
public void setQuestion() {
num1 = (int) (Math.random() * 100);
num2 = (int) (Math.random() * 100);
answer = num1 + num2;
}
public boolean checkAnswer(int userAnswer) {
return userAnswer == answer;
}
}
```
ComputerFrame类:
```
import javax.swing.*;
import java.awt.*;
public class ComputerFrame extends JFrame {
private JLabel questionLabel;
private JTextField answerField;
private JButton submitButton;
private Teacher teacher;
public ComputerFrame(Teacher teacher) {
this.teacher = teacher;
questionLabel = new JLabel();
answerField = new JTextField();
submitButton = new JButton("提交");
Container container = getContentPane();
container.setLayout(new FlowLayout());
container.add(questionLabel);
container.add(answerField);
container.add(submitButton);
setSize(300, 100);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
setQuestion();
submitButton.addActionListener(event -> checkAnswer());
}
private void setQuestion() {
teacher.setQuestion();
questionLabel.setText(teacher.getNum1() + " + " + teacher.getNum2() + " = ");
}
private void checkAnswer() {
int userAnswer = Integer.parseInt(answerField.getText());
if (teacher.checkAnswer(userAnswer)) {
JOptionPane.showMessageDialog(this, "回答正确!", "提示", JOptionPane.INFORMATION_MESSAGE);
} else {
JOptionPane.showMessageDialog(this, "回答错误!", "提示", JOptionPane.ERROR_MESSAGE);
}
setQuestion();
answerField.setText("");
}
}
```
MailClass类:
```
public class MainClass {
public static void main(String[] args) {
Teacher teacher = new Teacher();
ComputerFrame computerFrame = new ComputerFrame(teacher);
}
}
```
2. Book类
```
public class Book implements Comparable<Book> {
private double price;
public Book(double price) {
this.price = price;
}
public double getPrice() {
return price;
}
public void setPrice(double price) {
this.price = price;
}
@Override
public int compareTo(Book book) {
if (this.price > book.price) {
return 1;
} else if (this.price < book.price) {
return -1;
} else {
return 0;
}
}
@Override
public boolean equals(Object obj) {
if (obj instanceof Book) {
Book book = (Book) obj;
return this.price == book.price;
} else {
return false;
}
}
}
```
SortSearchMainClass类:
```
import java.util.LinkedList;
import java.util.ListIterator;
public class SortSearchMainClass {
public static void main(String[] args) {
LinkedList<Book> bookList = new LinkedList<>();
bookList.add(new Book(20));
bookList.add(new Book(30));
bookList.add(new Book(10));
Book newBook = new Book(10);
boolean isEqual = false;
ListIterator<Book> iterator = bookList.listIterator();
while (iterator.hasNext()) {
Book book = iterator.next();
if (book.equals(newBook)) {
isEqual = true;
break;
}
}
if (isEqual) {
System.out.println("链表中存在和新书价格相等的书。");
} else {
System.out.println("链表中不存在和新书价格相等的书。");
}
bookList.sort(null);
System.out.println("排序后的链表:");
for (Book book : bookList) {
System.out.println(book.getPrice());
}
}
}
```
3. 数据库管理系统
连接数据库:
```
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
public class DatabaseUtil {
private static final String JDBC_DRIVER = "com.mysql.cj.jdbc.Driver";
private static final String DB_URL = "jdbc:mysql://localhost:3306/Book?useSSL=false&allowPublicKeyRetrieval=true&serverTimezone=UTC";
private static final String USER = "root";
private static final String PASS = "123456";
public static Connection getConnection() {
Connection conn = null;
try {
Class.forName(JDBC_DRIVER);
conn = DriverManager.getConnection(DB_URL, USER, PASS);
} catch (SQLException | ClassNotFoundException e) {
e.printStackTrace();
}
return conn;
}
}
```
查询并计算平均价格:
```
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
public class QueryPrice {
public static void main(String[] args) {
String sql = "SELECT price FROM bookList ORDER BY RAND() LIMIT 10";
try (Connection conn = DatabaseUtil.getConnection();
PreparedStatement pstmt = conn.prepareStatement(sql);
ResultSet rs = pstmt.executeQuery()) {
double sum = 0;
int count = 0;
while (rs.next()) {
double price = rs.getDouble("price");
sum += price;
count++;
}
double average = sum / count;
System.out.println("10条记录的平均价格为:" + average);
} catch (SQLException e) {
e.printStackTrace();
}
}
}
```
实验目标 掌握Java Swing图形用户界面编程 实验步骤 1. 实现页面菜单,菜单项包括按名字读取、按学号读取、按班级读取; 2. 实现搜索框,用于输入名字或学号,根据名字或学号查询学生信息; 3. 实现班级下拉列表,用于选择班级并按班级查询学生信息。 4. 实现学生信息展示功能,展示从数据库中查询到的学生信息
;
以下是一个简单的Java Swing图形用户界面编程示例,实现了以上的四个功能:
```java
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.sql.*;
public class StudentInfoGUI extends JFrame implements ActionListener {
private static final String DB_URL = "jdbc:mysql://localhost:3306/student_db";
private static final String DB_USER = "root";
private static final String DB_PASSWORD = "root";
private static final String[] CLASS_OPTIONS = {"Class 1", "Class 2", "Class 3"};
private JLabel nameLabel, idLabel, classLabel;
private JTextField nameField, idField;
private JComboBox<String> classComboBox;
private JButton searchButton;
private JTextArea resultArea;
public StudentInfoGUI() {
super("Student Info");
// 创建菜单栏
JMenuBar menuBar = new JMenuBar();
JMenu fileMenu = new JMenu("File");
JMenuItem nameItem = new JMenuItem("Search by Name");
JMenuItem idItem = new JMenuItem("Search by ID");
JMenuItem classItem = new JMenuItem("Search by Class");
nameItem.addActionListener(this);
idItem.addActionListener(this);
classItem.addActionListener(this);
fileMenu.add(nameItem);
fileMenu.add(idItem);
fileMenu.add(classItem);
menuBar.add(fileMenu);
setJMenuBar(menuBar);
// 创建搜索面板
JPanel searchPanel = new JPanel(new FlowLayout(FlowLayout.LEFT));
nameLabel = new JLabel("Name:");
idLabel = new JLabel("ID:");
nameField = new JTextField(10);
idField = new JTextField(10);
searchButton = new JButton("Search");
searchButton.addActionListener(this);
searchPanel.add(nameLabel);
searchPanel.add(nameField);
searchPanel.add(idLabel);
searchPanel.add(idField);
searchPanel.add(searchButton);
// 创建班级下拉列表
classLabel = new JLabel("Class:");
classComboBox = new JComboBox<>(CLASS_OPTIONS);
classComboBox.addActionListener(this);
// 创建结果文本域
resultArea = new JTextArea(20, 50);
resultArea.setEditable(false);
// 将组件添加到容器中
Container contentPane = getContentPane();
contentPane.setLayout(new BorderLayout());
contentPane.add(searchPanel, BorderLayout.NORTH);
JPanel classPanel = new JPanel();
classPanel.add(classLabel);
classPanel.add(classComboBox);
contentPane.add(classPanel, BorderLayout.WEST);
contentPane.add(new JScrollPane(resultArea), BorderLayout.CENTER);
setSize(600, 400);
setLocationRelativeTo(null);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
@Override
public void actionPerformed(ActionEvent e) {
if (e.getSource() == searchButton) {
String name = nameField.getText().trim();
String id = idField.getText().trim();
String className = (String) classComboBox.getSelectedItem();
search(name, id, className);
} else if (e.getActionCommand().equals("Search by Name")) {
nameField.requestFocus();
} else if (e.getActionCommand().equals("Search by ID")) {
idField.requestFocus();
} else if (e.getActionCommand().equals("Search by Class")) {
classComboBox.requestFocus();
}
}
private void search(String name, String id, String className) {
resultArea.setText("");
String sql = "SELECT * FROM students WHERE name LIKE ? AND id LIKE ? AND class LIKE ?";
try (Connection conn = DriverManager.getConnection(DB_URL, DB_USER, DB_PASSWORD);
PreparedStatement stmt = conn.prepareStatement(sql)) {
stmt.setString(1, "%" + name + "%");
stmt.setString(2, "%" + id + "%");
stmt.setString(3, "%" + className + "%");
ResultSet rs = stmt.executeQuery();
while (rs.next()) {
String result = String.format("Name: %s\nID: %s\nClass: %s\nScore: %d\n\n",
rs.getString("name"), rs.getString("id"), rs.getString("class"), rs.getInt("score"));
resultArea.append(result);
}
if (resultArea.getText().isEmpty()) {
resultArea.setText("No records found.");
}
} catch (SQLException ex) {
ex.printStackTrace();
JOptionPane.showMessageDialog(this, "Error: " + ex.getMessage(), "Error", JOptionPane.ERROR_MESSAGE);
}
}
public static void main(String[] args) {
StudentInfoGUI gui = new StudentInfoGUI();
gui.setVisible(true);
}
}
```
在本示例中,我们使用了Java Swing的各种组件,如JLabel、JTextField、JComboBox、JButton、JTextArea等,同时还实现了ActionListener接口以响应用户事件。在搜索函数中,我们使用了Java JDBC连接MySQL数据库,并使用PreparedStatement执行带参数的SQL查询语句,以避免SQL注入攻击。
阅读全文