鏈接:程式設計工藝大師
4*4 Button,點擊button會顯示對應的數字
程式代碼:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
class Test
{
public static void main(String[] args)
{
JFrame jtfMainFrame = new JFrame("Which Button Demo");
jtfMainFrame.setSize(600, 200);
JPanel jplPanel = new JPanel();
int i;
int j=1;
int num1 , num2;
JButton jbnButton[] = new JButton[16];
JButton changeButton[] = new JButton[3];
jplPanel.setLayout (new GridLayout(4, 4));
for(i = 0;i<=15;i++){
jbnButton[i]=new JButton("Button"+(i+1));
jplPanel.add(jbnButton[i]);
String s = Integer.toString(i+1);
jbnButton[i].addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
/*if(j<3){
changeButton[j] = jbnButton[i];
num1 = i;
j++;
}
else if(j==3){
changeButton[j]=jbnButton[i];
jbnButton[num1] = changeButton[j];
jbnButton[i] = changeButton[j-1];
j=1;
}*/
//j++;
//JButton btn = (JButton[i])e.getSourec();
//lab.setText(btn.getLabel+".");
System.out.println("Button"+s);
}
});
}
jtfMainFrame.getContentPane().add(jplPanel, BorderLayout.CENTER);
jtfMainFrame.setVisible(true);
}
}
2015年4月24日 星期五
2015年4月17日 星期五
第七週 - 作業
鏈接:程式設計工藝大師
程式代碼:
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()
{
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];
for(int i=1;i<=9;i++)
{
button[i] = new JButton();
//button[i].setActionCommand(i);
button[i].addActionListener(this);
button[i].setSize(50,50);
button[i].setText(Integer.toString(i));
jplPanel.add(button[i]);
}
button[1].setActionCommand("1");
button[2].setActionCommand("2");
button[3].setActionCommand("3");
button[4].setActionCommand("4");
button[5].setActionCommand("5");
button[6].setActionCommand("6");
button[7].setActionCommand("7");
button[8].setActionCommand("8");
button[9].setActionCommand("9");
window.getContentPane().add(jplPanel, BorderLayout.CENTER);
window.setSize(500,500);
window.setVisible(true);
}
public void actionPerformed(ActionEvent e){
String cmd = e.getActionCommand();
if (cmd == "1"){
System.out.println(1);
}
if (cmd == "2"){
System.out.println(2);
}
if (cmd == "3"){
System.out.println(3);
}
if (cmd == "4"){
System.out.println(4);
}
if (cmd == "5"){
System.out.println(5);
}
if (cmd == "6"){
System.out.println(6);
}
if (cmd == "7"){
System.out.println(7);
}
if (cmd == "8"){
System.out.println(8);
}
if (cmd == "9"){
System.out.println(9);
}
}
}
程式代碼:
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()
{
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];
for(int i=1;i<=9;i++)
{
button[i] = new JButton();
//button[i].setActionCommand(i);
button[i].addActionListener(this);
button[i].setSize(50,50);
button[i].setText(Integer.toString(i));
jplPanel.add(button[i]);
}
button[1].setActionCommand("1");
button[2].setActionCommand("2");
button[3].setActionCommand("3");
button[4].setActionCommand("4");
button[5].setActionCommand("5");
button[6].setActionCommand("6");
button[7].setActionCommand("7");
button[8].setActionCommand("8");
button[9].setActionCommand("9");
window.getContentPane().add(jplPanel, BorderLayout.CENTER);
window.setSize(500,500);
window.setVisible(true);
}
public void actionPerformed(ActionEvent e){
String cmd = e.getActionCommand();
if (cmd == "1"){
System.out.println(1);
}
if (cmd == "2"){
System.out.println(2);
}
if (cmd == "3"){
System.out.println(3);
}
if (cmd == "4"){
System.out.println(4);
}
if (cmd == "5"){
System.out.println(5);
}
if (cmd == "6"){
System.out.println(6);
}
if (cmd == "7"){
System.out.println(7);
}
if (cmd == "8"){
System.out.println(8);
}
if (cmd == "9"){
System.out.println(9);
}
}
}
2015年4月16日 星期四
期中程式
鏈接:程式設計工藝大師
程式內容:工程計算器
程式代碼:
import java.lang.*;
import java.util.Scanner;
public class calculator {
public static void main(String args[]) {
// Java.util.Scanner
Scanner keyin = new Scanner(System.in);
double value, ans=0;
int select;
disp_men ();
//keyin nextInt()
select = keyin.nextInt();
while ( select != 7) {
System.out.println("輸入一個數值 (注意三角函數範圍) =>");
value = keyin.nextDouble();
switch(select){
case 1:
ans = Math.log(value);
System.out.printf("log(%,2f) = %.2f\n", value, ans);
break;
case 2:
ans = Math.log10(value);
System.out.printf("log10(%,2f) = %.2f\n", value, ans);
break;
case 3:
ans = Math.sqrt(value);
System.out.printf("sqrt(%,2f) = %.2f\n", value, ans);
break;
case 4:
ans = Math.sin(value);
System.out.printf("sin(%,2f) = %.2f\n", value, ans);
break;
case 5:
ans = Math.cos(value);
System.out.printf("cos(%,2f) = %.2f\n", value, ans);
break;
case 6:
ans = Math.tan(value);
System.out.printf("tan(%,2f) = %.2f\n", value, ans);
break;
default:
System.out.printf("sin(%,2f) = %.2f\n", value, ans);
}
disp_men();
select = keyin.nextInt();
}
System.out.print("== 謝謝使用 ==\n");
}
static void disp_men() {
System.out.printf("\n*** 工程計算器提供有下列功能 ***\n");
System.out.printf("(1) log 函數\t (2) log10() 函數\n");
System.out.printf("(3) sqrt 函數\t (2) sin() 函數\n");
System.out.printf("(5) cos 函數\t (2) tan() 函數\n");
System.out.printf("(7) 離開系統");
System.out.printf("請選擇輸入 =>");
}
}
程式內容:工程計算器
程式代碼:
import java.lang.*;
import java.util.Scanner;
public class calculator {
public static void main(String args[]) {
// Java.util.Scanner
Scanner keyin = new Scanner(System.in);
double value, ans=0;
int select;
disp_men ();
//keyin nextInt()
select = keyin.nextInt();
while ( select != 7) {
System.out.println("輸入一個數值 (注意三角函數範圍) =>");
value = keyin.nextDouble();
switch(select){
case 1:
ans = Math.log(value);
System.out.printf("log(%,2f) = %.2f\n", value, ans);
break;
case 2:
ans = Math.log10(value);
System.out.printf("log10(%,2f) = %.2f\n", value, ans);
break;
case 3:
ans = Math.sqrt(value);
System.out.printf("sqrt(%,2f) = %.2f\n", value, ans);
break;
case 4:
ans = Math.sin(value);
System.out.printf("sin(%,2f) = %.2f\n", value, ans);
break;
case 5:
ans = Math.cos(value);
System.out.printf("cos(%,2f) = %.2f\n", value, ans);
break;
case 6:
ans = Math.tan(value);
System.out.printf("tan(%,2f) = %.2f\n", value, ans);
break;
default:
System.out.printf("sin(%,2f) = %.2f\n", value, ans);
}
disp_men();
select = keyin.nextInt();
}
System.out.print("== 謝謝使用 ==\n");
}
static void disp_men() {
System.out.printf("\n*** 工程計算器提供有下列功能 ***\n");
System.out.printf("(1) log 函數\t (2) log10() 函數\n");
System.out.printf("(3) sqrt 函數\t (2) sin() 函數\n");
System.out.printf("(5) cos 函數\t (2) tan() 函數\n");
System.out.printf("(7) 離開系統");
System.out.printf("請選擇輸入 =>");
}
}
2015年4月9日 星期四
第六週 - 作業
來源:程式設計工藝大師
程式代碼:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
class Test
{
public static void main(String[] args)
{
JFrame jtfMainFrame = new JFrame("Which Button Demo");
jtfMainFrame.setSize(450, 150);
JButton jbnButton1 = new JButton("Button 1");
JButton jbnButton2 = new JButton("Button 2");
JButton jbnButton3 = new JButton("Button 3");
JButton jbnButton4 = new JButton("Button 4");
JButton jbnButton5 = new JButton("Button 5");
JButton jbnButton6 = new JButton("Button 6");
JButton jbnButton7 = new JButton("Button 7");
JButton jbnButton8 = new JButton("Button 8");
JButton jbnButton9 = new JButton("Button 9");
JButton jbnButton10 = new JButton("Button 10");
jbnButton1.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
System.out.println("Button 1!");
}
});
jbnButton2.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
System.out.println("Button 2!");
}
});
jbnButton3.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
System.out.println("Button 3!");
}
});
jbnButton4.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
System.out.println("Button 4!");
}
});
jbnButton5.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
System.out.println("Button 5!");
}
});
jbnButton6.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
System.out.println("Button 6!");
}
});
jbnButton7.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
System.out.println("Button 7!");
}
});
jbnButton8.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
System.out.println("Button 8!");
}
});
jbnButton9.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
System.out.println("Button 9!");
}
});
jbnButton10.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
System.out.println("Button 10!");
}
});
JPanel jplPanel = new JPanel();
jplPanel.add(jbnButton1);
jplPanel.add(jbnButton2);
jplPanel.add(jbnButton3);
jplPanel.add(jbnButton4);
jplPanel.add(jbnButton5);
jplPanel.add(jbnButton6);
jplPanel.add(jbnButton7);
jplPanel.add(jbnButton8);
jplPanel.add(jbnButton9);
jplPanel.add(jbnButton10);
jtfMainFrame.getContentPane().add(jplPanel, BorderLayout.CENTER);
jtfMainFrame.setVisible(true);
System.out.println("chih-yu hsu");
}
}
以迴圈形式建立Button
程式代碼:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class Test extends JFrame{
public static void main(String[]args){
JFrame window=new JFrame("Calculator");
JPanel jplPanel = new JPanel();
JButton button[]=new JButton[10];
for(int i=0;i<button.length;i++){
button[i] = new JButton();
button[i].setSize(50,50);
button[i].setText(Integer.toString(i+1));
jplPanel.add(button[i]);
}
window.getContentPane().add(jplPanel, BorderLayout.CENTER);
window.setSize(500,500);
window.setVisible(true);
}}
程式代碼:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
class Test
{
public static void main(String[] args)
{
JFrame jtfMainFrame = new JFrame("Which Button Demo");
jtfMainFrame.setSize(450, 150);
JButton jbnButton1 = new JButton("Button 1");
JButton jbnButton2 = new JButton("Button 2");
JButton jbnButton3 = new JButton("Button 3");
JButton jbnButton4 = new JButton("Button 4");
JButton jbnButton5 = new JButton("Button 5");
JButton jbnButton6 = new JButton("Button 6");
JButton jbnButton7 = new JButton("Button 7");
JButton jbnButton8 = new JButton("Button 8");
JButton jbnButton9 = new JButton("Button 9");
JButton jbnButton10 = new JButton("Button 10");
jbnButton1.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
System.out.println("Button 1!");
}
});
jbnButton2.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
System.out.println("Button 2!");
}
});
jbnButton3.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
System.out.println("Button 3!");
}
});
jbnButton4.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
System.out.println("Button 4!");
}
});
jbnButton5.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
System.out.println("Button 5!");
}
});
jbnButton6.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
System.out.println("Button 6!");
}
});
jbnButton7.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
System.out.println("Button 7!");
}
});
jbnButton8.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
System.out.println("Button 8!");
}
});
jbnButton9.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
System.out.println("Button 9!");
}
});
jbnButton10.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
System.out.println("Button 10!");
}
});
JPanel jplPanel = new JPanel();
jplPanel.add(jbnButton1);
jplPanel.add(jbnButton2);
jplPanel.add(jbnButton3);
jplPanel.add(jbnButton4);
jplPanel.add(jbnButton5);
jplPanel.add(jbnButton6);
jplPanel.add(jbnButton7);
jplPanel.add(jbnButton8);
jplPanel.add(jbnButton9);
jplPanel.add(jbnButton10);
jtfMainFrame.getContentPane().add(jplPanel, BorderLayout.CENTER);
jtfMainFrame.setVisible(true);
System.out.println("chih-yu hsu");
}
}
以迴圈形式建立Button
程式代碼:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class Test extends JFrame{
public static void main(String[]args){
JFrame window=new JFrame("Calculator");
JPanel jplPanel = new JPanel();
JButton button[]=new JButton[10];
for(int i=0;i<button.length;i++){
button[i] = new JButton();
button[i].setSize(50,50);
button[i].setText(Integer.toString(i+1));
jplPanel.add(button[i]);
}
window.getContentPane().add(jplPanel, BorderLayout.CENTER);
window.setSize(500,500);
window.setVisible(true);
}}
訂閱:
文章 (Atom)