2014-01-21 60 views
0
import java.awt.FlowLayout; 
import java.awt.event.ActionListener; 
import java.awt.event.ActionEvent; 
import javax.swing.JFrame; 
import javax.swing.JButton; 
import javax.swing.Icon; 
import javax.swing.ImageIcon; 
import javax.swing.JOptionPane; 

public class GUI extends JFrame { 

private JButton reg; 
private JButton custom; 
private JButton custom2; 
public GUI(){ 
    super("Heartstone Arena Alpha 0.01"); 
    setLayout(new FlowLayout()); 

    reg = new JButton("Click Me"); 
    add(reg); 

    Icon ACn = new ImageIcon(getClass().getResource("463.png")); 
    Icon ACg = new ImageIcon(getClass().getResource("463 (1).png")); 
    custom = new JButton("Custom", ACn); 
    custom.setRolloverIcon(ACg); 
    add(custom); 

    HandlerClass handler = new HandlerClass(); 
    reg.addActionListener(handler); 
    custom.addActionListener(handler); 


} 

private class HandlerClass implements ActionListener { 
    public void actionPerformed(ActionEvent event) { 




     Icon An = new ImageIcon(getClass().getResource("Alexstrasza(303).png")); 
     custom2 = new JButton(" ", An); 
     custom2.setIcon(An); 
     custom2.setRolloverIcon(An); 





    } 
} 

這裏更換按鈕的代碼,我想要做的是有它的點擊時CUSTOM2更換定製。 我怎麼會去這件事嗎? 我嘗試使用定製=無效,然後添加(CUSTOM2);但它不顯示 PS:忽略註冊按鈕與另一個按鈕

+4

您可以使用不同的聽衆和文本一個按鈕進行評論。但是,如果不適合,看['CardLayout'(http://docs.oracle.com/javase/7/docs/api/java/awt/CardLayout.html)所看到的這個[簡短的例子] (http://stackoverflow.com/a/5786005/418556)。 –

+0

刪除自定義/清潔框架 - >添加自定義2 - >重繪框架 – lebryant

+0

@lebryant不要忘了驗證和或包框架或更好的,只是沒有提及hackish的方法,除非必要(與卡片佈局 - 它沒有必要*)。 –

回答

0

您需要添加一個ActionListener到您的按鈕,這應該使第一個按鈕不可見,第二個按鈕可見。因此,它並沒有真正被摧毀,你只是不表現出來。
像這樣:

public class YourClassName implements ActionListener { 

    private JButton button1; 
    private JButton button2; 

    public YourClassName() { 

    // Code Snippet ---------------- 

    button1 = new JButton("Click to replace"); 
    button1.addActionListener(this); 
    // implement code for resizing and positioning here 

    button2 = new JButton("I am new here"); 
    button2.setVisible(false); 
    // implement code for resizing and positioning here 

    // ... 

    } 

    @Override 
    public void actionPerformed(ActionEvent e) { 
    if(e.getSource() == button1) { 
     button1.setVisible(false); 
     button2.setvisible(true); 
    } 
    } 
} 

注:該代碼是由我的頭了,有可能是在它的錯誤。此外,它只是一個片段,隨時爲完整的代碼