2012-02-21 14 views
4

JButtonをクリックしたときにJLabelを表示しようとしています。私はアクションリスナーを追加し、そのコンポーネントをレイアウトに追加しました。 actionPerformedでJButtonをクリックしたときにlabel1.setVisible(true)を使用しています。私はまだそれを働かせることはできません。私のコードを見ることができますか?actionPerformedでJButtonをクリックするとJLabelが表示される

public class LearnAppMain extends JFrame implements ActionListener { 

// Define variables 
public JButton button1; 
public JLabel label1; 
    public JTextField field1; 

    private Image image1; 
private String apple = "apple.jpg"; 

public LearnAppMain() { 

    ImageIcon image1 = new ImageIcon(this.getClass().getResource(apple)); 
    JLabel label1 = new JLabel(image1); 

    button1 = new JButton("A"); 
    button1.addActionListener(this); 

    field1 = new JTextField(10); 

    // Create layout 
    setLayout(new FlowLayout()); 

    // create Container 
    final Container cn = getContentPane(); 

    cn.add(button1); 
    cn.add(field1); 
    cn.add(label1); 

    // setLayout(new FlowLayout()); 
    setSize(250, 250); 
    setLocationRelativeTo(null); 
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
    setVisible(true); 
} 

@Override 
public void actionPerformed(ActionEvent e) { 

    Object source = e.getSource(); 

    if (e.getSource() == button1) { 
     label1.setVisible(true); 
     field1.setText("Apple"); 
    } 

} 

} 

私は別のクラスファイルに私のmainメソッドを持っています。私が得るエラーは、label1.setVisible(true)につながります。

私はこれらのことを言うと私は見てきましたが、追加する必要のあるものがあるかどうか疑問に思っています。

答えて

5

ここでの問題のカップルがありました:

  • あなたlabel1がコンストラクタでJLabel labelを行うことによって隠されていたが。あなたは基本的に、コンストラクタにlabel1という別の変数を宣言しました。この変数はクラス自体に隠されています。
  • あなたのラベルは、起動時に表示されていた - 私はテストのためにlabel.setVisible(false)を使用していますが、それと変更コメントを外して、私は、イメージを持っていなかったように私も脇Imageの作成を置くそう

たい場合があります適切に。代わりにactionPerformedをオーバーライドする

import java.awt.*; 
import java.awt.event.*; 
import javax.swing.*; 

public class LearnAppMain extends JFrame implements ActionListener { 

// Define variables 
public JButton button1; 
public JLabel label1; 
    public JTextField field1; 

    private Image image1; 
private String apple = "apple.jpg"; 

public LearnAppMain() { 

    //ImageIcon image1 = new ImageIcon(this.getClass().getResource(apple)); 
    //JLabel label1 = new JLabel(image1); 
    label1 = new JLabel("hello"); 
    label1.setVisible(false); 

    button1 = new JButton("A"); 
    button1.addActionListener(this); 

    field1 = new JTextField(10); 

    // Create layout 
    setLayout(new FlowLayout()); 

    // create Container 
    final Container cn = getContentPane(); 

    cn.add(button1); 
    cn.add(field1); 
    cn.add(label1); 

    // setLayout(new FlowLayout()); 
    setSize(250, 250); 
    setLocationRelativeTo(null); 
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
    setVisible(true); 
} 

@Override 
public void actionPerformed(ActionEvent e) { 

    Object source = e.getSource(); 

    if (e.getSource() == button1) { 
     label1.setVisible(true); 
     field1.setText("Apple"); 
    } 

} 
public static void main(String[] args) { 
    new LearnAppMain(); 
} 
} 

私は別の(通常はインナークラス)を使用することをお勧めしたいActionListenerインスタンス:

はここで完全な作業バージョンです。例えば、あなたが興味を持っている場合、これは同様の例のために:

また、あなたは、より大きなアプリケーション(つまり、単なる実験やプロトタイピングのためではない)でこれを使用している場合は、すべてのSwingコードがあることを確認してくださいEDTで実行します。

通常、この目的ではSwingUtilities.invokeLaterを使用します。

これが役に立ちます。

+0

ありがとう、本当に助けになりました!私は間違いなくそれらのリンクを調べます。 –

+0

確かに、それはうまくいった! –

+1

@KeithKaplan:icyrockの投稿があなたを助けているなら、質問の隣にある上向きの矢印をクリックして投票してください(私がすでに行ったように)。問題が解決したら、その左側のチェックマークをクリックして*回答*として選択します。 –

0

最初に、画像自体をJLabelに追加しないでください。

だけ

ImageIcon image1 = new ImageIcon(this.getClass().getResource(apple)); 
JLabel label1 = new JLabel(""); 
label1.setVisible(true); 

が、その後のアクションで変更が ます。public void actionPerformedの(のActionEvent e)の{

if (e.getSource() == button1) 
{ 

    field1.seticon(image1); 
    field1.revalidate(); 
} 

を実施しない、それは間違いなく

に動作します。..オブジェクトを作成し、好きなことを残します
-1
clientDetail.addActionListener(new ActionListener(){ 
    public void actionPerformed (ActionEvent e){ 

     d.getContentPane().removeAll(); 
     g = new GridBagLayout(); 
     gc = new GridBagConstraints(); 

     d.setLayout(g); 
     JLabel message= new JLabel(" Message"); 
     addComponent(message,5,1,1,2); 
     JTextArea Message = new JTextArea(); 
     addComponent(Message,5,1,1,2); 

     d.setVisible(true); 

     d.setVisible(true); 
     d.pack(); 

     } 

     private void addComponent(Component component, int i, int i0, int i1, int i2) { 

     gc.gridx=i; 
     gc.gridy=i0; 
     gc.gridheight=i1; 
     gc.gridwidth=i2; 
     g.setConstraints(component, gc); 
     add(component); 

     } 


}); 

Recep.addActionListener(new ActionListener(){ 

    public void actionPerformed(ActionEvent e){ 

     } 

}); 
関連する問題