class JMenuSample01 extends JFrame
{
MyLabel lbl;
JTextField txt;
public JMenuSample01()
{
super( "sample" );
setDefaultCloseOperation( EXIT_ON_CLOSE );
Dimension scr = Toolkit.getDefaultToolkit().getScreenSize();
int w = scr.width;
int h = scr.height;
setBounds( w / 4 , h / 4 , w / 4 , h / 5 );
JPanel panel = ( JPanel )getContentPane();
panel.setLayout( new FlowLayout() );
lbl = new MyLabel( "" , new Dimension( 100,50 ) );
lbl.setBackground( Color.green );
txt = new JTextField( 10 );
panel.add( lbl );
panel.add( new JButton( new FileSaveAction() ) );
panel.add( new JButton( new FileOpenAction() ) );
panel.add( new JButton( new ClearAction() ) ) ;
panel.add( txt );
setVisible( true );
JMenuItem fileMenuOpen = new JMenuItem( new FileOpenAction() );
JMenuItem fileMenuSave = new JMenuItem( new FileSaveAction() );
JMenu menuFile = new JMenu( "ファイル" );
menuFile.add(fileMenuOpen);
menuFile.add(fileMenuSave);
JMenuItem fileMenuClear = new JMenuItem( new ClearAction() );
JMenu menuEdit = new JMenu( "編集" );
menuEdit.add(fileMenuClear);
JMenuBar menu = new JMenuBar();
menu.add(menuFile);
menu.add(menuEdit);
setJMenuBar(menu);
}
public static void main( String[] arg )
{
new JMenuSample01();
}
class FileSaveAction extends AbstractAction
{
public FileSaveAction()
{
putValue( Action.NAME , "保存" );
}
public void actionPerformed( ActionEvent e )
{
try
{
MyFile mf = new MyFile();
mf.setFilePath("test.txt");
mf.fileSave( txt.getText() );
}
catch( IOException ex )
{
System.out.println( "error" );
}
}
}
class FileOpenAction extends AbstractAction
{
public FileOpenAction()
{
putValue( Action.NAME , "開く" );
}
public void actionPerformed( ActionEvent e )
{
try
{
MyFile mf = new MyFile();
mf.setFilePath( "test.txt" );
String dat = mf.fileRead();
if( dat != null )
{
lbl.setText( dat );
}
}
catch( IOException ex )
{
lbl.setText( "error" );
}
}
}
class ClearAction extends AbstractAction
{
public ClearAction()
{
putValue( Action.NAME , "クリア" );
}
public void actionPerformed( ActionEvent e )
{
txt.setText("");
lbl.setText("");
}
}
}
class MyLabel extends JLabel
{
public MyLabel( String title )
{
super( title );
}
public MyLabel( String title , Dimension size )
{
super( title );
setPreferredSize( size );
}
public MyLabel( String title ,int w , int h )
{
super( title );
setPreferredSize(new Dimension( w , h ) );
}
public void setBackground( Color color )
{
setOpaque( true );
super.setBackground( color );
}
}
class JMenuSample01 extends JFrame
{
/* ----------------------省略--------------------------- */
JMenuItem fileMenuOpen = new JMenuItem( new FileOpenAction() );
JMenuItem fileMenuSave = new JMenuItem( new FileSaveAction() );
JMenuItem end = new JMenuItem( new CloseAction() );
JMenu menuFile = new JMenu( "ファイル" );
menuFile.add(fileMenuOpen);
menuFile.add(fileMenuSave);
menuFile.addSeparator();
menuFile.add(end);
JMenuItem fileMenuClear = new JMenuItem( new ClearAction() );
JMenu menuEdit = new JMenu( "編集" );
menuEdit.add(fileMenuClear);
JMenuBar menuBar = new JMenuBar();
menuBar.add(menuFile);
menuBar.add(menuEdit);
setJMenuBar(menuBar);
setVisible( true );
}
public static void main( String[] arg )
{
new JMenuSample01();
}
class CloseAction extends AbstractAction
{
public CloseAction()
{
putValue( Action.NAME , "閉じる" );
}
public void actionPerformed( ActionEvent e )
{
System.exit(0);
}
}
}
class JFileChooser01 extends JFrame
{
private JTextField txt;
JPanel panel;
public JFileChooser01()
{
super( "sample" );
setDefaultCloseOperation( EXIT_ON_CLOSE );
Dimension scr = Toolkit.getDefaultToolkit().getScreenSize();
int w = scr.width;
int h = scr.height;
setBounds( w / 4 , h / 4 , w / 4 , h / 5 );
panel = ( JPanel )getContentPane();
txt = new JTextField(20);
panel.setLayout( new BorderLayout() );
panel.add( txt , BorderLayout.NORTH);
panel.add( new JButton( new DispAction() ) , BorderLayout.CENTER );
setVisible( true );
}
public static void main( String[] arg )
{
new JFileChooser01();
}
class DispAction extends AbstractAction
{
public DispAction()
{
putValue( Action.NAME , "表示" );
}
public void actionPerformed( ActionEvent e )
{
JFileChooser jfc = new JFileChooser();
int r = jfc.showOpenDialog(null);
}
}
}
class MyLabel extends JLabel
{
public MyLabel( String title )
{
super( title );
}
public MyLabel( String title , Dimension size )
{
super( title );
setPreferredSize( size );
}
public MyLabel( String title ,int w , int h )
{
super( title );
setPreferredSize(new Dimension( w , h ) );
}
public void setBackground( Color color )
{
setOpaque( true );
super.setBackground( color );
}
}
/*JFileChooserを開くモードで表示
*public int showOpenDialog(Component parent)を利用
* 定義より showOpenDialogメソッドは インスタンスメソッド
* 定義に static がない(クラスメソッドではない)
* => JFileChooserのオブジェクト生成が必要
* 例)
* JFileChooser jfc = new JFileChooser();
* int r = jfc.showOpenDialog(null);
*/
class JFileChooser01 extends JFrame
{
private JTextField txt;
JPanel panel;
public JFileChooser01()
{
super( "sample" );
setDefaultCloseOperation( EXIT_ON_CLOSE );
Dimension scr = Toolkit.getDefaultToolkit().getScreenSize();
int w = scr.width;
int h = scr.height;
setBounds( w / 4 , h / 4 , w / 4 , h / 5 );
panel = ( JPanel )getContentPane();
txt = new JTextField(20);
panel.setLayout( new BorderLayout() );
panel.add( txt , BorderLayout.NORTH);
panel.add( new JButton( new DispAction() ) , BorderLayout.CENTER );
setVisible( true );
}
public static void main( String[] arg )
{
new JFileChooser01();
}
class DispAction extends AbstractAction
{
public DispAction()
{
putValue( Action.NAME , "表示" );
}
public void actionPerformed( ActionEvent e )
{
JFileChooser jfc = new JFileChooser();
int r = jfc.showOpenDialog(null);
}
}
}
class MyLabel extends JLabel
{
public MyLabel( String title )
{
super( title );
}
public MyLabel( String title , Dimension size )
{
super( title );
setPreferredSize( size );
}
public MyLabel( String title ,int w , int h )
{
super( title );
setPreferredSize(new Dimension( w , h ) );
}
public void setBackground( Color color )
{
setOpaque( true );
super.setBackground( color );
}
}
class FileMenuEnsyu extends JFrame
{
String filePath = "";
JTextField txt;
public FileMenuEnsyu()
{
super( "sample" );
setDefaultCloseOperation( EXIT_ON_CLOSE );
Dimension scr = Toolkit.getDefaultToolkit().getScreenSize();
int w = scr.width;
int h = scr.height;
setBounds( w / 4 , h / 4 , w / 4 , h / 5 );
JPanel panel = ( JPanel )getContentPane();
panel.setLayout( new FlowLayout() );
txt = new JTextField( 10 );
panel.add( txt );
JMenuItem newOpen = new JMenuItem( new NewAction() );
JMenuItem fileMenuOpen = new JMenuItem( new FileOpenAction() );
JMenuItem fileOverWrite = new JMenuItem( new FileOverWriteAction() );
JMenuItem fileMenuSave = new JMenuItem( new FileSaveAction() );
JMenuItem end = new JMenuItem( new CloseAction() );
JMenu menuFile = new JMenu( "ファイル" );
menuFile.add(newOpen);
menuFile.add(fileMenuOpen);
menuFile.add(fileOverWrite);
menuFile.add(fileMenuSave);
menuFile.addSeparator();
menuFile.add(end);
JMenuBar menuBar = new JMenuBar();
menuBar.add(menuFile);
setJMenuBar(menuBar);
setVisible( true );
}
public static void main( String[] arg )
{
new FileMenuEnsyu();
}
class FileSaveAction extends AbstractAction
{
public FileSaveAction()
{
putValue( Action.NAME , "名前を付けて保存" );
}
public void actionPerformed( ActionEvent e )
{
ExampleFileFilter filter = new ExampleFileFilter();
JFileChooser jfc = new JFileChooser();
jfc.setFileFilter( filter );
int r = jfc.showSaveDialog( null );
try
{
if( r == JFileChooser.APPROVE_OPTION )
{
filePath = jfc.getSelectedFile().toString();
MyFile mf = new MyFile();
mf.setFilePath( filePath );
mf.fileSave( txt.getText() );
}
}
catch( IOException ex )
{
System.out.println( "error" );
}
}
}
class FileOpenAction extends AbstractAction
{
public FileOpenAction()
{
putValue( Action.NAME , "開く" );
}
public void actionPerformed( ActionEvent e )
{
ExampleFileFilter filter = new ExampleFileFilter();
JFileChooser jfc = new JFileChooser();
jfc.setFileFilter( filter );
int r = jfc.showOpenDialog( null );
try
{
if( r == JFileChooser.APPROVE_OPTION )
{
filePath = jfc.getSelectedFile().toString();
MyFile mf = new MyFile();
mf.setFilePath( filePath );
String dat = mf.fileRead();
if( dat != null )
{
txt.setText( dat );
}
}
}
catch( IOException ex )
{
System.out.println( "error" );
}
}
}
class FileOverWriteAction extends AbstractAction
{
public FileOverWriteAction()
{
putValue( Action.NAME , "上書き保存" );
}
public void actionPerformed( ActionEvent e )
{
ExampleFileFilter filter = new ExampleFileFilter();
JFileChooser jfc = new JFileChooser();
jfc.setFileFilter( filter );
try
{
if( filePath == "")
{
int r = jfc.showSaveDialog( null );
class CloseAction extends AbstractAction
{
public CloseAction()
{
putValue( Action.NAME , "閉じる" );
}
public void actionPerformed( ActionEvent e )
{
System.exit(0);
}
}
}
class MyLabel extends JLabel
{
public MyLabel( String title )
{
super( title );
}
public MyLabel( String title , Dimension size )
{
super( title );
setPreferredSize( size );
}
public MyLabel( String title ,int w , int h )
{
super( title );
setPreferredSize(new Dimension( w , h ) );
}
public void setBackground( Color color )
{
setOpaque( true );
super.setBackground( color );
}
}
public class DrawSamp1 extends JFrame
{
public DrawSamp1(String title)
{
super(title);
setDefaultCloseOperation(EXIT_ON_CLOSE);
Dimension scrSize = Toolkit.getDefaultToolkit().getScreenSize();
int w = scrSize.width;
int h = scrSize.height;
setBounds(w/4 , h/4 , w/2 , h/2);
//MyPaintの生成
MyPaint canvas = new MyPaint();
//描画領域への登録
getContentPane().add(canvas);
setVisible(true);
}
public static void main (String[] args)
{
new DrawSamp1("Draw!");
}
}
class MyPaint extends Canvas
{
public MyPaint()
{
super();
}
public void paint(Graphics g)
{
g.drawLine(100,10,100,100);
g.drawLine(10,10,10,100);
g.drawLine(10,10,100,10);
g.drawLine(10,100,100,100);
public class DrawSamp2 extends JFrame
{
public DrawSamp2(String title)
{
super(title);
setDefaultCloseOperation(EXIT_ON_CLOSE);
Dimension scrSize = Toolkit.getDefaultToolkit().getScreenSize();
int w = scrSize.width;
int h = scrSize.height;
setBounds(w/4 , h/4 , w/2 , h/2);
//MyPaintの生成
MyPaint canvas = new MyPaint();
//描画領域への登録
getContentPane().add(canvas);
setVisible(true);
}
public static void main (String[] args)
{
new DrawSamp1("Draw!");
}
}
class MyPaint extends Canvas implements MouseListener
{
int x,y;
public MyPaint()
{
super();
addMouseListener(this);
}
/*public void paint(Graphics g)
{
g.drawLine(10,10,x,y);
}*/
public void update(Graphics g)
{
g.drawLine(10,10,x,y);
}
public void mouseClicked(MouseEvent e)
{
x = e.getX();
y = e.getY();
repaint();
}
public void mouseEntered(MouseEvent e){}
public void mouseExited(MouseEvent e){}
public void mousePressed(MouseEvent e){}
public void mouseReleased(MouseEvent e){}
public class DrawSamp5 extends JFrame
{
public DrawSamp5(String title)
{
super(title);
setDefaultCloseOperation(EXIT_ON_CLOSE);
Dimension scrSize = Toolkit.getDefaultToolkit().getScreenSize();
int w = scrSize.width;
int h = scrSize.height;
setBounds(w/4 , h/4 , w/2 , h/2);
MyPaint canvas = new MyPaint();
getContentPane().add(canvas);
setVisible(true);
}
public static void main (String[] args)
{
new DrawSamp5("Draw!");
}
}
class MyPaint extends JPanel implements MouseListener , MouseMotionListener
{
int x1,y1, x2,y2;
int x, y , w , h;
boolean drawMode = false ;
public MyPaint()
{
super();
addMouseListener(this);
addMouseMotionListener(this);
}
public void paintComponent(Graphics g)
{
super.paintComponent(g);
public class DrawSamp6 extends JFrame
{
public DrawSamp6(String title)
{
super(title);
setDefaultCloseOperation(EXIT_ON_CLOSE);
Dimension scrSize = Toolkit.getDefaultToolkit().getScreenSize();
int w = scrSize.width;
int h = scrSize.height;
setBounds(w/4 , h/4 , w/2 , h/2);
MyPaint canvas = new MyPaint();
getContentPane().add(canvas);
setVisible(true);
}
public static void main (String[] args)
{
new DrawSamp6("Draw!");
}
}
class MyPaint extends JPanel implements MouseListener , MouseMotionListener
{
int x1,y1, x2,y2;
int x, y , w , h;
int wx , wy;
boolean drawMode = false ;
public MyPaint()
{
super();
addMouseListener(this);
addMouseMotionListener(this);
}
public class DrawSamp7 extends JFrame
{
public DrawSamp7(String title)
{
super(title);
setDefaultCloseOperation(EXIT_ON_CLOSE);
Dimension scrSize = Toolkit.getDefaultToolkit().getScreenSize();
int w = scrSize.width;
int h = scrSize.height;
setBounds(w/4 , h/4 , w/2 , h/2);
MyPaint canvas = new MyPaint();
canvas.setDrawType(MyPaint.DRAW_OVAL);
canvas.setDrawColor();
getContentPane().add(canvas);
setVisible(true);
}
public static void main (String[] args)
{
new DrawSamp7("Draw!");
}
}
class MyPaint extends JPanel implements MouseListener , MouseMotionListener
{
int x1,y1, x2,y2;
int x, y , w , h;
int wx , wy;
int drawType;
boolean drawMode = false ;
boolean setMode = false ;
Color color;
public final static int DRAW_LINE = 0;
public final static int DRAW_RECT = 1;
public final static int DRAW_OVAL = 2;
Image backImage = null; //2つ目の描画領域用変数
public MyPaint()
{
super();
addMouseListener(this);
addMouseMotionListener(this);
}
public void setDrawType(int drawType)
{
this.drawType = drawType;
}
public void setDrawColor()
{
try
{
color = JColorChooser.showDialog(null , "Color" , Color.black);
public class DrawEnsyu1111 extends JFrame implements WindowStateListener , ComponentListener
{
MyPaint canvas = new MyPaint();
public DrawEnsyu1111(String title)
{
super(title);
setDefaultCloseOperation(EXIT_ON_CLOSE);
Dimension scrSize = Toolkit.getDefaultToolkit().getScreenSize();
int w = scrSize.width;
int h = scrSize.height;
setBounds(w/4 , h/4 , w/2 , h/2);
JMenu mFile = new JMenu( "ファイル" );
mFile.add(new JMenuItem( new mFCloseAction() ) );
JMenu mDrawType = new JMenu( "描画タイプ" );
mDrawType.add(new JMenuItem( new mDrawLineAction() ) );
mDrawType.add(new JMenuItem( new mDrawRectAction() ) );
mDrawType.add(new JMenuItem( new mDrawOvalAction() ) );
JMenu mEdit = new JMenu( "編集" );
mEdit.add(new JMenuItem( new mEditColorAction() ) );
JMenuBar menu = new JMenuBar();
menu.add( mFile );
menu.add( mDrawType );
menu.add( mEdit );
public void componentHidden(ComponentEvent e){}
public void componentMoved(ComponentEvent e){}
public void componentResized(ComponentEvent e)
{
canvas.sizeMode = true;
}
public void componentShown(ComponentEvent e){}
}
class MyPaint extends JPanel implements MouseListener , MouseMotionListener
{
int x1,y1, x2,y2;
int x, y , w , h;
int wx , wy;
boolean drawMode = false ;
boolean setMode = false ;
boolean sizeMode = false;
Color color = Color.black;
public final static int DRAW_LINE = 0;
public final static int DRAW_RECT = 1;
public final static int DRAW_OVAL = 2;
private int drawType = DRAW_LINE;
Image backImage = null;
public MyPaint()
{
super();
addMouseListener(this);
addMouseMotionListener(this);
}
public void setDrawType(int drawType)
{
this.drawType = drawType;
}
public void setDrawColor()
{
try
{
color = JColorChooser.showDialog(null , "Color" , color);
if(color == null)
{
color = Color.black;
}
}
catch(HeadlessException e)
{
;
}
}