-
April 5th, 2009, 12:02 PM
#1
HI Button problem
Hi all,
Basically my problem is that I've been able to create the buttons, but am unable to get any of the text to show up on the buttons, nor for the event listener to work. Any help would be greatly appreciated.
Thanks
Nate
import java.awt.Component;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class Test1
{
private static final int FRAME_WIDTH = 400 ;
private static final int FRAME_HEIGHT = 400 ;
public static void main(String[] args)
{
JFrame frame = new JFrame() ;
LifeButton[][] myButton = new LifeButton[10][10] ;
JPanel panel = new JPanel() ;
panel.setLayout( new GridLayout( 10, 10 ) ) ;
for( int i = 0 ; i < myButton.length ; i++ )
{
for( int k = 0 ; k < myButton.length ; k++ )
{
myButton[i][k] = new LifeButton( "O", false, i, k ) ;
panel.add( myButton[i][k] ) ;
}
}
frame.add( panel ) ;
frame.setSize( FRAME_WIDTH, FRAME_HEIGHT ) ;
frame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE ) ;
frame.setVisible( true ) ;
}
}
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
public class LifeButton extends JButton
{
private String text ;
private boolean value ;
private int x ;
private int y ;
public LifeButton( String text, boolean value, int x, int y )
{
System.out.println( "Initializing button " + x + " " + y ) ;
this.text = text ;
this.value = value ;
this.x = x ;
this.y = y ;
button() ;
}
public void button()
{
System.out.println( "in button " + x + " " + y ) ;
final JButton myButton = new JButton( text ) ;
class ClickListener implements ActionListener
{
public void actionPerformed( ActionEvent event )
{
System.out.println( "in button action " + x + " " + y ) ;
if( value )
{
value = false ;
text = "0" ;
}
else
{
value = true ;
text = "X" ;
}
myButton.setText( text ) ;
}
}
ActionListener listen = new ClickListener() ;
myButton.addActionListener( listen ) ;
}
}
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|