java小程序之(GUI)确认对话框JOptionPane.showConfirmDialog

本文介绍了一个简单的Java程序,用于通过一系列选择题来猜测用户的生日日期。该程序利用了Swing库来显示对话框并获取用户的反馈,通过用户确认其生日是否在给定的数字集合中来逐步缩小范围。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

猜生日,示例程序:

//import javax.swing.JOptionPane;
import javax.swing.*;
public class Demo032401{
    public static void main( String [] args ){
        String s1 = 
                            " 1   3   5    7\n " + 
                            " 9 11 13 15 \n " +
                            " 17 19 21 23\n " +
                            " 25 27 29 31\n ";
        String s2 = 
                            " 2 3 6 7\n " +
                            " 10 11 14 15\n " +
                            " 18 19 22 23\n " +
                            " 26 27 30 31\n ";
        String s3 = 
                            "4 5 6 7\n " +
                            " 12 13 14 15\n " +
                            " 20 21 22 23\n " +
                            " 28 29 30 31\n ";
        String s4 = 
                            " 8 9 10 11\n " +
                            " 12 13 14 15\n " +
                            " 24 25 26 27\n " +
                            " 28 29 30 31\n ";
        String s5 =
                            " 16 17 18 19\n " +
                            " 20 21 22 23\n " +
                            " 24 25 26 27\n " +
                            " 28 29 30 31\n ";
        int day = 0;
        int answer = JOptionPane.showConfirmDialog( null, " Is your birthday in these numbers?\n " + s1 );
        if ( answer == JOptionPane.YES_OPTION )
            day += 1;

        answer = JOptionPane.showConfirmDialog( null, " Is your birthday in these numbers?\n " + s2 );
        if ( answer == JOptionPane.YES_OPTION )
            day += 2;

        answer = JOptionPane.showConfirmDialog( null, " Is your birthday in these numbers?\n " + s3 );
        if ( answer == JOptionPane.YES_OPTION )
            day += 4;

        answer = JOptionPane.showConfirmDialog( null, " Is your birthday in these numbers?\n " + s4 );
        if ( answer == JOptionPane.YES_OPTION )
            day += 8;

        answer = JOptionPane.showConfirmDialog( null, " Is your birthday in these numbers?\n " + s5 );
        if ( answer == JOptionPane.YES_OPTION )
            day += 16;

        //show your birthday number
        JOptionPane.showMessageDialog( null, " Your birthday number is " + day + " ! " );
        System.out.println( "RUN OVER!!!" );
    }
}
代码分析package view; import model.Book; import model.BookDAO; import javax.swing.*; import javax.swing.table.DefaultTableModel; import java.awt.*; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.util.List; import static model.BookDAO.updateBook; public class BookView extends JFrame { private BookDAO bookDAO; private JTable bookTable; private DefaultTableModel tableModel; public BookView() { bookDAO = new BookDAO(); initializeUI(); } private void initializeUI() { setTitle("图书管理系统"); setSize(800, 600); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setLocationRelativeTo(null); // 创建按钮面板 JPanel buttonPanel = new JPanel(); JButton addButton = new JButton("添加书籍"); JButton viewButton = new JButton("查看所有书籍"); JButton updateButton = new JButton("更新书籍"); JButton deleteButton = new JButton("删除书籍"); addButton.setBackground(new Color(17, 248, 160)); viewButton.setBackground(new Color(17, 248, 160)); updateButton.setBackground(new Color(17, 248, 160)); deleteButton.setBackground(new Color(17, 248, 160)); buttonPanel.add(addButton); buttonPanel.add(viewButton); buttonPanel.add(updateButton); buttonPanel.add(deleteButton); buttonPanel.setLayout(new GridLayout(2, 2, 1, 10)); // 创建表格 bookTable = new JTable(); tableModel = new DefaultTableModel(new Object[]{"ID", "书名", "作者", "余量", "出版社"}, 0); bookTable.setModel(tableModel); bookTable.setRowHeight(40); bookTable.setBackground(Color.cyan); JScrollPane scrollPane = new JScrollPane(bookTable); // 添加组件到主面板 setLayout(new BorderLayout()); add(buttonPanel, BorderLayout.SOUTH); add(scrollPane, BorderLayout.CENTER); // 添加按钮事件监听器 addButton.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { addBook(); } }); viewButton.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { viewAllBook(); } }); updateButton.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { updateBook(); } }); deleteButton.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { deleteBook(); } }); setVisible(true); } private void addBook() { JTextField idField = new JTextField(); JTextField nameField = new JTextField(); JTextField authorField = new JTextField(); JTextField numField = new JTextField(); JTextField pressField = new JTextField(); Object[] message = { "ID:", idField, "书名:", nameField, "作者:", authorField, "余量:", numField, "出版社:", pressField }; int option = JOptionPane.showConfirmDialog(this, message, "添加书籍", JOptionPane.OK_CANCEL_OPTION); if (option == JOptionPane.OK_OPTION) { int id = Integer.parseInt(idField.getText()); String name = nameField.getText(); String author = authorField.getText(); int num = Integer.parseInt(numField.getText()); String press = pressField.getText(); Book book = new Book(id, name, author, num, press); bookDAO.addBook(book); JOptionPane.showMessageDialog(this, "书籍已添加: " + book); viewAllBook(); // 更新表格数据 } } private void viewAllBook() { List<Book> books = bookDAO.getAllBook(); tableModel.setRowCount(0); // 清空表格 for (Book book : books) { tableModel.addRow(new Object[]{book.getBookId(), book.getBookName(), book.getBookAuthor(), book.getBookNum(), book.getBookPress()}); } } private void updateBook() { int selectedRow = bookTable.getSelectedRow(); if (selectedRow == -1) { JOptionPane.showMessageDialog(this, "请选择要更新的书籍。"); return; } int currentId = (int) tableModel.getValueAt(selectedRow, 0); JTextField idField = new JTextField(String.valueOf(currentId)); JTextField nameField = new JTextField((String) tableModel.getValueAt(selectedRow, 1)); JTextField authorField = new JTextField(String.valueOf(tableModel.getValueAt(selectedRow, 2))); JTextField numField = new JTextField(String.valueOf(tableModel.getValueAt(selectedRow, 3))); JTextField pressField = new JTextField(String.valueOf(tableModel.getValueAt(selectedRow, 4))); Object[] message = { "ID:", idField, "书名:", nameField, "作者:", authorField, "余量", numField, "出版社", pressField }; int option = JOptionPane.showConfirmDialog(this, message, "更新书籍", JOptionPane.OK_CANCEL_OPTION); if (option == JOptionPane.OK_OPTION) { int id = Integer.parseInt(idField.getText()); String name = nameField.getText(); String author = nameField.getText(); int num = Integer.parseInt(idField.getText()); String press = nameField.getText(); Book book = bookDAO.getBookById(currentId); if (book != null) { book.setBookId(id); book.setBookName(name); book.setBookAuthor(author); book.setBookNum(num); book.setBookPress(press); bookDAO.updateBook(book); JOptionPane.showMessageDialog(this, "书籍信息已更新: " + book); viewAllBook(); // 更新表格数据 } else { JOptionPane.showMessageDialog(this, "书籍ID不存在。"); } } } private void deleteBook() { int selectedRow = bookTable.getSelectedRow(); if (selectedRow == -1) { JOptionPane.showMessageDialog(this, "请选择要删除的书籍。"); return; } int id = (int) tableModel.getValueAt(selectedRow, 0); bookDAO.deleteBook(id); JOptionPane.showMessageDialog(this, "书籍已删除,ID: " + id); viewAllBook(); // 更新表格数据 } public static void main(String[] args) { SwingUtilities.invokeLater(new Runnable() { @Override public void run() { new BookView(); } }); } }
最新发布
07-04
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值