JOptionPane.showInputDialog的属性
时间: 2023-05-29 18:05:18 浏览: 69
JOptionPane.showInputDialog是一个用于显示一个带有输入框的对话框的方法,它有以下属性:
1. message:显示在对话框中的提示信息。
2. title:对话框的标题。
3. messageType:对话框的类型。可以是JOptionPane.PLAIN_MESSAGE、JOptionPane.INFORMATION_MESSAGE、JOptionPane.WARNING_MESSAGE、JOptionPane.ERROR_MESSAGE或JOptionPane.QUESTION_MESSAGE。
4. icon:对话框的图标。
5. options:对话框中的选项。
6. initialValue:输入框中的初始值。
7. parentComponent:对话框的父组件。
其中,message、title、messageType和parentComponent是必需的属性。
相关问题
用Java语言编写程序使用对象输入/输出流完成用户注册登录界面,User类(用户名,密码)保存在工程文件夹下res\password.txt中 。(仿P204-例题10-12),使用消息框给出相关信息(自学JOptionPane的showMessageDialog方法的使用)
好的,我理解了你的问题。下面是Java代码实现:
```java
import java.io.*;
import javax.swing.*;
class User implements Serializable {
private String username;
private String password;
public User(String username, String password) {
this.username = username;
this.password = password;
}
public String getUsername() {
return username;
}
public String getPassword() {
return password;
}
}
public class LoginDemo {
public static void main(String[] args) {
String filePath = "res/password.txt";
File file = new File(filePath);
// 创建文件夹
File folder = new File("res");
if (!folder.exists()) {
folder.mkdirs();
}
try {
// 创建文件
if (!file.exists()) {
file.createNewFile();
}
// 读取用户信息
FileInputStream fileIn = new FileInputStream(file);
ObjectInputStream in = new ObjectInputStream(fileIn);
User user = (User) in.readObject();
in.close();
fileIn.close();
// 登录对话框
String inputUsername = JOptionPane.showInputDialog("请输入用户名:");
String inputPassword = JOptionPane.showInputDialog("请输入密码:");
// 校验用户名密码
if (inputUsername.equals(user.getUsername()) && inputPassword.equals(user.getPassword())) {
JOptionPane.showMessageDialog(null, "登录成功!");
} else {
JOptionPane.showMessageDialog(null, "用户名或密码错误!");
}
} catch (IOException | ClassNotFoundException e) {
e.printStackTrace();
}
// 注册对话框
String username = JOptionPane.showInputDialog("请输入要注册的用户名:");
String password = JOptionPane.showInputDialog("请输入要注册的密码:");
User newUser = new User(username, password);
try {
// 写入用户信息
FileOutputStream fileOut = new FileOutputStream(file);
ObjectOutputStream out = new ObjectOutputStream(fileOut);
out.writeObject(newUser);
out.close();
fileOut.close();
JOptionPane.showMessageDialog(null, "注册成功!");
} catch (IOException e) {
e.printStackTrace();
}
}
}
```
代码中的 `User` 类是一个简单的 JavaBean,包含用户名和密码两个属性。`LoginDemo` 类实现了用户登录和注册的功能。
程序会先读取工程文件夹下的 `res/password.txt` 文件,如果文件不存在则会创建新文件。读取到的 `User` 对象会用于登录校验。
登录校验和注册都使用了 `JOptionPane` 类的 `showInputDialog` 和 `showMessageDialog` 方法。前者用于输入用户名和密码,后者用于展示校验结果和注册成功提示。
注意,需要在类定义前添加 `import java.io.*;` 和 `import javax.swing.*;` 以引入必要的类。
java弹窗表格实现管理员对图书的查询和修改,不用数据库
可以使用Java的Swing组件来实现管理员对图书的查询和修改,并且不用数据库。具体来说,可以使用JTable组件来实现表格的显示和编辑,使用JOptionPane组件来实现弹窗的显示。以下是简单的示例代码:
```
import javax.swing.*;
import javax.swing.table.DefaultTableModel;
import java.awt.*;
import java.util.ArrayList;
public class BookManager {
private ArrayList<Book> books;
public BookManager() {
books = new ArrayList<Book>();
}
public void addBook(Book book) {
books.add(book);
}
public ArrayList<Book> searchBook(String keyword) {
ArrayList<Book> results = new ArrayList<Book>();
for (Book book : books) {
if (book.getTitle().contains(keyword) || book.getAuthor().contains(keyword)) {
results.add(book);
}
}
return results;
}
public void modifyBook(Book book) {
int index = books.indexOf(book);
if (index != -1) {
books.set(index, book);
}
}
public static void main(String[] args) {
BookManager manager = new BookManager();
JFrame frame = new JFrame("图书管理系统");
frame.setSize(800, 600);
frame.setLocationRelativeTo(null);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel panel = new JPanel(new BorderLayout());
JTable table = new JTable(new DefaultTableModel(new Object[] {"书名", "作者", "出版社", "价格"}, 0));
JScrollPane scrollPane = new JScrollPane(table);
panel.add(scrollPane, BorderLayout.CENTER);
JPanel buttonPanel = new JPanel();
JButton addButton = new JButton("添加图书");
addButton.addActionListener(e -> {
String title = JOptionPane.showInputDialog(frame, "请输入书名:");
String author = JOptionPane.showInputDialog(frame, "请输入作者:");
String publisher = JOptionPane.showInputDialog(frame, "请输入出版社:");
double price = Double.parseDouble(JOptionPane.showInputDialog(frame, "请输入价格:"));
Book book = new Book(title, author, publisher, price);
manager.addBook(book);
DefaultTableModel model = (DefaultTableModel) table.getModel();
model.addRow(new Object[] {book.getTitle(), book.getAuthor(), book.getPublisher(), book.getPrice()});
});
buttonPanel.add(addButton);
JButton searchButton = new JButton("查询图书");
searchButton.addActionListener(e -> {
String keyword = JOptionPane.showInputDialog(frame, "请输入关键字:");
ArrayList<Book> results = manager.searchBook(keyword);
if (results.size() == 0) {
JOptionPane.showMessageDialog(frame, "未找到相关图书!");
} else {
DefaultTableModel model = (DefaultTableModel) table.getModel();
model.setRowCount(0);
for (Book book : results) {
model.addRow(new Object[] {book.getTitle(), book.getAuthor(), book.getPublisher(), book.getPrice()});
}
}
});
buttonPanel.add(searchButton);
JButton modifyButton = new JButton("修改图书");
modifyButton.addActionListener(e -> {
int row = table.getSelectedRow();
if (row == -1) {
JOptionPane.showMessageDialog(frame, "请先选择要修改的图书!");
return;
}
String title = JOptionPane.showInputDialog(frame, "请输入书名:", table.getValueAt(row, 0));
String author = JOptionPane.showInputDialog(frame, "请输入作者:", table.getValueAt(row, 1));
String publisher = JOptionPane.showInputDialog(frame, "请输入出版社:", table.getValueAt(row, 2));
double price = Double.parseDouble(JOptionPane.showInputDialog(frame, "请输入价格:", table.getValueAt(row, 3)));
Book book = new Book(title, author, publisher, price);
manager.modifyBook(book);
DefaultTableModel model = (DefaultTableModel) table.getModel();
model.setValueAt(book.getTitle(), row, 0);
model.setValueAt(book.getAuthor(), row, 1);
model.setValueAt(book.getPublisher(), row, 2);
model.setValueAt(book.getPrice(), row, 3);
});
buttonPanel.add(modifyButton);
panel.add(buttonPanel, BorderLayout.SOUTH);
frame.setContentPane(panel);
frame.setVisible(true);
}
}
class Book {
private String title;
private String author;
private String publisher;
private double price;
public Book(String title, String author, String publisher, double price) {
this.title = title;
this.author = author;
this.publisher = publisher;
this.price = price;
}
public String getTitle() {
return title;
}
public String getAuthor() {
return author;
}
public String getPublisher() {
return publisher;
}
public double getPrice() {
return price;
}
public void setPrice(double price) {
this.price = price;
}
public String toString() {
return "书名:" + title + ",作者:" + author + ",出版社:" + publisher + ",价格:" + price;
}
}
```
这个示例代码实现了一个简单的图书管理系统,其中BookManager类负责管理图书信息,包括添加、查询和修改,Book类代表图书实体,包括书名、作者、出版社和价格等属性。在程序运行时,通过Swing组件构建了一个包含表格和按钮的窗口,用户可以通过按钮进行相应的操作。由于图书信息存储在内存中的ArrayList中,所以不需要使用数据库。