//************************************************************* //           ツールバー動作確認 //           「ToolBarTest」 // //          作 成 者:ルート高菜    //          作成開始月:2007/4 //          最終更新月:2007/11 [TN112/J102] //************************************************************* import java.awt.BorderLayout; import java.awt.Color; import java.awt.Container; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.event.KeyEvent; import javax.swing.AbstractButton; import javax.swing.ImageIcon; import javax.swing.JApplet; import javax.swing.JButton; import javax.swing.JPanel; import javax.swing.JToolBar; public class ToolBarTest extends JApplet implements ActionListener { //Eclipseの場合、シリアライズ可能クラスでこれがないと警告が出る private static final long serialVersionUID=0; // JButton button01; JButton button02; JButton button03; JButton button11; JButton button12; JButton button13; JButton button21; JButton button22; // JPanel panel; // public void init(){ //ボタンごとに画像と文字を設定、ツールチップとして説明を加える button01=new JButton("夏の夜の夢(A)"); button01.setToolTipText("文字のみ指定"); // button02=new JButton(new ImageIcon(getImage(getCodeBase(),"image/01.gif"))); button02.setToolTipText("画像のみ指定"); // button03=new JButton("マクベス(C)",new ImageIcon(getImage(getCodeBase(),"image/02.gif"))); button03.setToolTipText("両方指定(文字がdefault、default)"); // button11=new JButton("オセロー(D)",new ImageIcon(getImage(getCodeBase(),"image/03.gif"))); button11.setToolTipText("両方指定(文字がTOP、CENTER)"); button11.setVerticalTextPosition(AbstractButton.TOP); button11.setHorizontalTextPosition(AbstractButton.CENTER); // button12=new JButton("リア王(E)",new ImageIcon(getImage(getCodeBase(),"image/03.gif"))); button12.setToolTipText("両方指定(文字が.CENTER、CENTER)"); button12.setVerticalTextPosition(AbstractButton.CENTER); button12.setHorizontalTextPosition(AbstractButton.CENTER); // button13=new JButton("じゃじゃ馬ならし(F)",new ImageIcon(getImage(getCodeBase(),"image/03.gif"))); button13.setToolTipText("両方指定(文字がBOTTOM、CENTER)"); button13.setVerticalTextPosition(AbstractButton.BOTTOM); button13.setHorizontalTextPosition(AbstractButton.CENTER); // button21=new JButton("ベニスの商人(G)",new ImageIcon(getImage(getCodeBase(),"image/04.gif"))); button21.setToolTipText("両方指定(文字がTOP、LEFT)"); button21.setVerticalTextPosition(AbstractButton.TOP); button21.setHorizontalTextPosition(AbstractButton.LEFT); // button22=new JButton("空騒ぎ(H)",new ImageIcon(getImage(getCodeBase(),"image/04.gif"))); button22.setToolTipText("両方指定(文字がBOTTOM、LEFTT)"); button22.setVerticalTextPosition(AbstractButton.BOTTOM); button22.setHorizontalTextPosition(AbstractButton.LEFT); // JToolBar tool=new JToolBar("ToolBar"); tool.add(button01); tool.add(button02); tool.add(button03); tool.addSeparator(); tool.add(button11); tool.add(button12); tool.add(button13); tool.addSeparator(); tool.add(button21); tool.add(button22); // //コンテナ枠 Container cp=getContentPane(); cp.add(tool,BorderLayout.NORTH); cp.add(panel=new JPanel(),BorderLayout.CENTER); //ニーモニック(キーボードからAlt+?で選択) button01.setMnemonic(KeyEvent.VK_A); button02.setMnemonic(KeyEvent.VK_B); button03.setMnemonic(KeyEvent.VK_C); button11.setMnemonic(KeyEvent.VK_D); button12.setMnemonic(KeyEvent.VK_E); button13.setMnemonic(KeyEvent.VK_F); button21.setMnemonic(KeyEvent.VK_G); button22.setMnemonic(KeyEvent.VK_H); // button01.addActionListener(this); button02.addActionListener(this); button03.addActionListener(this); button11.addActionListener(this); button12.addActionListener(this); button13.addActionListener(this); button21.addActionListener(this); button22.addActionListener(this); } public void actionPerformed(ActionEvent e) { int r=(int)(Math.random()*256); int g=(int)(Math.random()*256); int b=(int)(Math.random()*256); panel.setBackground(new Color(r,g,b)); } }