2012-02-21 71 views
4

我試圖讓JLabel在單擊JButton時出現。我添加了一個動作監聽器並將該組件添加到佈局中。我在actionPerformed中單擊JButton時使用label1.setVisible(true)。我仍然無法得到它的工作。可以看一下我的代碼嗎?設置JLabel在JButton被單擊時可見可見執行

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"); 
    } 

} 

} 

我有我的主要方法在另一個類文件。我得到的錯誤引導我到label1.setVisible(true);

我見過的每個問題他們都這樣說,但我想知道是否還有其他需要添加的東西。

回答

5

有幾個問題在這裏:

  • label1是由在構造函數中做JLabel label隱藏。你基本上在你的構造函數中聲明瞭另一個名爲label1的變量,該變量隱藏了類中的變量。
  • 你的標籤是可見的啓動 - 我用label.setVisible(false)的測試,但你可能想以其他方式

我也放Image創作一邊,因爲我沒有圖像,所以取消那和變化作爲適當的。

下面是一個完整的工作版本:

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實例,而不是覆蓋actionPerformed。見例如這對於一個類似的例子,如果你有興趣:

另外,如果你是在一個更大的應用程序中使用這個(即不只是實驗或原型),確保所有的Swing代碼在EDT上運行。

您通常爲此使用SwingUtilities.invokeLater

希望這會有所幫助。

+0

感謝這真的幫了大忙!我一定會考慮這些鏈接。 – 2012-02-21 02:30:45

+0

當然,很高興它解決了! – 2012-02-21 02:33:51

+1

@KeithKaplan:如果icyrock的帖子可以幫助您,請點擊問題旁邊的向上箭頭進行投票(正如我已經完成的那樣)。如果它解決了您的問題,請通過單擊左側的複選標記將其選爲*答案*。 – 2012-02-21 03:15:55

0

首先您不要將圖像先添加到JLabel。

剛剛創建的對象,並把它像..

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

然後做動作的修改進行 公共無效的actionPerformed(ActionEvent的五){

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){ 

     } 

});