2015/4/24

第七週 製作16個按鈕&按出其對應的數字

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(4,4));
JPanel jplPanel = new JPanel();
jplPanel.setLayout(new GridLayout(4,4));
JButton button[]=new JButton[17];
s=String.valueOf(1);
for (int i = 1; i <= 16; i++)
{
button[i] = new JButton();
button[i].setSize(50,50);
button[i].setText(Integer.toString(i));
button[i].setActionCommand("此按鈕為:"+i);
button[i].addActionListener(this);
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);
       
}}

2015/4/17

第六週 製作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()
{
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=String.valueOf(1);
for (int i = 1; i <= 9; i++)
{
button[i] = new JButton();
button[i].setSize(50,50);
button[i].setText(Integer.toString(i));
button[i].setActionCommand("此按鈕為:"+i);
button[i].addActionListener(this);
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);
     
}}



2015/4/10

第五週 製作按鈕跳出訊息&10個按鈕陣列

I. 製作按扭跳出訊息
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");

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!");
  }});

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);
jtfMainFrame.getContentPane().add(jplPanel, BorderLayout.CENTER);
jtfMainFrame.setVisible(true);
System.out.print("chih-yu hsu");
}
}

參考網站: http://www.javabeginner.com/java-swing/java-jbutton-class-example


II. 製作10個陣列
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class Calculator 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); JTextField jtfInput = new JTextField(20); JButton jbnButton1=new JButton("Button 1"); JButton jbnButton2=new JButton("Button 2"); JPanel jplPanel = new JPanel(); JPanel jplPane2 = new JPanel(); jplPanel.setLayout(new FlowLayout()); jplPanel.add(jtfInput); jplPanel.add(jbnButton1); jplPanel.add(jbnButton2); jtfMainFrame.getContentPane().add(jplPanel, BorderLayout.CENTER); jtfMainFrame.pack(); jtfMainFrame.setVisible(true); System.out.print("yan-ya,liou"); } }