标签:io ar os 使用 java sp for 数据 on
1.就地实例化,表格,表格滚动条,有滚动条自动出现抬头,表格model 2.新增窗口,上下调节的输入框,这种输入框中的数据获取方法,输入框部件设置,重置输入框内容
注意与主窗口通用一个BookManager……刚开始就因为在此窗口new了一个manager弄死加不进去……还不会报错…………
1 的代码先是主窗口 可以显示所有藏书 按钮2个 新增和删除 只做好了新增的
package com.lovo.ui; import java.awt.BorderLayout; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.util.List; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JPanel; import javax.swing.JScrollPane; import javax.swing.JTable; import javax.swing.UIManager; import javax.swing.UnsupportedLookAndFeelException; import javax.swing.table.DefaultTableModel; import com.lovo.BookManager.Book; import com.lovo.BookManager.BookManager; /** * 租赁图书查看界面 * @author Abe */ @SuppressWarnings("serial") public class MyTable extends JFrame { private BookManager manager = new BookManager(); private JTable bookTabel; private String[] columnNames = { "编号", "书名", "价格", "状态", "借出日期", "借出次数" }; private JPanel myPanel = new JPanel(); private JButton newButton; private JButton resectButton; /** * 构造器,初始化 */ public MyTable() { this.setSize(600, 400); this.setResizable(false); this.setDefaultCloseOperation(EXIT_ON_CLOSE); this.setLocationRelativeTo(null); initComponents(); } /** * 方法:获得该类使用的manager * @return */ public BookManager getBookManager(){ return manager; } /** * 方法:获得manager中说有书的属性,保存在Object[][]中 */ public void refreshTableModel(){ if (bookTabel != null) { List<Book> list = manager.findAll(); Object[][] date = new Object[list.size()][columnNames.length]; for (int i = 0; i < list.size(); i++) { Book temp = list.get(i); date[i][0] = temp.getIsbn(); date[i][1] = temp.getName(); date[i][2] = temp.getPrice(); date[i][3] = temp.isLended() ? "借出" : "未借出"; date[i][4] = temp.getLendDate(); date[i][5] = temp.getCounter(); } // 创造一个匿名内部类 就地实例化里面的方法 来设置表格的模型 并将数据放入表格 bookTabel.setModel(new DefaultTableModel(date, columnNames) { @Override public boolean isCellEditable(int row, int colum) { return false; } }); } } public void initComponents() { bookTabel = new JTable();// JTable 表格 bookTabel.getTableHeader().setReorderingAllowed(false);// 设置表头不允许重新排列 refreshTableModel(); JScrollPane jsp = new JScrollPane(bookTabel); //不是新增一个这货,直接在new里添加要修饰的表格 //将表格加上滚动条 保存在jsp this.add(jsp, BorderLayout.CENTER); //将jsp放入 窗口中间区域 newButton = new JButton("新增"); newButton.addActionListener(new ActionListener() { //就地实例化的监听器 @Override public void actionPerformed(ActionEvent e) { if(e.getSource() == newButton){ new AddBookDialog(MyTable.this).setVisible(true); //把自己传给新增窗口,新增窗口设置成可见 } } }); resectButton = new JButton("删除"); myPanel.add(newButton); myPanel.add(resectButton); this.add(myPanel, BorderLayout.SOUTH); } //main方法 主窗口可见 设置UI为和现在的系统一样 throws为 系统自动生成…… public static void main(String[] args) throws ClassNotFoundException, InstantiationException, IllegalAccessException, UnsupportedLookAndFeelException { UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName()); new MyTable().setVisible(true); } }
package com.lovo.ui; import java.awt.Component; import java.awt.Font; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.JButton; import javax.swing.JDialog; import javax.swing.JLabel; import javax.swing.JSpinner; import javax.swing.JTextField; import javax.swing.SpinnerModel; import javax.swing.SpinnerNumberModel; import javax.swing.UIManager; import com.lovo.BookManager.Book; /** * 类:新增窗口 * @author Abe */ @SuppressWarnings("serial") public class AddBookDialog extends JDialog { //用JDialog不用JFrame 可以设置Modal固定焦点在此窗口 private JLabel[] hint = new JLabel[3]; private static String[] str = {"编号","书名","日租金"}; private JTextField newIsdn; private JTextField newName; private JSpinner newPrice; private JButton addButton; private JButton resetButton; private MyTable table; /** * 构造器 * @param table */ public AddBookDialog(MyTable table){ this.table = table; this.setModal(true); this.setSize(400,400); this.setResizable(false); this.setLocationRelativeTo(null); this.setLayout(null); this.setDefaultCloseOperation(DISPOSE_ON_CLOSE); inisComponents(); } /** * 方法:初始化构件 */ private void inisComponents() { for(int i = 0 ; i < hint.length ; i++){ hint[i] = new JLabel(str[i]); hint[i].setBounds(50 , 100 + 60 * i , 80 , 50); this.add(hint[i]); } newIsdn = new JTextField(); newIsdn.setBounds(130, 110, 200, 30); this.add(newIsdn); newName = new JTextField(); newName.setBounds(130, 170, 200, 30); this.add(newName); SpinnerModel sModel = new SpinnerNumberModel(1.0, 0.1, 5.0, 0.1); newPrice = new JSpinner(sModel); newPrice.setBounds(130, 230, 200,30); this.add(newPrice); addButton = new JButton("新增"); addButton.setBounds(100, 300, 70, 30); addButton.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { Object obj = e.getSource(); if(obj == addButton){ //价格调整标签 String isdn = newIsdn.getText(); String name = newName.getText(); String pricestr = String.format("%.1f", newPrice.getModel().getValue()); double price =Double.parseDouble(pricestr); table.getBookManager().add(new Book(isdn,name,price)); table.refreshTableModel(); AddBookDialog.this.dispose(); } } }); this.add(addButton); resetButton = new JButton("重置"); resetButton.setBounds(230, 300, 70, 30); resetButton.addActionListener(new ActionListener() { //就地实例化 按键触发事件 重置 @Override public void actionPerformed(ActionEvent e) { Object obj = e.getSource(); if(obj == resetButton){ for(Component c : getContentPane().getComponents()) { //最后放工具包 com.lovo.MyUtil.resetComponent(c); //这里调用了重置工具 } } } }); this.add(resetButton); //统一设置格式 容器内还有容器的,不会改变字体 Font font = new Font("微软雅黑", Font.PLAIN, 14); for (Component c : this.getContentPane().getComponents()) { c.setFont(font); } } public static void main(String[] args) throws Exception{ UIManager.setInstalledLookAndFeels(UIManager.getInstalledLookAndFeels()); new AddBookDialog(null).setVisible(true); } }
package com.lovo; import java.awt.Component; import javax.swing.JCheckBox; import javax.swing.JComboBox; import javax.swing.JPasswordField; import javax.swing.JSpinner; import javax.swing.JTextField; /** * WOOO计划包 * @author Abe */ public class MyUtil { private MyUtil(){ } /** * 方法:自动匹配类型初始化 输入框 * @param c */ public static void resetComponent (Component c){ if(c instanceof JTextField){ //文字框 ((JTextField) c).setText(""); }else if(c instanceof JPasswordField){ //密码框 ((JPasswordField) c).setText(""); }else if(c instanceof JCheckBox){ //复选框 ((JCheckBox) c).setSelected(false); }else if(c instanceof JComboBox<?>){ //下拉框 ((JComboBox<?>) c).setSelectedIndex(0); }else if(c instanceof JSpinner){ //不知道什么框,有上下调整箭头的 ((JSpinner)c).setValue(0); } } }
JAVA程序设计(14.3)----- 图书馆管理系统 初步设计 界面篇~借书目录查看窗口,新书添加窗口
标签:io ar os 使用 java sp for 数据 on
原文地址:http://blog.csdn.net/anubies/article/details/41022399