package swing; import java.awt.Component; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.event.KeyEvent; import java.io.File; import java.text.SimpleDateFormat; import java.util.ArrayList; import java.util.Calendar; import java.util.Properties; import javax.swing.BorderFactory; import javax.swing.BoxLayout; import javax.swing.JButton; import javax.swing.JComponent; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JPanel; import javax.swing.JTextArea; import javax.swing.JTextField; import javax.swing.SwingConstants; public class ybc extends JPanel implements ActionListener { private static final long serialVersionUID = 1L; JLabel theLabel; JLabel jb = null; JTextField jtf = null; JLabel jb1 = null; JTextField jtf1 = null; DateChooser jdate = null; JTextArea htmlTextArea; Properties props; ArrayList<JPanel> jpanelList = null; public ybc() { setLayout(new BoxLayout(this, BoxLayout.PAGE_AXIS)); JButton changeTheLabel = new JButton(PropertyManager.getInstance().getProperty( "save_button_name")); changeTheLabel.setMnemonic(KeyEvent.VK_C); changeTheLabel.setAlignmentX(Component.LEFT_ALIGNMENT); changeTheLabel.addActionListener(this); JButton queryTheLabel = new JButton(PropertyManager.getInstance().getProperty( "query_button_name")); queryTheLabel.setMnemonic(KeyEvent.VK_C); changeTheLabel.setAlignmentX(Component.LEFT_ALIGNMENT); queryTheLabel.addActionListener(this); JPanel leftPanel = new JPanel(); leftPanel.setLayout(new BoxLayout(leftPanel, BoxLayout.Y_AXIS)); leftPanel.setBorder(BorderFactory.createCompoundBorder(BorderFactory .createTitledBorder(PropertyManager.getInstance().getProperty("border_title")), BorderFactory.createEmptyBorder(10, 10, 10, 10))); jdate = new DateChooser(leftPanel, "yyyy-MM-dd"); /*String tmp = FileTool.getContentWithTime(PropertyManager.getInstance().getProperty( "store_file_name"), new SimpleDateFormat("yyyy-MM-dd").format(Calendar.getInstance() .getTime())); String jtfy = "0"; String hsfy = "0"; if (tmp != null) { String[] contents = tmp.split(","); jtfy = contents[1].split("=")[1]; hsfy = contents[2].split("=")[1]; } jb = new JLabel("交通费"); jtf = new JTextField(jtfy, 5); jtf.setHorizontalAlignment(SwingConstants.RIGHT); jb1 = new JLabel("伙食费"); jtf1 = new JTextField(hsfy, 5); jtf1.setHorizontalAlignment(SwingConstants.RIGHT); JPanel jbPanel = new JPanel(); JPanel jbPanel2 = new JPanel(); jbPanel.add(jb); jbPanel.add(jtf); jbPanel2.add(jb1); jbPanel2.add(jtf1);*/ leftPanel.add(jdate); jpanelList = createPanels(); for (JPanel jpanel : jpanelList) { leftPanel.add(jpanel); } //leftPanel.add(Box.createRigidArea(new Dimension(0, 10))); leftPanel.add(changeTheLabel); leftPanel.add(queryTheLabel); setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10)); add(leftPanel); } // React to the user pushing the Change button. public void actionPerformed(ActionEvent e) { if (PropertyManager.getInstance().getProperty("save_button_name").equals( e.getActionCommand())) { StringBuffer sb = new StringBuffer(); sb.append("时间=").append(jdate.getText()).append(","); for (JPanel jpanel : jpanelList) { sb.append(((JLabel)jpanel.getComponent(0)).getText()).append("=").append( ((JTextField)jpanel.getComponent(1)).getText()).append(","); } if (!FileTool.existThisTimeLine(PropertyManager.getInstance().getProperty( "store_file_name"), jdate.getText())) { FileTool.writeLineByWriter(PropertyManager.getInstance().getProperty( "store_file_name"), sb.toString()); } else { FileTool.readAndReplaceStr(PropertyManager.getInstance().getProperty( "store_file_name"), "e:/temp.txt", jdate.getText(), sb.toString()); } } if (PropertyManager.getInstance().getProperty("query_button_name").equals( e.getActionCommand())) { QueryJTable jTableDefineTest = new QueryJTable(); jTableDefineTest.setVisible(true); /* Object[] options = { "确定", "取消", "帮助" }; JOptionPane.showMessageDialog(null, "在对话框内显示的描述性的文字", "标题条文字串", JOptionPane.ERROR_MESSAGE);*/ /* int response = JOptionPane.showOptionDialog(this, "这是个选项对话框,用户可以选择自己的按钮的个数", "选项对话框标题", JOptionPane.YES_OPTION, JOptionPane.QUESTION_MESSAGE, null, options, options[0]); if (response == 0) { this.setName("0"); } else if (response == 1) { this.setName("1"); } else if (response == 2) { this.setName("2"); } */ /*JOptionPane.showConfirmDialog(null, "choose one", "choose one", JOptionPane.YES_NO_OPTION); */ } } public static ArrayList<JPanel> createPanels() { String tmp = FileTool.getContentWithTime(PropertyManager.getInstance().getProperty( "store_file_name"), new SimpleDateFormat("yyyy-MM-dd").format(Calendar.getInstance() .getTime())); String dataSrc = PropertyManager.getInstance().getProperty("data_name"); ArrayList<JPanel> al = new ArrayList<JPanel>(); if (dataSrc != null) { String[] strs = dataSrc.split(","); for (int i = 0; i < strs.length; i++) { JPanel jPanel = new JPanel(); JLabel jLabel = new JLabel(strs[i]); JTextField jTextField = new JTextField("0", 5); jTextField.setHorizontalAlignment(SwingConstants.RIGHT); jPanel.add(jLabel); jPanel.add(jTextField); al.add(jPanel); } } return al; /* if (tmp != null) { String[] contents = tmp.split(","); jtfy = contents[1].split("=")[1]; hsfy = contents[2].split("=")[1]; } if (tmp != null) { String[] contents = tmp.split(","); jtfy = contents[1].split("=")[1]; hsfy = contents[2].split("=")[1]; }*/ /* jb = new JLabel("交通费"); jtf = new JTextField(jtfy, 5); jtf.setHorizontalAlignment(SwingConstants.RIGHT); jb1 = new JLabel("伙食费"); jtf1 = new JTextField(hsfy, 5); jtf1.setHorizontalAlignment(SwingConstants.RIGHT);*/ /* JPanel jbPanel = new JPanel(); JPanel jbPanel2 = new JPanel(); jbPanel.add(jb); jbPanel.add(jtf); jbPanel2.add(jb1); jbPanel2.add(jtf1);*/ } private static void createAndShowGUI() { JFrame frame = new JFrame(PropertyManager.getInstance().getProperty("frame_title")); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // frame.setSize(new Dimension(400, 300)); frame.setLocation(500, 600); JComponent newContentPane = new ybc(); newContentPane.setOpaque(true); // content panes must be opaque frame.setContentPane(newContentPane); // Display the window. //显示出来最合适的窗口大小 setsize无效 frame.pack(); frame.setVisible(true); } public static void main(String[] args) { // 加载配置文件 File file = new File("properties/sysconfig.properties"); if (file.exists()) { try { PropertyManager.getInstance().initializeProperties(file); } catch (Exception e) { e.printStackTrace(); } } javax.swing.SwingUtilities.invokeLater(new Runnable() { public void run() { createAndShowGUI(); } }); } }
package swing; import java.io.*; import java.util.Properties; public class PropertyManager { private PropertyManager() { props = new Properties(); propsFile = null; } public static PropertyManager getInstance() { return propertyManager; } public String getProperty(String s) { return props.getProperty(s); } public void setProperty(String s, String s1) throws Exception { props.setProperty(s, s1); try { FileOutputStream fileoutputstream = new FileOutputStream(propsFile); props.store(fileoutputstream, ""); fileoutputstream.close(); } catch(IOException ioexception) { throw new Exception(ioexception); } } public void initializeProperties(File file) throws Exception { try { propsFile = file; FileInputStream fileinputstream = new FileInputStream(file); props.load(fileinputstream); fileinputstream.close(); } catch(IOException ioexception) { throw new Exception(ioexception); } } private static PropertyManager propertyManager = new PropertyManager(); private Properties props; private File propsFile; }
// 配置文件,可配置的形式获取资源
properties/sysconfig.properties
store_file_name=E/:/test.txtborder_title=/u95FB/u7231/u6BB7data_name=/u4EA4/u901A/u8D39,/u4F19/u98DF/u8D39,/u6C34/u7535/u8D39time_name=/u65F6/u95F4frame_title=/u6BB7/u7231/u95FBquery_button_name=/u67E5/u8BE2mth_detail_title_name=/u6BCF/u6708/u8BE6/u60C5save_button_name=/u4FDD/u5B58
package swing; import java.awt.BorderLayout; import java.awt.Color; import java.awt.Component; import java.awt.Font; import java.awt.GridLayout; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.event.KeyEvent; import java.text.SimpleDateFormat; import java.util.ArrayList; import java.util.Calendar; import java.util.Date; import java.util.Vector; import javax.swing.DefaultCellEditor; import javax.swing.JButton; import javax.swing.JComboBox; import javax.swing.JFrame; import javax.swing.JPanel; import javax.swing.JScrollPane; import javax.swing.JTable; import javax.swing.JTextField; import javax.swing.ListSelectionModel; import javax.swing.SwingConstants; import javax.swing.table.DefaultTableCellRenderer; import javax.swing.table.DefaultTableModel; import javax.swing.table.JTableHeader; import javax.swing.table.TableCellRenderer; import javax.swing.table.TableModel; public class QueryJTable extends JFrame implements ActionListener { private static final long serialVersionUID = 1L; private DefaultTableModel tableModel; //表格模型对象 private JTable table; JComboBox yearbox; JComboBox mthBox; public QueryJTable() { super(); setTitle(PropertyManager.getInstance().getProperty("mth_detail_title_name")); setBounds(400, 400, 600, 300); //setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //获取当前日期 String yearMth = getCurDateStr(); String curYear = yearMth.substring(0, 4); String curMth = yearMth.substring(4); JPanel ym = new JPanel(); ym.setLayout(new GridLayout(1, 2)); yearbox = new JComboBox(); for (int i = 2010; i < 2050; i++) { yearbox.addItem(i); } yearbox.setSelectedItem(Integer.valueOf(curYear)); mthBox = new JComboBox(); mthBox.addItem("01"); mthBox.addItem("02"); mthBox.addItem("03"); mthBox.addItem("04"); mthBox.addItem("05"); mthBox.addItem("06"); mthBox.addItem("07"); mthBox.addItem("08"); mthBox.addItem("09"); mthBox.addItem("10"); mthBox.addItem("11"); mthBox.addItem("12"); mthBox.setSelectedItem(curMth); ym.add(yearbox); ym.add(mthBox); getContentPane().add(ym, BorderLayout.NORTH); JButton changeTheLabel = new JButton(PropertyManager.getInstance().getProperty( "query_button_name")); changeTheLabel.setMnemonic(KeyEvent.VK_C); changeTheLabel.setAlignmentX(Component.LEFT_ALIGNMENT); changeTheLabel.addActionListener(this); getContentPane().add(changeTheLabel, BorderLayout.SOUTH); JScrollPane scrollPane = new JScrollPane(); //支持滚动 getContentPane().add(scrollPane, BorderLayout.CENTER); // 从配置文件中获取列名 String []dataStr =PropertyManager.getInstance().getProperty("data_name").split(","); String[] columnNames =null; if(null!=dataStr) { columnNames = new String[dataStr.length+1]; columnNames[0]=PropertyManager.getInstance().getProperty("time_name"); for (int i = 1; i < dataStr.length+1; i++) { columnNames[i] = dataStr[i-1]; } } Vector columnNameV = new Vector(); //获得表头 for (int column = 0; column < columnNames.length; column++) { columnNameV.add(columnNames[column]); } Vector tableValueV = new Vector(); for (int row = 1; row < 11; row++) //获得数据 { Vector rowV = new Vector(); for (int column = 0; column < columnNames.length; column++) { rowV.add(columnNames[column] + row); //数据 } tableValueV.add(rowV); } tableModel = new DefaultTableModel(tableValueV, columnNameV); table = new MyTable(tableModel); table.setDefaultEditor(Object.class, new MyEditor()); //table = new MyTable(tableValueV, columnNameV); //自定义的表格 //设置列宽 table.getColumnModel().getColumn(0).setMinWidth(200); table.getColumnModel().getColumn(1).setMinWidth(200); table.getColumnModel().getColumn(2).setMinWidth(200); DefaultTableCellRenderer cellrender = new DefaultTableCellRenderer(); cellrender.setHorizontalAlignment(JTextField.CENTER); table.getColumnModel().getColumn(0).setCellRenderer(cellrender); table.setAutoResizeMode(JTable.AUTO_RESIZE_OFF); //关闭表格列的自动调整功能。 table.setSelectionMode(ListSelectionModel.SINGLE_SELECTION); //单选 table.setSelectionBackground(Color.YELLOW); table.setSelectionForeground(Color.RED); table.setRowHeight(20); scrollPane.setViewportView(table); //支持滚动 if (tableModel.getRowCount() > 0) { for (int row = tableModel.getRowCount() - 1; row >= 0; row--) { tableModel.removeRow(row); } } String yearm = yearbox.getSelectedItem().toString() + "-" + mthBox.getSelectedItem().toString(); ArrayList al = FileTool.readFileContent(PropertyManager.getInstance().getProperty( "store_file_name"), yearm); for (int i = 0; i < al.size(); i++) { ArrayList obj = (ArrayList)al.get(i); tableModel.addRow(obj.toArray()); } } /** * 获取当前年月的字符串 */ public static String getCurDateStr() { Date date = Calendar.getInstance().getTime(); SimpleDateFormat sdf = new SimpleDateFormat("yyyyMM"); return sdf.format(date); } /** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub QueryJTable jTableDefineTest = new QueryJTable(); jTableDefineTest.setVisible(true); } public void actionPerformed(ActionEvent e) { if (tableModel.getRowCount() > 0) { for (int row = tableModel.getRowCount() - 1; row >= 0; row--) { tableModel.removeRow(row); } } String ym = yearbox.getSelectedItem().toString() + "-" + mthBox.getSelectedItem().toString(); ArrayList al = FileTool.readFileContent("e:/test.txt", ym); for (int i = 0; i < al.size(); i++) { ArrayList obj = (ArrayList)al.get(i); tableModel.addRow(obj.toArray()); } } } class MyEditor extends DefaultCellEditor { private static final long serialVersionUID = 1781979580278898929L; public MyEditor() { super(new JTextField()); } public Component getTableCellEditorComponent(JTable table, Object value, boolean isSelected, int row, int column) { //获得默认表格单元格控件 JTextField editor = (JTextField)super.getTableCellEditorComponent(table, value, isSelected, row, column); if (value != null) editor.setText(value.toString()); if (column == 0) { //设置对齐方式 editor.setHorizontalAlignment(SwingConstants.CENTER); editor.setFont(new Font("Serif", Font.BOLD, 14)); } else { editor.setHorizontalAlignment(SwingConstants.RIGHT); editor.setFont(new Font("Serif", Font.ITALIC, 12)); } return editor; } } class MyTable extends JTable { private static final long serialVersionUID = 1L; public MyTable(Vector rowData, Vector columnNames) { super(rowData, columnNames); } public MyTable(TableModel dm) { super(dm); } public JTableHeader getTableHeader() { JTableHeader tableHeader = super.getTableHeader(); tableHeader.setReorderingAllowed(false); //设置表格列不可重排 DefaultTableCellRenderer hr = (DefaultTableCellRenderer)tableHeader.getDefaultRenderer(); //获得表格头的单元格对象 hr.setHorizontalAlignment(DefaultTableCellRenderer.CENTER); //列名居中 return tableHeader; } public TableCellRenderer getDefaultRenderer(Class<?> columnClass) { DefaultTableCellRenderer cr = (DefaultTableCellRenderer)super .getDefaultRenderer(columnClass); cr.setHorizontalAlignment(DefaultTableCellRenderer.RIGHT); //单元格内容居中 return cr; } public boolean isCellEditable(int row, int column) { return false; //单元格不可修改 } }