import javax.swing.*;
import java.awt.*;
import java.util.Arrays;
public class button
{
public static void main(String[] args) throws IOException
{
JFrame frame = new JFrame("作業GridLayout)");
GridLayout f1=new GridLayout(4, 13);
Container c=frame.getContentPane();
c.setLayout(f1);
int [] num = new int[16]; //宣告一個數字陣列,用來記錄產生過的亂數
Arrays.fill(num,-1); //將陣列內容全都設為-1
int i=1;
do{
int j=(int)(Math.random()*16);
if(num[j] == 0) continue; //判斷是否亂數取出的數字是否出現過
num[j] = 0;//將出現過的數字索引值的陣列內容設為0,代表出現過
c.add(new JButton("第"+j+"個"));
i++;
}while(i<=16);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(300,300);
frame.setVisible(true);
}
}
=============================================================
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class Calculator extends JFrame implements ActionListener{
public static void main(String[]args){
new Calculator();
}
public Calculator()
{
String s;
JFrame window=new JFrame("Calculator");
window.setDefaultLookAndFeelDecorated(true);
window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
window.setLayout(new GridLayout(3,3));
JPanel jplPanel = new JPanel();
jplPanel.setLayout(new GridLayout(3,3));
JButton button[]=new JButton[10];
//s=Integer.toString(1);
s=String.valueOf(1);
//System.out.println(s);
//s="1";
for (int i = 1; i <= 9; i++)
{
button[i] = new JButton();
button[i].setActionCommand(s);
button[i].addActionListener(this);
button[i].setSize(50,50);
button[i].setText(Integer.toString(i));
jplPanel.add(button[i]);
}
window.getContentPane().add(jplPanel, BorderLayout.CENTER);
window.setSize(500,500);
window.setVisible(true);
}
public void actionPerformed(ActionEvent e) {
String cmd = e.getActionCommand();
System.out.println(cmd);
if (cmd == "1") {
System.out.println("Yes....");
}
}
}
沒有留言:
張貼留言