2014-09-19 51 views
0

單擊按鈕時,我想顯示標籤。我正在使用Eclipse Juno。我有標籤添加和設置可見部分...在Java swing中單擊按鈕後顯示標籤

wLabel = new JLabel("YOu and Me"); 
    wLabel .setVisible(false); 
    wLabel .setBounds(80, 35, 100, 25); 
    wLabel .setFont(new Font("Meiryo", Font.PLAIN, 9)); 
    wLabel .setForeground(new Color(255, 102, 21)); 
    add(wLabel); 

按鈕

wButton = new JButton("W"); 
    wButton .setActionCommand("myButton"); 
    wButton .addActionListener(this); 
    wButton .setFont(new Font("Meiryo UI", Font.PLAIN, 11)); 
    wButton .setBounds(10, 33, 70, 35); 
    wButton .setBackground(new Color(102, 51, 20)); 
    add(wButton); 

而這裏的actionPerformed。我已經實現的ActionListener

public void actionPerformed(ActionEvent e) { 
    // TODO Auto-generated method stub 
    if (e.getActionCommand().equals("myButton")) { 
     wLabel.setVisible(true); 

    } 
} 
+0

wButton =新的JButton( 「W」); \t \t wButton.setActionCommand(「myButton」); \t \t wButton.setFont(new Font(「Meiryo UI」,Font.PLAIN,11)); \t \t wButton.setBounds(10,32,80,30); \t \t wButton.setBackground(new Color(102,51,0)); \t \t add(wButton); \t \t wButton.addActionListener(新的ActionListener(){ \t \t \t公共無效的actionPerformed(ActionEvent的五){ \t \t \t \t如果(e.getActionCommand()。等於( 「myButton的」)){ \t \t \t \t \t wLabel.setVisible(true); \t \t \t} \t \t}}); – JumboJey 2014-09-19 10:50:31

+0

如果我這樣做, – JumboJey 2014-09-19 10:50:48

+0

嘗試將例如System.out.println()檢查是否調用方法。如果它不起作用,請嘗試刪除'if(..)'語句 – 2014-09-19 10:57:53

回答

0

最初,你可以設置能見度爲false您的標籤,然後點擊按鈕設置像label.setVisible(true)

用於例如知名度後像這樣,注意我用蘭巴語法對Java 8

import java.awt.Dimension; 
import javax.swing.JButton; 
import javax.swing.JFrame; 
import javax.swing.JLabel; 
import javax.swing.JPanel; 

public class BtnDisabled { 

    public static void main(String[] args) { 

     JFrame frame = new JFrame(""); 
     JLabel label = new JLabel("You and Me"); 
     label.setVisible(false); 
     JPanel panel = new JPanel(); 
     panel.add(label); 

     JButton btn = new JButton("W"); 
    btn.addActionListener(e -> { 
     if (!label.isVisible()) { 
      label.setVisible(true); 
     } 
    }); 
     panel.add(btn); 
     frame.add(panel); 
     frame.setSize(new Dimension(500, 500)); 

     frame.setVisible(true); 
    } 
} 
+0

我不使用JFrame。我也使用maven和WindowBuilder編輯器。我在WindowBuilder中測試。我對WBe很陌生。 – JumboJey 2014-09-19 11:04:26

+0

替換代碼中的行「wButton .addActionListener(this);」與wButton。addActionListener方法(新的ActionListener(){ \t \t \t @覆蓋 \t \t \t公共無效的actionPerformed(ActionEvent的五){ \t \t \t \t如果(!wlabel.isVisible()){ \t \t \t \t \t wlabel.setVisible(真); \t \t \t \t} \t \t \t} \t \t}); – sol4me 2014-09-19 11:10:25

+1

'我不使用JFrame.'然後,你在使用什麼? JLabel必須繼承JPanel,並且該JPanel必須位於頂級容器(例如JFrame)上。 – splungebob 2014-09-19 13:39:14

0

添加的ActionListener到您的按鈕:

wButton.addActionListener(new ActionListener() { 
    public void actionPerformed(ActionEvent e) { 
     //Execute when button is pressed 
     wLabel.setVisible(true); 
    } 
}); 

或者更好的做法(我認爲)是創建一個實現ActionListener的單獨的類。但結果將是相同的。我不知道你的應用程序有多大,但我建議分開ActionListeners(就像我提到的單獨的類),只是爲了讓你的代碼清晰。

+0

我試過了,它不起作用。 – JumboJey 2014-09-19 10:53:14

+0

最有可能的問題不在按鈕中,這個setVisible。甚至在開始添加標籤並寫入setVisible(false)時,我仍然可以看到它。我擴展了JPanel,所以我沒有使用單獨的JFrame – JumboJey 2014-09-19 11:05:57

+0

請發佈更多的代碼,因爲我爲測試編寫的示例應用程序,它適用於我。我想在別的地方一定有問題。 – 2014-09-19 11:09:11

0
public NewJFrame() { 
    initComponents(); 
    jLabel1.setVisible(false); 
} 

private void initComponents(){ 
    jLabel1 = new javax.swing.JLabel(); 
    jButton1 = new javax.swing.JButton(); 

    setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE); 

    jLabel1.setText("jLabel1"); 

    jButton1.setText("jButton1"); 
    jButton1.addActionListener(new java.awt.event.ActionListener() { 
     public void actionPerformed(java.awt.event.ActionEvent evt) { 
      jButton1ActionPerformed(evt); 
     } 
    });} 




    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {           
    jLabel1.setVisible(true); 
} 

這段代碼爲我工作,這裏NewJFrame()是構造