取代
註解掉很多
// AWT, JButton類別
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class swingtest extends JFrame implements ActionListener
{
static JButton bt1=new JButton("RUN"); // 建立按鈕物件
static JTextField tf1=new JTextField("1");
/*
static JTextField tf1=new JTextField("1");
static JTextField tf2=new JTextField("2");
static JTextField tf3=new JTextField("3");
static JTextField tf4=new JTextField("4");
static JTextField tf5=new JTextField("5");
static JTextField tf6=new JTextField("6");
static JTextField tf7=new JTextField("7");
*/
public static void main(String args[])
{
swingtest fm=new swingtest();
//BorderLayout bd=new BorderLayout();
//fm.setLayout(bd);
GridLayout gl= new GridLayout(3,3);
JPanel p1 = new JPanel(gl);
JPanel p2 = new JPanel(gl);
String numbers[] = {"0", "1", "2", "3", "4", "5", "6", "7", "8"};
JButton buttons[]=new JButton[10];
fm.setLayout(gl);
fm.setSize(300,225);
fm.add(p1);
fm.add(p2);
p2.add(tf1, gl);
p2.add(bt1, gl);
for (int i = 0; i < numbers.length; i++)
{
buttons[i] = new JButton(numbers[i]);
p1.add(buttons[i], gl);
}
fm.setVisible(true);
bt1.addActionListener(fm);
/*
fm.add(bt1); // 在視窗內加入按鈕
fm.add(tf1);
fm.add(tf2);
fm.add(tf3);
fm.add(tf4);
fm.add(tf5);
fm.add(tf6);
fm.add(tf7);
*/
}
public void actionPerformed(ActionEvent e)
{
String s1;
s1=tf1.getText();
System.out.println(s1);
}
/*
public void actionPerformed(ActionEvent e)
{
int r1,r2,r3,r4,r5,r6,r7;
r1=(int) (Math.random()*49)+1;
r2=(int) (Math.random()*49)+1;
r3=(int) (Math.random()*49)+1;
r4=(int) (Math.random()*49)+1;
r5=(int) (Math.random()*49)+1;
r6=(int) (Math.random()*49)+1;
r7=(int) (Math.random()*49)+1;
String sv1 = Integer.toString(r1);
String sv2 = Integer.toString(r2);
String sv3 = Integer.toString(r3);
String sv4 = Integer.toString(r4);
String sv5 = Integer.toString(r5);
String sv6 = Integer.toString(r6);
String sv7 = Integer.toString(r7);
tf1.setText(sv1);
tf2.setText(sv2);
tf3.setText(sv3);
tf4.setText(sv4);
tf5.setText(sv5);
tf6.setText(sv6);
tf7.setText(sv7);
}
*/
}
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class swingtest extends JFrame implements ActionListener
{
static JButton bt1=new JButton("RUN"); // 建立按鈕物件
static JTextField tf1=new JTextField("1");
/*
static JTextField tf1=new JTextField("1");
static JTextField tf2=new JTextField("2");
static JTextField tf3=new JTextField("3");
static JTextField tf4=new JTextField("4");
static JTextField tf5=new JTextField("5");
static JTextField tf6=new JTextField("6");
static JTextField tf7=new JTextField("7");
*/
public static void main(String args[])
{
swingtest fm=new swingtest();
//BorderLayout bd=new BorderLayout();
//fm.setLayout(bd);
GridLayout gl= new GridLayout(3,3);
JPanel p1 = new JPanel(gl);
JPanel p2 = new JPanel(gl);
String numbers[] = {"0", "1", "2", "3", "4", "5", "6", "7", "8"};
JButton buttons[]=new JButton[10];
fm.setLayout(gl);
fm.setSize(300,225);
fm.add(p1);
fm.add(p2);
p2.add(tf1, gl);
p2.add(bt1, gl);
for (int i = 0; i < numbers.length; i++)
{
buttons[i] = new JButton(numbers[i]);
p1.add(buttons[i], gl);
}
fm.setVisible(true);
bt1.addActionListener(fm);
/*
fm.add(bt1); // 在視窗內加入按鈕
fm.add(tf1);
fm.add(tf2);
fm.add(tf3);
fm.add(tf4);
fm.add(tf5);
fm.add(tf6);
fm.add(tf7);
*/
}
public void actionPerformed(ActionEvent e)
{
String s1;
s1=tf1.getText();
System.out.println(s1);
}
/*
public void actionPerformed(ActionEvent e)
{
int r1,r2,r3,r4,r5,r6,r7;
r1=(int) (Math.random()*49)+1;
r2=(int) (Math.random()*49)+1;
r3=(int) (Math.random()*49)+1;
r4=(int) (Math.random()*49)+1;
r5=(int) (Math.random()*49)+1;
r6=(int) (Math.random()*49)+1;
r7=(int) (Math.random()*49)+1;
String sv1 = Integer.toString(r1);
String sv2 = Integer.toString(r2);
String sv3 = Integer.toString(r3);
String sv4 = Integer.toString(r4);
String sv5 = Integer.toString(r5);
String sv6 = Integer.toString(r6);
String sv7 = Integer.toString(r7);
tf1.setText(sv1);
tf2.setText(sv2);
tf3.setText(sv3);
tf4.setText(sv4);
tf5.setText(sv5);
tf6.setText(sv6);
tf7.setText(sv7);
}
*/
}
執行圖
沒有留言:
張貼留言