//Swing, Button Class
import java.awt.*;
import java.awt.event.*;
import javax.swing.JFrame;
public class SwingTest extends JFrame
{
static JFrame myfrm=new JFrame("Button class"); // Java Class JFrame
static Button btn1=new Button("Button 1"); // 建立1按鈕物件
static Button btn2=new Button("Button 2"); // 建立2按鈕物件
static TextField tbx1=new TextField("TextField 1"); // 建立1文字方塊物件
public static void main(String args[])
{
BorderLayout border=new BorderLayout();
myfrm.setLayout(border);
myfrm.setSize(250,150);
myfrm.add(btn1, border.EAST); // 在視窗內加入按鈕1
myfrm.add(btn2, border.CENTER); // 在視窗內加入按鈕2
myfrm.add(tbx1, border.WEST); // 在視窗內加入按鈕2
myfrm.setVisible(true);
}
public void actionPerformed(ActionEvent e)
{
int rn;
rn=(int) (Math.random()*49) ;
System.out.println(rn );
}
}
再來將Button也改成JButton
將TextField也改成JTextField
//Swing, JButton Class
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class SwingTest extends JFrame
{
static JFrame myfrm=new JFrame("JButton class"); // Java Class JFrame
static JButton btn1=new JButton("JButton 1"); // 建立1按鈕物件
static JButton btn2=new JButton("JButton 2"); // 建立2按鈕物件
static JTextField tbx1=new JTextField("JTextField 1"); // 建立1文字方塊物件
public static void main(String args[])
{
BorderLayout border=new BorderLayout();
myfrm.setLayout(border);
myfrm.setSize(250,150);
myfrm.add(btn1, border.EAST); // 在視窗內加入按鈕1
myfrm.add(btn2, border.CENTER); // 在視窗內加入按鈕2
myfrm.add(tbx1, border.WEST); // 在視窗內加入按鈕2
myfrm.setVisible(true);
}
public void actionPerformed(ActionEvent e)
{
int rn;
rn=(int) (Math.random()*49) ;
System.out.println(rn );
}
}
改變按鈕的排列
加入GridLayout
//Swing, JButton Class
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class SwingTest extends JFrame
{
static JFrame myfrm=new JFrame("JButton class"); // Java Class JFrame
static JButton btn1=new JButton("JButton 1"); // 建立1按鈕物件
static JButton btn2=new JButton("JButton 2"); // 建立2按鈕物件
static JTextField tbx1=new JTextField("JTextField 1"); // 建立1文字方塊物件
public static void main(String args[])
{
//BorderLayout border=new BorderLayout();
GridLayout border=new GridLayout(0,2);
myfrm.setLayout(border);
myfrm.setSize(250,150);
myfrm.add(btn1, border); // 在視窗內加入按鈕1
myfrm.add(btn2, border); // 在視窗內加入按鈕2
myfrm.add(tbx1, border); // 在視窗內加入按鈕2
/*
String[] numbers = {"0", "1", "2", "3", 4", "5", "6", "7", "8", "9"}; // goes on to 26
JButtons[] buttons = new JButton(numbers.length)
// create instance of each button
for (int i = 0; i < numbers.length; i++)
{
buttons[i] = new JButton(numbers[i]); // create buttons
}
*/
myfrm.setVisible(true);
}
public void actionPerformed(ActionEvent e)
{
int rn;
rn=(int) (Math.random()*49) ;
System.out.println(rn );
}
}
再打上另外的指令
更方便也更簡單的增加按鈕
//Swing, JButton Class
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class SwingTest extends JFrame
{
static JFrame myfrm=new JFrame("JButton class"); // Java Class JFrame
static JButton btn1=new JButton("JButton 1"); // 建立1按鈕物件
static JButton btn2=new JButton("JButton 2"); // 建立2按鈕物件
static JTextField tbx1=new JTextField("JTextField 1"); // 建立1文字方塊物件
public static void main(String args[])
{
//BorderLayout border=new BorderLayout();
GridLayout border=new GridLayout(0,2);
String numbers[] = {"0", "1", "2", "3"," 4", "5", "6", "7", "8", "9"}; // goes on to 26
JButton buttons[]=new JButton[10];
myfrm.setLayout(border);
myfrm.setSize(250,150);
myfrm.add(btn1, border); // 在視窗內加入按鈕1
myfrm.add(btn2, border); // 在視窗內加入按鈕2
myfrm.add(tbx1, border); // 在視窗內加入按鈕2
// create instance of each button
for (int i = 0; i < numbers.length; i++)
{
buttons[i] = new JButton(numbers[i]); // create buttons
myfrm.add(buttons[i], border); // 在視窗內加入按鈕1
}
myfrm.setVisible(true);
}
public void actionPerformed(ActionEvent e)
{
int rn;
rn=(int) (Math.random()*49) ;
System.out.println(rn );
}
}
改成3*3矩陣
//Swing, JButton Class
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class SwingTest extends JFrame
{
static JFrame myfrm=new JFrame("JButton class"); // Java Class JFrame
public static void main(String args[])
{
//BorderLayout border=new BorderLayout();
GridLayout border=new GridLayout(3,3);
String numbers[] = {"0", "1", "2", "3"," 4", "5", "6", "7", "8"}; // goes on to 26
JButton buttons[]=new JButton[10];
myfrm.setLayout(border);
myfrm.setSize(250,150);
// create instance of each button
for (int i = 0; i < numbers.length; i++)
{
buttons[i] = new JButton(numbers[i]); // create buttons
myfrm.add(buttons[i], border); // 在視窗內加入按鈕1
}
myfrm.setVisible(true);
}
public void actionPerformed(ActionEvent e)
{
int rn;
rn=(int) (Math.random()*49) ;
System.out.println(rn );
}
}
增加panel
//Swing, JButton Class
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class SwingTest extends JFrame
{
static JFrame myfrm=new JFrame("JButton class"); // Java Class JFrame
public static void main(String args[])
{
//BorderLayout border=new BorderLayout();
GridLayout border=new GridLayout(3,3);
JPanel p = new JPanel(border); //PREFERRED!
myfrm.add(p);
String numbers[] = {"0", "1", "2", "3"," 4", "5", "6", "7", "8"}; // goes on to 26
JButton buttons[]=new JButton[10];
myfrm.setLayout(border);
myfrm.setSize(250,150);
// create instance of each button
for (int i = 0; i < numbers.length; i++)
{
buttons[i] = new JButton(numbers[i]); // create buttons
myfrm.add(buttons[i], border); // 在視窗內加入按鈕1
}
myfrm.setVisible(true);
}
public void actionPerformed(ActionEvent e)
{
int rn;
rn=(int) (Math.random()*49) ;
System.out.println(rn );
}
}
------------------------------------------------------------------------------------------------------------------------------------
// with event
//AWT, JButton類別 有ActionListener
//AWT, JButton類別
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class AwtTestEvent extends JFrame implements ActionListener
{
//static JJFrame myfrm=new JFrame("JButton class"); // Java Class JFrame
//static AwtTestEvent myfrm=new AwtTestEvent("JFrame 1 "); // Java Class JFrame
static JButton btn1=new JButton("JButton 1"); // 建立1按鈕物件
static JTextField tbx1=new JTextField(" "); // 建立1文字方塊物件
public static void main(String args[])
{
String numbers[] = {"0", "1", "2", "3"," 4", "5", "6", "7", "8"}; // goes on to 26
JButton buttons[]=new JButton[10];
AwtTestEvent myfrm=new AwtTestEvent();
GridLayout border1=new GridLayout(3,3);
GridLayout border2=new GridLayout(2,1);
JPanel p1 = new JPanel(border1); //PREFERRED!
JPanel p2 = new JPanel(border2); //PREFERRED!
// create instance of each button
for (int i = 0; i < numbers.length; i++)
{
buttons[i] = new JButton(numbers[i]); // create buttons
p1.add(buttons[i], border1); // 在視窗內加入按鈕1
}
myfrm.add(p1);
myfrm.add(p2);
myfrm.setLayout(border1);
myfrm.setLayout(border2);
myfrm.setSize(250,150);
btn1.addActionListener(myfrm);
p2.add(btn1, border2); // 在視窗內加入按鈕1
p2.add(tbx1, border2); // 在視窗內加入文字方塊1;
myfrm.setVisible(true);
}
public void actionPerformed(ActionEvent e)
{
String sr;
sr=tbx1.getText();
System.out.println(sr);
}
}
沒有留言:
張貼留言