以下のツールを使ってファイルを保存しているのですが、同じファイル名があると
自動的に上書き保存されてしまいます。同じファイル名があるときに上書き保存せずにファイル名の後ろに(1),(2)といった風に枝番を自動的につけるようには
どうすればようでしょうか?実際に追加するソースコード等も教えていただけると
助かります。
※回答者様のソースを記入したら「ローカル変数mは使用されていません」
と出たのでその解決策も教えていただけると助かります。
lang
1 2 3 4 5public class MainInit implements ActionListener , WindowListener { //impriments(組み込む) 6 private JFrame mainFrame; 7 private Container contentPane; 8 private JPanel itemPanel; 9 private JPanel textPanel; 10 private JPanel buttonPanel; 11 private JTextField textField; 12 private JButton dirButton; 13 private JButton addButton; 14 private ScreenShot screenShot; 15 private String folderpath; 16 private JLabel folderlabel; 17 18 private SettingWindow settingwindow; 19 20 private ItemBox itemboxtest1; 21 private ItemBox itemboxtest2; 22 private ItemBox itemboxtest3; 23 private ItemBox itemboxtest4; 24 25 //private Thumbnail DrawPanel; 26 27 28 29 30 public MainInit(){ 31 mainFrame = new JFrame("エビデンスキャプチャ"); 32 mainFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //終了方法 33 mainFrame.setSize(500, 300); //ウィンドウのサイズ 34 mainFrame.setLocationRelativeTo(null); //ウィンドウを画面の中央に表示 35 contentPane = mainFrame.getContentPane(); //要素、成分 メインフレームのパネルを取得 36 37 38 contentPane.setLayout(null); 39 40 41 dirButton = new JButton("Dir"); //Dirボタンの作成 クラス名( ) = コンストラクタ 42 dirButton.setBounds(20, 0, 80, 30); 43 contentPane.add(dirButton); //コンパネにボタンを追加 44 45 addButton = new JButton("Capture"); 46 addButton.setBounds(400, 0, 90, 60); 47 contentPane.add(addButton); 48 49 50 51 52 53 JMenuBar menubar = new JMenuBar(); 54 JMenu menu1 = new JMenu("設定"); //メニューの生成 55 JMenu menu2 = new JMenu("ヘルプ"); 56 JMenuItem menuitem1 = new JMenuItem("設定"); 57 58 menubar.add(menu1); 59 menubar.add(menu2); 60 menu1.add(menuitem1); 61 62 mainFrame.getRootPane().setJMenuBar(menubar); //メインフレームに対してメニューバーを追加 63 64 folderlabel = new JLabel(); 65 textField = new JTextField(); //オブジェクトの作成 66 //dirButton = new JButton("Dir"); //Dirボタンの作成 67 //addButton = new JButton("Capture"); //Captureボタンの作成 68 69 70 //「追加」ボタンとアクション・リスナーの関連付け 71 dirButton.addActionListener(this); 72 dirButton.setActionCommand("Dir"); // イベントに対するコマンド文字列登録 73 addButton.addActionListener(this); 74 addButton.setActionCommand("Capture"); // イベントに対するコマンド文字列登録 75 76 77 menuitem1.addActionListener(this); 78 menuitem1.setActionCommand("settingwindow"); // イベントに対するコマンド文字列登録 79 80 81 itemPanel = new JPanel(); //上下 82 itemPanel.setBounds(0, 0, 500, 100); 83 contentPane.add(itemPanel); 84 //itemPanel.setBackground(Color.RED); 85 86 textPanel = new JPanel(); //directryfield dir add 87 textPanel.setLayout(null); 88 contentPane.add(textPanel); 89 textPanel.setBounds(0,80, 500, 100); 90 //textPanel.setBackground(Color.GREEN); 91 92 buttonPanel = new JPanel(); 93 94 buttonPanel.setLayout(null); 95 contentPane.add(buttonPanel); 96 buttonPanel.setBounds(0,180,500,200); 97 //buttonPanel.setBackground(Color.ORANGE); 98 99 100 itemboxtest1 = new ItemBox(); //□↑↓ 101 itemPanel.add(itemboxtest1); 102 103 104 itemboxtest2 = new ItemBox(); 105 itemPanel.add(itemboxtest2); 106 107 itemboxtest3 = new ItemBox(); 108 itemPanel.add(itemboxtest3); 109 110 itemboxtest4 = new ItemBox(); 111 itemPanel.add(itemboxtest4); 112 113 114 //DrawPanel = new Thumbnail(); 115 //DrawPanel.add(addButton); 116 117 118 119 textPanel.add(folderlabel); 120 folderlabel.setBounds(0, 30, 200, 30); 121 122 textPanel.add(textField); 123 textField.setBounds(320, 30, 150, 30); 124 125 buttonPanel.add(dirButton); 126 buttonPanel.add(addButton); 127 128 129 130 131 // フォルダパス初期値 132 folderpath = "C:\\tmp"; 133 // folderlabel,textFieldを初期値設定 134 folderlabel.setText(folderpath); 135 136 textField.setText("UT_EOM_%d-%d-%d-%d.jpg"); //初期表示フォルダパス 137 138 139 mainFrame.setVisible(true); 140 screenShot = new ScreenShot(); 141 142 } 143 144 145 146 147 // 利用者の操作に応じた処理を実装 148 public void actionPerformed(ActionEvent event){ 149 // コマンド文字列を取得 150 String cmd = event.getActionCommand(); 151 152 if(cmd.equals("Capture")){ 153 int x = mainFrame.getX(); 154 int y = mainFrame.getY(); 155 // 画面キャプチャー 156 try{ 157 String filepath = FilePathCreate(); 158 // 自分を消す(見えない位置に移動) 159 mainFrame.setLocation(-1000,-1000); 160 // 画面キャプチャ 161 screenShot.screenCapture(filepath); 162 163 // 自分を表示(元の位置に移動) 164 mainFrame.setLocation(x, y); 165 166 //new Thumbnail(filepath); //サムネイルの表示 167 168 169 } catch (AWTException e){ 170 // TODO 自動生成された catch ブロック 171 e.printStackTrace(); 172 } catch (IOException e){ 173 // TODO 自動生成された catch ブロック 174 e.printStackTrace(); 175 } 176 } 177 else if(cmd.equals("Dir")) { 178 179 180 //folderpath = settingwindow.getfolderpath(); //folderpath()を取得する。 181 //folderlabel.setText(folderpath); //folderlabelにfolderpathをセットする。 182 183 184 JFileChooser filechooser = new JFileChooser(folderpath); //上二行と相互関係 185 186 filechooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY); 187 188 int selected = filechooser.showSaveDialog(this.contentPane); 189 if (selected == JFileChooser.APPROVE_OPTION){ 190 File file = filechooser.getSelectedFile(); 191 folderpath = file.getAbsolutePath(); 192 folderlabel.setText(folderpath); 193 194 195 } 196 197 else if(cmd.equals("settingwindow")){ 198 199 200 settingwindow = new SettingWindow(); 201 } 202 } 203 204 205 } 206 207 208 209 210 211 212 213 // ファイルのパスを生成 214 public String FilePathCreate() { 215 String filepath; 216 String filename; 217 filepath = folderpath; 218 219 filename = FileNameCreate(); 220 filepath = filepath + "\\" + filename; 221 return filepath; 222 } 223 224 225 // 数値リストからファイル名を生成 226 public String FileNameCreate() { 227 String filename; 228 String[] itemNumbers = new String[4]; 229 filename = textField.getText(); 230 int i=0; 231 int found = 0; 232 233 itemNumbers[0] = itemboxtest1.getText(); 234 itemNumbers[1] = itemboxtest2.getText(); 235 itemNumbers[2] = itemboxtest3.getText(); 236 itemNumbers[3] = itemboxtest4.getText(); 237 238 for(i=0; i<4; i++){ 239 found = filename.indexOf("%d",found); 240 if( found != 0 ){ 241 filename = filename.replaceFirst("%d",itemNumbers[i]); 242 } 243 } 244 return filename; 245 } 246 247 // アプリケーションの起動 248 public static void main(String[] args) { 249 new MainInit(); 250 } 251}
lang
1public class ItemBox extends JPanel implements ActionListener{ //ItemBoxはJPanelというオブジェクトクラスを継承 2 //ActionListnerというインタフェースを実装 3 private JButton upButton; 4 private JButton downButton; 5 private JTextField itemField; 6 private Container contentPane; 7 private JPanel itemPanel; 8 9 GridBagLayout gbl = new GridBagLayout(); 10 11 12 13 void addButton(Object itemobject, int x, int y, int w, int h) { 14 GridBagConstraints gbc = new GridBagConstraints(); 15 gbc.fill = GridBagConstraints.BOTH; 16 gbc.gridx = x; 17 gbc.gridy = y; 18 gbc.gridwidth = w; 19 gbc.gridheight = h; 20 gbl.setConstraints((Component) itemobject, gbc); 21 add((Component) itemobject); 22 } 23 24 public ItemBox(){ 25 26 //setLayout(null); 27 //setBackground(Color.blue); 28 29 itemField = new JTextField("0"); 30 itemField.setPreferredSize(new Dimension(20, 10)); //アイテムフィールドの大きさ指定 31 itemField.setFocusable(false); //入力制限 32 33 upButton = new JButton("↑"); //upButtonオブジェクトを作成し、↑を代入する。 34 downButton = new JButton("↓"); //downButtonオブジェクトを作成し、↓を代入する。 35 36 37 38 upButton.setActionCommand("upButton"); 39 upButton.addActionListener(this); //アクションイベントを受け取れるようになる。 40 downButton.setActionCommand("downButton"); 41 downButton.addActionListener(this); 42 43 setLayout(gbl); 44 addButton(itemField, 0, 0, 1, 2); // (0, 0) 幅=1, 高さ=3 45 addButton(upButton, 1, 0, 1, 1); // (1, 0) 幅=1, 高さ=1 46 addButton(downButton, 1, 1, 1, 1); // (1, 1) 幅=1, 高さ=1 47 48 49 50 } 51 52 53 54 public String getText(){ 55 56 57 return itemField.getText(); 58 59 60 } 61 62 63 //操作に応じた処理の実装 64 public void actionPerformed(ActionEvent event){ //イベントが発生するとここが処理される 65 //ユーザーの操作e対象の判断 //このクラスでのアクションリスナー 66 String cmd = event.getActionCommand(); 67 68 if(cmd.equals("upButton")) { 69 int i = Integer.parseInt(itemField.getText()); 70 i++; 71 if(i>99)i=0; 72 //itemField.setText(String.format("%1$02d",i)); //0埋め 73 itemField.setText(String.valueOf(i)); 74 75 } 76 else if(cmd.equals("downButton")){ 77 int i = Integer.parseInt(itemField.getText()); 78 i--; 79 if(i<0)i=99; 80 //itemField.setText(String.format("%1$02d",i)); //0埋め 81 itemField.setText(String.valueOf(i)); 82 83 } 84 } 85 86 87 88} 89 90
lang
1public class ScreenShot { 2 3 public void screenCapture(String filepath) throws AWTException, IOException { 4 Robot robot = new Robot(); 5 Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize(); 6 BufferedImage image = robot.createScreenCapture( 7 new Rectangle(0, 0, screenSize.width, screenSize.height)); 8 ImageIO.write(image, "PNG", new File(filepath)); //PNGファイルの保存 9 } 10} 11
回答2件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2015/07/08 14:09
2015/07/09 02:58
2015/07/09 04:03
2015/07/09 04:31
2015/07/09 05:11