2016-06-29 9 views
-2

私が作っているゲームでは、プレイヤーに2つ(またはそれ以上)のボタンを選択させ、ボタンをクリックすると新しい画面が表示されます。クリック後のJButtonのテキストの削除

ただし、現在のところ、1番目のボタンのテキストはクリックされたままです。私は、リスナーでsetVisible(false)を試してから、後でsetText( "")を実行しましたが、どちらもエラーを作成するだけです。誰かが私に他の助言を与えることができれば、私は大いに感謝しています。私は参照用に私のゲームのパネル全体を持っていますが、B1Listenerだけがこの問題に関連しているはずです。

import javax.swing.*; 
import java.awt.*; 
import java.awt.event.*; 
import java.awt.Graphics; 
import java.awt.Image; 
import java.awt.image.BufferedImage; 
import javax.swing.Timer; 


public class Panel00 extends JPanel 
{ 
    private BufferedImage myImage; 
    private Graphics myBuffer; 
    public Timer timer; 
    public JButton button1; 
    public JButton button2; 
    public JLabel label1 = new JLabel("Good Choice!"); 
    public JLabel label2 = new JLabel("You're Fired!!"); 
    public int x = 10;  //CountDown from 100  
    public int delay = 1000; //milliseconds 
    boolean drawWorld = false; 



    public Panel00() 
    { 

    myImage = new BufferedImage(1000, 1000, BufferedImage.TYPE_INT_RGB); 
    myBuffer = myImage.getGraphics(); 




    setLayout(null); 


    JButton button1 = new JButton(); 
    button1.setSize(300, 200); 
    button1.setLocation(100,150); 
    button1.setFont(new Font("Serif", Font.BOLD, 18)); 
    button1.setText("<html><center>"+"Until we are able to determine and understand this problem"+"<br>"+" and the dangerous threat it poses, our country cannot be the victims of horrendous attacks"+"<br>"+"by people that believe only in Jihad, and have no sense of reason or respect for human life"+"</center></html>"); 
    button1.addActionListener(new B1Listener()); 
    button1.setBorder(null); 
    button1.setOpaque(false); 
    button1.setContentAreaFilled(false); 
    button1.setBorderPainted(false); 
    add(button1); 

    JButton button2 = new JButton(); 
    button2.setSize(300, 200); 
    button2.setLocation(600,150); 
    button2.setFont(new Font("Serif", Font.BOLD, 18)); 
    button2.setText("<html><center>"+"If ISIS wants to fight, fine with us. "+"<br>"+"We have wanted that fight for a long time. There is no room in the world for ISIS any more."+"<br>"+"The Muslims or us, one of us will have to go."+"</center></html>"); 
    button2.addActionListener(new B2Listener()); 
    button2.setBorder(null); 
    button2.setOpaque(false); 
    button2.setContentAreaFilled(false); 
    button2.setBorderPainted(false); 
    add(button2); 

    ActionListener counter = 
    new ActionListener() 
    { 
     public void actionPerformed(ActionEvent evt) 
     { 
      repaint(); 
      x--; 
      if (x == 0) 
      { 
       timer.stop(); 
      } 
     } 
    }; 
    timer = new Timer(delay, counter); 
    timer.start(); 


    setFocusable(true); 
    } 


    public void paintComponent(Graphics g) 
    { 
    ImageIcon Nintendo = new ImageIcon("trumpT.jpg"); 

    g.drawImage(Nintendo.getImage(), 0, 0, 1000, 1000, null); 

    ImageIcon N = new ImageIcon("trump speech.jpg"); 

    g.setColor(Color.WHITE); 
    g.fillOval(90,100,320,320); 

    g.setColor(Color.WHITE); 
    g.fillOval(590,100,320,320); 

    g.setColor(Color.WHITE); 
    g.setFont(new Font("Serif",Font.BOLD, 50)); 
    g.drawString(""+x,500,50); 

    if (drawWorld) 
    { 
    g.drawImage(N.getImage(), 0, 0, 1000, 1000, null); 
    } 
} 

    private class B1Listener implements ActionListener 
    { 
    public void actionPerformed(ActionEvent e) 
    { 
    repaint(); 
    drawWorld = true; 

    label1.setLocation(500,700); 
    label1.setSize(400, 400); 
    label1.setForeground(new Color(212, 175, 55)); 
    label1.setFont(new Font("Serif", Font.BOLD, 40)); 
    add(label1); 

    button1.setText(""); 

    timer.stop(); 

    } 
} 

    private class B2Listener implements ActionListener 
    { 
    public void actionPerformed(ActionEvent e) 
    { 
    label2.setLocation(500,700); 
    label2.setSize(400, 400); 
    label2.setForeground(Color.RED); 
    label2.setFont(new Font("Serif", Font.BOLD, 40)); 
    add(label2); 
    timer.stop(); 
    } 
} 
} 
+1

JButton button1 = new JButton(); ... JButton button2 = new JButton(); 

を変更? – PabloJ

+0

ボタンを宣言した後、同じスコープでイベントリスナーを設定する必要があります。これを行うには、@ControlAltDelで提案されているように、匿名の内部クラスを使用できます –

答えて

0

button1.addActionListener(new ActionListener() { 
    public void actionPerformed(ActionEvent ae) { 
    button1.setText(""); 
    } 
}); 
0

あなたPanel00コンストラクタがnullのままインスタンス変数Button1を、隠しローカルButton1を作成するように、匿名の内部クラスを使用することをお勧め。そして、あなたはbutton2で同じことをやっています。あなたのコンストラクタで

、それはあなたを与えるんエラーはどのような

button1 = new JButton(); 
    ... 
    button2 = new JButton(); 
関連する問題