import java.awt.Dimension ;
import java.awt.Point ;
import java.awt.event.ComponentAdapter ;
import java.awt.event.ComponentEvent ;
import java.awt.event.WindowAdapter ;
import java.awt.event.WindowEvent ;
import java.awt.event.WindowStateListener ;
import java.io.BufferedReader ;
import java.io.BufferedWriter ;
import java.io.File ;
import java.io.FileReader ;
import java.io.FileWriter ;
import java.io.IOException ;
import java.util.Date ;
import java.util.HashMap ;
import java.util.Map ;
import javax.swing.JFrame ;
public class saveWindowSize
extends JFrame {
// 座標記録用
// 最大化の時の巻き戻し用
// 座標を記録した時間
public static void main
( String [ ] args
) { new saveWindowSize( ) ;
}
saveWindowSize( ) {
init( ) ;
}
public int getIniInt
( String key,
int defaultValue
) { if ( iniMap.containsKey ( key) ) {
try {
return Integer .
parseInt ( iniMap.
get ( key
) ) ; // 握りつぶす
}
}
return defaultValue;
}
public void init( ) {
iniMap = loadIni( ) ;
this .
setDefaultCloseOperation ( JFrame .
EXIT_ON_CLOSE ) ; int x = getIniInt( "x" , 100 ) ;
int y = getIniInt( "y" , 100 ) ;
int w = getIniInt( "w" , 300 ) ;
int h = getIniInt( "h" , 200 ) ;
int e = getIniInt( "e" , 0 ) ;
this .setTitle ( "タイトル" ) ;
// ウィンドウ閉じる時にiniファイルを記録する
saveIni( ) ;
}
} ) ;
// ウィンドウが動いたら位置を記録する
// JFrame.NORMAL : 通常(デフォルト)
// JFrame.MAXIMIZED_HORIZ : 垂直方向に最大化
// JFrame.MAXIMIZED_VERT : 垂直方向に最大化
// JFrame.MAXIMIZED_BOTH : 全画面表示
// JFrame.ICONIFIED : アイコン化
// ウィンドウが通常の時だけウィンドウの位置を記録
if ( getExtendedState
( ) == JFrame .
NORMAL && isShowing
( ) ) { Point tmp
= getLocationOnScreen
( ) ; ptbk.setLocation ( pt) ;
pt.setLocation ( tmp) ;
System .
out .
println ( "" + pt.
x + "," + pt.
y ) ; }
}
// ウィンドウサイズが変化したらサイズを記録
// ウィンドウが通常の時だけサイズを記録
if ( getExtendedState
( ) == JFrame .
NORMAL ) { dim.setSize ( getSize( ) ) ;
}
}
} ) ;
// ウィンドウが最大化/最小化/通常になると呼び出される
addWindowStateListener( new WindowStateListener( ) {
@Override
// 最大化された場合
if ( getExtendedState
( ) == JFrame .
MAXIMIZED_BOTH ) { // 最大化した時に、ウィンドウの位置をステータスノーマルのままずらして来ることがあるので
// ウィンドウの位置を記録してから1秒以内に最大化通知が来たら、前の座標記録をキャンセルする
long l
= new Date ( ) .
getTime ( ) ; if ( l - ptDate.getTime ( ) < 1000 ) {
System .
out .
println ( "最大化座標を記録したかもなのでもとに戻す" ) ; pt.setLocation ( ptbk) ;
}
}
}
} ) ;
this .setBounds ( x, y, w, h) ;
this .setExtendedState ( e) ;
dim.setSize ( w, h) ;
pt.setLocation ( x, y) ;
ptbk.setLocation ( pt) ;
setVisible( true ) ;
}
public Map
< String , String
> loadIni
( ) { try {
while ( ( line = br.readLine ( ) ) != null ) {
int i = line.indexOf ( '=' ) ;
if ( i < 0 ) continue ;
String key
= line.
substring ( 0 , i
) ; String value
= line.
substring ( i
+ 1 ) ;
map.put ( key, value) ;
}
br.close ( ) ;
fr.close ( ) ;
e.printStackTrace ( ) ;
}
return map;
}
public void saveIni( ) {
try {
bw.write ( "x=" + pt.x ) ;
bw.newLine ( ) ;
bw.write ( "y=" + pt.y ) ;
bw.newLine ( ) ;
bw.write ( "w=" + dim.width ) ;
bw.newLine ( ) ;
bw.write ( "h=" + dim.height ) ;
bw.newLine ( ) ;
bw.write ( "e=" + getExtendedState( ) ) ;
bw.newLine ( ) ;
bw.close ( ) ;
fw.close ( ) ;
e.printStackTrace ( ) ;
}
}
}
aW1wb3J0IGphdmEuYXd0LkRpbWVuc2lvbjsKaW1wb3J0IGphdmEuYXd0LlBvaW50OwppbXBvcnQgamF2YS5hd3QuZXZlbnQuQ29tcG9uZW50QWRhcHRlcjsKaW1wb3J0IGphdmEuYXd0LmV2ZW50LkNvbXBvbmVudEV2ZW50OwppbXBvcnQgamF2YS5hd3QuZXZl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