• ベストアンサー

ImageIconをJButtonに事後的に表示するには

こんにちは。 JButtonにImageIconを表示するのに、コンストラクタ で表示するのは分かるのですが、事後的に表示する 方法は無いでしょうか。 具体的には、ボタンを押したのをきっかけに、 ボタンの画像を変えるとか。 実は・・ 現在凡庸的なレイアウトのJButton,JTextAreaなどを 配置したフレームを継承して使っているのですが、 JButtonは、既に配置されているので、コンストラクタ では、ImageIconを設置できないのです。 なにか分かる人、お願いします。

質問者が選んだベストアンサー

  • ベストアンサー
  • liar_adan
  • ベストアンサー率48% (730/1515)
回答No.1

jbutton.setIcon(imageIcon); でできるはずです。 あと、setPressedIcon()とか、 setRolloverIcon()とかsetText()とかを 必要に応じて使えば、望む動作ができるでしょう。 これらのメソッドは、JButtonではなく、 その上のAbstractButtonクラスに書いてます。 Javaドキュメントを参照ください。

pizzicato33
質問者

お礼

ありがとうございました。 うまくいきました。

全文を見る
すると、全ての回答が全文表示されます。

関連するQ&A

  • JTextAreaの大きさの指定と固定について

    今JTextAreaをJPanelに貼り付けてそれを更にJFrameに貼り付けるプログラムを書いています。 フレームはBorderLayoutにしており、WESTにもEASTにもパネルをaddしてます。 frame.add(createWestPanel(), BorderLayout.WEST); JPanel createWestPanel() {   ・・・   return p; } の様な形です。 このパネルにJTextAreaを使用しており、 ボタンを押すとJTextAreaに文を表示する仕様にしたいのですが 大きさを指定しなければ表示する字数によって大きさが勝手に変更されてしまい(その様になるのは当たり前ですが)、他のコンポーネント(例えばBorderLayout.CENTERにあるボタンなど)と重なったりしてしまいます。 そこで、JTextAreaの大きさを指定した上で固定したいのですがどのようにすればよいでしょうか? また、 WestPanelを □□□    ボタン (JTextField) (JButton)  JTextArea と言った形にしたいのですが、どうやってもJTextAreaがJButtonの右に来てしまいます。 これも解決方法を教えていただけると助かります。 お願いします。

  • cadで作った図面をフレーム上で表示

    こんばんは いつもお世話になっています。 gif形式の図を表示することはできるのですが AutoCADで描いた図はgifファイルに出力できません。 下記のようにコーディングしたのですが、”ぜひお越しください”しか表示されませんでした。 ほかの形式(dxf、wfm、bmp)でもいろいろ試したのですができませんでした。 どうすればAutoCADで作った図をフレーム上に表示できるのでしょうか? どなたか教えていただけませんか? import java.awt.*; import javax.swing.*; import javax.swing.ImageIcon; class map { public static void main(String args[]) { JFrame frame=new JFrame("map test"); JPanel panel=new JPanel(); ImageIcon ico=new ImageIcon("地図.gif"); JButton button=new JButton("ぜひお越しください",ico); panel.add(button); Container ct=frame.getContentPane(); ct.add(panel); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.pack(); frame.setVisible(true); } }

  • コミュニケーション支援用の画面を作っております

    コミュニケーション画面を作っています。記号絵の表示です。絵で書いた紙を使っていましたが、かける絵が少なく、小さい表示しかできず不便を感じております。自分で次の紙をめくるのも手足が不自由で苦労しております。タブを付け、画面を表示したり、数個のボタンで絵記号を選ぶようにしたいです。(1)画面の北面に記号が意味する説明を表示し、中面には絵記号を並べ、南面には画面を押すたびに絵記号と説明が切り替わるようにしようとしています。(2)また、動作や物、人など分野別にインデックスを付け、そのインデックスも南面にのボタンで切り替わるようにしたいです。(1)間ではできたのですが、タブ(インデックス)上に(1)を置く方法がどうしても解りません。またタブをボタンで切り替える事もできません。コードは以下です。お詳しい方がおられましたら教えてください。 import javax.swing.*; import java.awt.CardLayout; import java.awt.BorderLayout; import java.awt.event.*; public class ekigou2 extends JFrame implements ActionListener{ JPanel cardPanel; JPanel cardPanel2; CardLayout layout; public static void main(String[] args){ ekigou2 frame = new ekigou2(); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setBounds(10, 10, 300, 200); frame.setTitle("タイトル"); frame.setVisible(true); } ekigou2(){ JTabbedPane tabbedpane = new JTabbedPane(); //tubb_on_panel JPanel tabPanel1 = new JPanel(); JPanel tabPanel2 = new JPanel(); //tabb_image ImageIcon tbicon1 = new ImageIcon("./img/post.gif"); ImageIcon tbicon2 = new ImageIcon("./img/mail.gif"); //tab_set tabbedpane.addTab("tab1", tbicon1, tabPanel1); tabbedpane.addTab("tab2", tbicon2, tabPanel2); tabbedpane.setIconAt(1, tbicon1); tabbedpane.setIconAt(1, tbicon2); //card_icon_image ImageIcon cardicon1 = new ImageIcon("./img/dousa/203025行く.jpg"); ImageIcon cardicon2 = new ImageIcon("./img/dousa/203026来る.jpg"); //smol_icon_image ImageIcon icon1s = new ImageIcon("./img/dousa/203025行くs.jpg"); ImageIcon icon2s = new ImageIcon("./img/dousa/203026来るs.jpg"); // card_icon_roop JPanel card1 = new JPanel(); card1.add(new JButton("行く",cardicon1)); JPanel card2 = new JPanel(); card2.add(new JButton("来る",cardicon2)); cardPanel = new JPanel(); layout = new CardLayout(); cardPanel.setLayout(layout); cardPanel.add(card1, "button1"); cardPanel.add(card2, "button2"); // smol_icon_switch JButton button1 = new JButton("行く",icon1s); button1.addActionListener(this); button1.setActionCommand("button1"); JButton button2 = new JButton("来る",icon2s); button2.addActionListener(this); button2.setActionCommand("button2"); //小さいボタンを乗せる JPanel btnPanel = new JPanel(); btnPanel.add(button1); btnPanel.add(button2); JButton nextButton = new JButton("Next"); nextButton.addActionListener(this); nextButton.setActionCommand("Next"); JPanel btnPanel2 = new JPanel(); btnPanel2.add(nextButton); getContentPane().add(cardPanel, BorderLayout.NORTH);//card_button getContentPane().add(btnPanel, BorderLayout.WEST);//smol_button getContentPane().add(btnPanel2, BorderLayout.PAGE_END);//next_button getContentPane().add(tabbedpane);//tub_panel } public void actionPerformed(ActionEvent e){ String cmd = e.getActionCommand(); if (cmd.equals("Next")){ layout.next(cardPanel); } String cmd2 = e.getActionCommand(); layout.show(cardPanel, cmd2); } }

    • ベストアンサー
    • Java
  • Jave Swing 画像表示わからない

    java swingでGUIをプログラミングしている者です。 JLabelにImageIconの画像を貼り付けるプログラムです。↓ //画像を表示するプログラム //インポート import javax.swing.*; import java.awt.*; import java.awt.event.*; class gazopro extends JFrame{ JLabel l1; JButton b1; JPanel p; public static void main(String args[]) { //ウィンドウを作成また設定 gazopro frame = new gazopro(); frame.setBounds(300,200,700,400); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setVisible(true); } gazopro(){ //コンポーネントを作成 p = new JPanel(); add(p); b1 = new JButton("画像を表示"); p.add(b1); ImageIcon icon = new ImageIcon("image.jpg"); l1 = new JLabel(icon); p.add(l1); } }   わからないところは、ImageIcon icon = new ImageIcon("image.jpg");のところです。 ImageIconの引数は、プログラミングを行っているパソコン(OS は Windows)のマイコンピューター→ Cドライブ→ MyJava→ image.jpgを指定しましたがこれで合ってますか? このプログラムでは、画像が表示されませんでした。 どうすればいいでしょうか? 解説と、ご指導宜しくお願いします。

    • ベストアンサー
    • Java
  • Eclipse3.6でSwingアプリケーションを作成しています。

    Eclipse3.6でSwingアプリケーションを作成しています。 ImageIconで画像のアドレスを登録し、画像をボタンとして使用したいのですが、画像のアドレスがフルアドレスでないとEclipse上で実行したときに画像が表示されません。 起動しているJAVAファイルと画像ファイルは同じ場所に保存してあります。 何か他の設定があるのでしょうか? 通常のJAVAコンパイル時のソース ImageIcon icon1 = new ImageIcon("./test.png"); JButton button1 = new JButton(icon1); ※このソースではEclipse上では画像が表示されない Eclipse上で起動させて時のソース ImageIcon icon1 = new ImageIcon("c:/eclipse/workspace/src/test.png"); JButton button1 = new JButton(icon1); 以上です。 宜しくお願いします。

    • ベストアンサー
    • Java
  • JAVAでの背景画像表示

    現在javaでゲームのメニュー画面を作っているのですが、ボタンを配置する所まではできたんですが、背景に画像を表示することができなくて困っています! import javax.swing.JFrame; import javax.swing.JPanel; import javax.swing.JLabel; import javax.swing.ImageIcon; import java.awt.Container; import java.awt.BorderLayout; import javax.swing.border.LineBorder; import javax.swing.border.EtchedBorder; import java.awt.Color; import java.awt.Container; public class Mati extends JFrame{ public static void main(String[] args){ Mati frame = new Mati(); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setBounds(10, 10,650,650); frame.setTitle("街"); frame.setVisible(true); JPanel h = new JPanel(); h.setOpaque(false); ImageIcon icon1 = new ImageIcon("Mati.jpg"); JLabel label1 = new JLabel(icon1); JLabel label2 = new JLabel(); h.add(label1); } Mati(){ JButton button1 = new JButton("宿屋"); button1.setFont(new Font("Mairyo", Font.PLAIN, 30)); JButton button2 = new JButton("道具屋"); button2.setFont(new Font("Mairyo", Font.PLAIN, 30)); JButton button3 = new JButton("武器屋"); button3.setFont(new Font("Mairyo", Font.PLAIN, 30)); JButton button4 = new JButton("街を出る"); button4.setFont(new Font("Mairyo", Font.PLAIN, 30)); JPanel p = new JPanel(); p.setLayout(new BoxLayout(p, BoxLayout.Y_AXIS)); p.add(Box.createRigidArea(new Dimension(10,30))); p.add(button1); p.add(Box.createRigidArea(new Dimension(5,8))); p.add(button2); p.add(Box.createRigidArea(new Dimension(5,8))); p.add(button3); p.add(Box.createRigidArea(new Dimension(5,8))); p.add(button4); getContentPane().add(p, BorderLayout.CENTER); } } ソースはこのようになっています。これからどうすれば背景画像が表示されるか教えていただきたいです。 よろしくお願いします。

  • JAVAアプレット レイアウトの変更

    JAVAアプレットのinit()の部分にラベルやボタンをきれいに設置するためGridLayoutを使って配置したのですが、ボタンを押したあとに今あったGridLayoutの場所の新しいレイアウトで新しいラベルを配置することはできるのでしょうか? 一応アプリケーションの場合は違うコンストラクターの新しいインスタンスを作成してやればいいなとおもったのですがアプレットの場合はどうすればいいのか分からず困っています。 どなたかよろしくお願いします。

  • java起動時にボタンが表示されない

    Javaの初心者です。以下のプログラムを起動するとJTextAreaとJLabel は、表示されるのですが、JButtonが表示されません。起動後にボタンの配置したところを触るとボタンが表示されるようになるのですが、ボタンのアクションリスナーが動作してしまいます。起動時にボタンも表示できるようにするには、どうしたらよいでしょうか? public class test_pro extends JFrame{ private static final long serialVersionUID = 1L; private JLabel label1;     JButton buton1,button2;     public JTextArea lt1; public test_pro() { this.setUndecorated(true);    GraphicsDevice device = GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice(); device.setFullScreenWindow(this); Container contentPane = getContentPane(); contentPane.setLayout(null); label1 = new JLabel("menu"); Font f1=new Font("Serif",1,22); label1.setFont(f1); label1.setForeground(Color.red);     contentPane.add(label1); lt1=new JTextArea(3,3); JScrollPane pane=new JScrollPane(lt1); pane.setBounds(new Rectangle(50,300,550,150)); contentPane.add(pane);     button1 = new JButton("SendRequestTest!"); button2 = new JButton("ReceiveResponseTest!"); label1.setBounds(new Rectangle(50,20,200,20)); button1.setBounds(new Rectangle(50,80,230,20)); button2.setBounds(new Rectangle(50,110,230,20));  contentPane.add(button1); contentPane.add(button2); button1.addActionListener(new MyJsendActionAdapter()); button2.addActionListener(new MyJreceiveActionAdapter());     this.setDefaultCloseOperation(EXIT_ON_CLOSE);     this.setVisible(true);    } /*以下省略*/

  • Jframeの中にJframeを表示させたい

    Jframeの中にボタンを作り、ボタンを押すとJInternalFrameではなくJframeを表示させるようにしたいんですが、なかなかできません。 検索したりしてサンプル探したんですが、 Jframeを表示させ、ラベルを貼ったりしたいんですが どなたかお願いします!! public class Browser extends JFrame{ public static void main(String args[]) { Browser frame = new Browser(); frame.setTitle(""); frame.setExtendedState(JFrame.MAXIMIZED_BOTH); frame.setVisible(true); } //ボタン private JButton bt = new JButton("~~"); public Browser() { bt.addActionListener(this); JToolBar tb = new JToolBar(); tb.setFloatable(false); getContentPane().add(tb, BorderLayout.NORTH); JPanel panel2 = new JPanel(); panel2.add(bt); panel2.setLayout(new FlowLayout(FlowLayout.LEFT)); tb.add(panel2); } //↓この辺りの処理の記述で頭が混乱してきました(泣 public class actionPerformed(ActionEvent e){ if(e.getSource() == bt){ Frame FW = new Frame(); FW.setSize(500,400); FW.setVisible(true); } } class Frame extends Frame{ public Frame(){ //コンストラクタの定義 } }

    • ベストアンサー
    • Java
  • javaのボタン表示等に関する質問

    下記のコードはフォームをNORTH、CENTER、SOUTHEに分割して、それぞれのpaneに ラベルとボタンを表示するものです。 Q1)myFrame.setBounds(350,0, total_x, total_y)をコンストラクターの最初に記述しますと  フォームの表示が正常でなく、コンストラクターの最後では、正常に行なわれます。  この理由等について、コメント頂けますと有り難いです。 Q2)mainに記述してある、下記のコードは無くても、Xでクロースできますが,このコード  の記述は正しいでしょうか GridLayout_new frame = new GridLayout_new(); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 以上お手数ですが、コメント頂けますと大変助かります。 //============================================= import java.awt.BorderLayout; import java.awt.Color; import java.awt.Dimension; import java.awt.GridLayout; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JPanel; import javax.swing.border.LineBorder; public class GridLayout_new extends JFrame{ int panelA_x=200, panelA_y=50; int panelB_x=200, panelB_y=200; int panelC_x=200, panelC_y=50; int total_x=panelA_x; int total_y=panelA_y+panelB_y+panelC_y; public static void main(String[] args){ GridLayout_new frame = new GridLayout_new(); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); } GridLayout_new(){ JFrame myFrame=new JFrame(); myFrame.setTitle("panelA"); // myFrame.setBounds(350,0, total_x, total_y); //正常なformの表示が出来ない myFrame.setVisible(true); LineBorder border = new LineBorder(Color.BLACK, 1, true); //======================== JLabel button1=new JLabel("Label1"); GLabel_Layout(button1, border); JButton button2 = new JButton("button2"); button_Layout(button2, border); //======================== JLabel button3=new JLabel("Label3"); GLabel_Layout(button3, border); JButton button4 = new JButton("button4"); button_Layout(button4, border); //======================== JLabel button5=new JLabel("Label5"); GLabel_Layout(button5, border); JButton button6 = new JButton("button6"); //======================== JLabel button7=new JLabel("Label7"); GLabel_Layout(button7, border); JButton button8 = new JButton("button8"); //======================== JLabel button9=new JLabel("Label9"); GLabel_Layout(button9, border); JButton button10 = new JButton("button10"); button_Layout(button10, border); //======================== JLabel button11=new JLabel("Label11"); GLabel_Layout(button11, border); JButton button12 = new JButton("button12"); button_Layout(button12, border); //================================== JPanel p1 = new JPanel(); GridLayout layout1 = new GridLayout(2,2); //2行、2列の設定 layout1.setHgap(2); layout1.setVgap(10); p1.setLayout(layout1); p1.setBackground(Color.GREEN); p1.add(button1); p1.add(button2); p1.add(button3); p1.add(button4); myFrame.setSize(panelA_x, panelA_y); myFrame.getContentPane().add(p1, BorderLayout.NORTH); //============================== JPanel p2 = new JPanel(); GridLayout layout2 = new GridLayout(2,2); //2行、2列の設定 layout2.setHgap(2); layout2.setVgap(10); p2.setLayout(layout2); p2.setBackground(Color.GREEN); p2.add(button5); p2.add(button6); p2.add(button7); p2.add(button8); myFrame.setSize(panelB_x, panelB_y); myFrame.getContentPane().add(p2, BorderLayout.CENTER); //================================== JPanel p3 = new JPanel(); GridLayout layout3 = new GridLayout(2,2); //2行、2列の設定 layout3.setHgap(2); layout3.setVgap(10); p3.setLayout(layout3); p3.setBackground(Color.GREEN); p3.add(button9); p3.add(button10); p3.add(button11); p3.add(button12); myFrame.setSize(panelC_x, panelC_y); myFrame.getContentPane().add(p3, BorderLayout.SOUTH); myFrame.setBounds(350,0, total_x, total_y); //正常なformの表示 } //constructor void GLabel_Layout(JLabel label, LineBorder border){ label.setPreferredSize(new Dimension(80,20)); label.setBorder(border); label.setBackground(Color.lightGray); label.setOpaque(true); } void button_Layout(JButton button, LineBorder border){ button.setPreferredSize(new Dimension(80,20)); button.setBorder(border); } } //main class....GridLayout_new

    • ベストアンサー
    • Java