- #1
FritoTaco
- 132
- 23
I don't know how to display what the string you see in the array. Do I need to put something in the paint method? It's annoying because my book and everywhere I look only show examples using integers and not strings.
This code is in the init method. I can't find out where to display the "dog", "cat", etc into the applet itself.
This code is in the init method. I can't find out where to display the "dog", "cat", etc into the applet itself.
Code:
String [][] name = {{"dog"}, {"cat"}, {"bridge"}, {"lake"}};
Code:
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import java.util.Arrays;
import java.applet.*;
import java.awt.Graphics;
public class DictionaryTest extends JApplet implements ActionListener
{
JLabel label;
//declaring and creating array of objects/buttons using intiailizer list
TextField tf;
JButton[ ] mybuttons =
{
new JButton( "one" ),
new JButton( "two" ),
new JButton( "three" ),
new JButton( "four" )
};
public void init( )
{
setLayout( new FlowLayout( ) );
tf = new TextField(15);
add(tf);
String [][] name = {{"dog"}, {"cat"}, {"bridge"}, {"lake"}};
for( int x=0; x < mybuttons.length; x++ )
{
mybuttons[x].addActionListener(this);
add( mybuttons[x] );
}
}
public void paint(Graphics g){
super.paint(g);
}
public void actionPerformed( ActionEvent ae )
{
Object obj = ae.getSource( );
if( obj == mybuttons[0] )
{
label.setText( "first button" );
}
else if ( obj == mybuttons[1] )
{
label.setText( "second button" );
}
else if( obj == mybuttons[2] )
{
label.setText( "third button" );
}
else if( obj == mybuttons[3] )
{
label.setText( "fourth button" );
}
}
}