2011年4月7日 星期四

JAVA-18

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class Test6 extends JFrame implements ActionListener
{
static TextField tbx1 = new TextField(2);
static JButton buttons[] = new JButton[10];
public static void main(String args[])
{
Test6 frame = new Test6();
FlowLayout flow = new FlowLayout();
String numbers[]={"0","1","2","3","4","5","6","7","8"};
GridLayout border1 = new GridLayout(3,3);
GridLayout border2 = new GridLayout(0,2);
frame.setLayout(border2);
frame.setSize(300,300);
JPanel p1= new JPanel(border1);
for (int i=0; i{
buttons[i] = new JButton(numbers[i]);
p1.add(buttons[i],border1);
}
frame.add(p1);
JPanel p2 = new JPanel(flow);
JButton button = new JButton("JButton 1");
button.addActionListener(frame);
p2.add(tbx1);
p2.add(button);
frame.add(p2);
frame.setVisible(true);
}
public void actionPerformed(ActionEvent e)
{
String stringValue;
stringValue = tbx1.getText();
int intValue = Integer.parseInt(stringValue);
System.out.println(intValue);
buttons[intValue].setBackground(Color.black);
}
}

沒有留言:

張貼留言