廣告廣告
  加入我的最愛 設為首頁 風格修改
首頁 首尾
 手機版   訂閱   地圖  簡體 
您是第 6736 個閱讀者
 
<<   1   2  下頁 >>(共 2 頁)
發表文章 發表投票 回覆文章
  可列印版   加為IE收藏   收藏主題   上一主題 | 下一主題   
schumacher76
個人文章 個人相簿 個人日記 個人地圖
路人甲
級別: 路人甲 該用戶目前不上站
推文 x0 鮮花 x0
分享: 轉寄此文章 Facebook Plurk Twitter 複製連結到剪貼簿 轉換為繁體 轉換為簡體 載入圖片
推文 x0
[Java][求助] Java-AWT與SWING程式問題求助
我想做的成果是...
在Text A輸入數字在Text B輸入字串之後按寫入
在Text C輸入剛剛輸入的數字然後按讀出並會在Text D顯示剛剛的字串...

請問以上程序有什麼地方出錯呢?
請各位前輩們指導一下...謝謝


import   java.awt.*;  
import   java.awt.event.*;  
import   javax.swing.*;  
import   javax.swing.event.*;

public class AwtIo
{
  static String datamem[]=new String;
  bt1.addActionListener(new ActLis());
  bt2.addActionListener(new ActLis());
  JLabel lb1=new JLabel("區域選擇");
  JButton bt1=new JButton("寫入");
  JButton bt2=new JButton("讀出");

  JTextField tx1=new JTextField("1");
  JTextField tx2=new JTextField("ANDY");
  JTextField tx3=new JTextField("");
  JTextField tx4=new JTextField("");
  static int i=1;
 
  public static void main(String args[])
  {
    bt1.addActionListener(new ActLis()); 
    bt2.addActionListener(new ActLis());
    JFrame frame = new JFrame("I/O測試");
    Container c=frame.getContentPane();
    c.setLayout(null);

    // JLabel元件
    JLabel lb1=new JLabel("區域選擇");
  ..

訪客只能看到部份內容,免費 加入會員 或由臉書 Google 可以看到全部內容




獻花 x0 回到頂端 [樓 主] From:臺灣索尼So-net網 | Posted:2009-03-17 19:57 |
overing 手機
個人文章 個人相簿 個人日記 個人地圖
初露鋒芒
級別: 初露鋒芒 該用戶目前不上站
推文 x0 鮮花 x55
分享: 轉寄此文章 Facebook Plurk Twitter 複製連結到剪貼簿 轉換為繁體 轉換為簡體 載入圖片

參考看看~
複製程式
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;

/* 都用到swing了乾脆就都用高階元件就好了
 */

public class NewAwtIo extends JFrame implements ActionListener {
  static String datamem[]=new String[10];
  JLabel lb1;
  JButton bt1;
  JButton bt2;
  JTextField tx1;
  JTextField tx2;
  JTextField tx3;
  JTextField tx4;
  static int i=1;
  
  public NewAwtIo() {
    setLayout(null);
    
    lb1 = new JLabel("區域選擇");
    lb1.setForeground(Color.red);
    lb1.setSize(240,40);
    lb1.setLocation(10,20);
    add(lb1);
    
    tx1 = new JTextField("");
    tx1.setForeground(Color.red);
    tx1.setSize(200,40);
    tx1.setLocation(10,70);
    add(tx1);
    
    tx2 = new JTextField("");
    tx2.setForeground(Color.red);
    tx2.setSize(200,40);
    tx2.setLocation(250,70);
    add(tx2);
    
    tx3 = new JTextField("");
    tx3.setForeground(Color.red);
    tx3.setSize(200,40);
    tx3.setLocation(10,150);
    add(tx3);
    
    tx4 = new JTextField("");
    tx4.setForeground(Color.black);
    tx4.setSize(200,40);
    tx4.setLocation(250,150);
    add(tx4);
    
    bt1=new JButton("寫入");
    bt1.setSize(100,40);
    bt1.setLocation(60,250);
    bt1.setBorderPainted(true);
    bt1.setEnabled(true);
    add(bt1);
    
    bt2=new JButton("寫出");
    bt2.setSize(100,40);
    bt2.setLocation(300,250);
    bt2.setBorderPainted(true);
    bt2.setEnabled(true);
    add(bt2);
    
    bt1.addActionListener(this);
    bt2.addActionListener(this);
    
    tx1.setEditable(true);
    tx2.setEditable(true);
    tx3.setEditable(true);
    setVisible(true);
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setSize(500,400); //設定視窗大小
    setVisible(true); //顯示視窗
  }
  
  public void actionPerformed(ActionEvent e) {
    int num1,num2;
    
    if(e.getSource().equals(bt1)) {
      num1 = Integer.parseInt(tx1.getText());
      datamem[num1] = tx2.getText();
      
      if((i%2) == 1)
        setBackground(Color.yellow);
      else
        setBackground(Color.red);
      i=i+1;
    }
    else if (e.getSource().equals(bt2)) {
      num2=Integer.parseInt(tx3.getText());
      tx4.setText(datamem[num2]);
      
      if((i%2) == 1)
        setBackground(Color.blue);
      else
        setBackground(Color.green);
      i=i+1;
    }
  }
  
  public static void main(String[] args) {
    new NewAwtIo();
  }
}

/*
class AwtIo
{
  static String datamem[]=new String[10];
  bt1.addActionListener(new ActLis()); //這個比較適合放在new之後
  bt2.addActionListener(new ActLis()); //同上
  JLabel lb1=new JLabel("區域選擇");
  JButton bt1=new JButton("寫入");
  JButton bt2=new JButton("讀出");

  JTextField tx1=new JTextField("1");
  JTextField tx2=new JTextField("ANDY");
  JTextField tx3=new JTextField("");
  JTextField tx4=new JTextField("");
  static int i=1;
 
  public static void main(String args[])
  {
    bt1.addActionListener(new ActLis()); //跟L114一樣
    bt2.addActionListener(new ActLis()); //同上
    JFrame frame = new JFrame("I/O測試");
    Container c=frame.getContentPane();
    c.setLayout(null);

    // JLabel元件
    JLabel lb1=new JLabel("區域選擇"); //重複到L115的動作 你是要類別的還是區域的?
    lb1.setForeground(Color.red);
    lb1.setSize(240,40);
    lb1.setLocation(10,20);
    c.add(lb1);

    JTextField tx1=new JTextField(""); //重複到L119的動作
    tx1.setForeground(Color.red);
    tx1.setSize(200,40);
    tx1.setLocation(10,70);
    c.add(tx1);

    JTextField tx2=new JTextField(""); //重複到L120
    tx2.setForeground(Color.red);
    tx2.setSize(200,40);
    tx2.setLocation(250,70);
    c.add(tx2);

    JTextField tx3=new JTextField(""); //重複到L121
    tx3.setForeground(Color.red);
    tx3.setSize(200,40);
    tx3.setLocation(10,150);
    c.add(tx3);

    JTextField tx4=new JTextField(""); //重複到L122
    tx4.setForeground(Color.black);
    tx4.setSize(200,40);
    tx4.setLocation(250,150);
    c.add(tx4);

    JButton bt1=new JButton("寫入"); //重複到L116
    bt1.setSize(100,40);
    bt1.setLocation(60,250);
    bt1.setBorderPainted(true);
    bt1.setEnabled(true);
    c.add(bt1);

    JButton bt2=new JButton("寫出"); //重複到L117
    bt2.setSize(100,40);
    bt2.setLocation(300,250);
    bt2.setBorderPainted(true);
    bt2.setEnabled(true);
    c.add(bt2);
   
    tx1.setEditable(true); //預設JTextField就是了 可省略
    tx2.setEditable(true); //同上
    tx3.setEditable(true); //同上
    frm.add(bt1); //跟上面c.add(...)重複 選一個就好
    frm.add(bt2); //同上
    frm.add(tx1); //同上
    frm.add(tx2); //同上
    frm.add(tx3); //同上
    frm.add(tx4); //同上
    frm.setVisible(true);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setSize(500,400); //設定視窗大小
    frame.setVisible(true); //顯示視窗
    frm.addWindowListener(new WindowAdapter() //L118那行可以抵L191~197的動作
    {
      public void windowClosing(WindowEvent e)
      {
        System.exit(0);
      }
    });
  }


  static class ActLis implements ActionListener
  {
    public void actionPerformed(ActionEvent e)
    {
   
      int num1,num2;
      Button bt=(Button) e.getSource();
      if (bt==bt1)
      {
        num1=Integer.parseInt(tx1.getText());
        datamem[num1]=tx2.getText();
   
        if((i%2) == 1)
          frm.setBackground(Color.yellow);
        else
          frm.setBackground(Color.red);
        i=i+1;
      }
   
      else if (bt==bt2)
      {
        num2=Integer.parseInt(tx3.getText());
        tx4.setText(datamem[num2]);

        if((i%2) == 1)
          frm.setBackground(Color.blue);
        else
          frm.setBackground(Color.green);
        i=i+1;
      }
    }
  }
} */

此文章被評分,最近評分記錄
財富:50 (by 三仙) | 理由: 因為您的參與,讓程式設計更容易!! ^^


Zippy G2 600W
Intel C2Q Q9550
Gigabyte GA-X48-DQ6
Gigabyte GV-NX96T512H-B
G-skill pi 1100MHz 2Gx4
ACARD ANS-9010 32GB (Windows7 10sec boot!!)
WD 150GB(WD1500ADFD)/640GB(6400AASK)
獻花 x1 回到頂端 [1 樓] From:未知地址 | Posted:2009-03-17 21:36 |
schumacher76
個人文章 個人相簿 個人日記 個人地圖
路人甲
級別: 路人甲 該用戶目前不上站
推文 x0 鮮花 x0
分享: 轉寄此文章 Facebook Plurk Twitter 複製連結到剪貼簿 轉換為繁體 轉換為簡體 載入圖片

謝謝 overing 大大的更正與指導...

不過愚鈍的我用您幫我更正後的程式執行,
出現的框架全部都是白色的,加上title之後也沒顯示出來。

請問是哪邊出問題還是說還有地方要修正呢?


獻花 x0 回到頂端 [2 樓] From:臺灣索尼So-net網 | Posted:2009-03-18 02:44 |
overing 手機
個人文章 個人相簿 個人日記 個人地圖
初露鋒芒
級別: 初露鋒芒 該用戶目前不上站
推文 x0 鮮花 x55
分享: 轉寄此文章 Facebook Plurk Twitter 複製連結到剪貼簿 轉換為繁體 轉換為簡體 載入圖片

下面是引用 schumacher76 於 2009-03-18 02:44 發表的 : 到引言文
謝謝 overing 大大的更正與指導...

不過愚鈍的我用您幫我更正後的程式執行,
出現的框架全部都是白色的,加上title之後也沒顯示出來。

請問是哪邊出問題還是說還有地方要修正呢?

這就怪了~ 表情
我這都是正常編譯正常執行的說
我是直接下指令編譯
javac NewAwtIo.java
接著
java NewAwtIo

直接附上原始檔給你好了請右鍵另存
執行時的圖

順邊修正 setBackground(Color);
因為JFrame顯示再最上層的並不是這副程式能上到色的pane
要先取得content pane再對其修改顏色才看的出來
所以複寫掉 改成
getContentPane().setBackground(Color.blue);


[ 此文章被overing在2009-03-19 04:30重新編輯 ]


Zippy G2 600W
Intel C2Q Q9550
Gigabyte GA-X48-DQ6
Gigabyte GV-NX96T512H-B
G-skill pi 1100MHz 2Gx4
ACARD ANS-9010 32GB (Windows7 10sec boot!!)
WD 150GB(WD1500ADFD)/640GB(6400AASK)
獻花 x0 回到頂端 [3 樓] From:未知地址 | Posted:2009-03-19 04:25 |
schumacher76
個人文章 個人相簿 個人日記 個人地圖
路人甲
級別: 路人甲 該用戶目前不上站
推文 x0 鮮花 x0
分享: 轉寄此文章 Facebook Plurk Twitter 複製連結到剪貼簿 轉換為繁體 轉換為簡體 載入圖片

再請問...
我程式的Title用這個指令可以嗎?

    JFrame frame = new JFrame("I/O測試");
    Container c=frame.getContentPane();
    c.setLayout(null);

後來改成這個也不行..
    JFrame frame = new JFrame();
    frame.setTitle("I/O測試");
    frame.setLayout(null);

還是說這2個指令都可以,不過還要在加什麼指令呢?


獻花 x0 回到頂端 [4 樓] From:臺灣 | Posted:2009-03-19 10:44 |
overing 手機
個人文章 個人相簿 個人日記 個人地圖
初露鋒芒
級別: 初露鋒芒 該用戶目前不上站
推文 x0 鮮花 x55
分享: 轉寄此文章 Facebook Plurk Twitter 複製連結到剪貼簿 轉換為繁體 轉換為簡體 載入圖片

下面是引用 schumacher76 於 2009-03-19 10:44 發表的 : 到引言文
再請問...
我程式的Title用這個指令可以嗎?

    JFrame frame = new JFrame("I/O測試");
    Container c=frame.getContentPane();
    c.setLayout(null);

後來改成這個也不行..
    JFrame frame = new JFrame();
    frame.setTitle("I/O測試");
    frame.setLayout(null);

還是說這2個指令都可以,不過還要在加什麼指令呢?
設定title的話兩種方式都可以
JFrame frame = new JFrame("I/O測試");
這種是透過JFrame這類別的建構子帶入title
建構子 JFrame(String) 會將參數字串作為建構視窗的標題
setTitle("I/O測試");
這是直接去設定標題

不過
Container c=frame.getContentPane();
setLayout(null);
這兩行就跟title無關了

還是不行的話麻煩po上你現在版本的code這樣比較好找出問題所在


[ 此文章被overing在2009-03-19 20:15重新編輯 ]


Zippy G2 600W
Intel C2Q Q9550
Gigabyte GA-X48-DQ6
Gigabyte GV-NX96T512H-B
G-skill pi 1100MHz 2Gx4
ACARD ANS-9010 32GB (Windows7 10sec boot!!)
WD 150GB(WD1500ADFD)/640GB(6400AASK)
獻花 x0 回到頂端 [5 樓] From:未知地址 | Posted:2009-03-19 20:10 |
schumacher76
個人文章 個人相簿 個人日記 個人地圖
路人甲
級別: 路人甲 該用戶目前不上站
推文 x0 鮮花 x0
分享: 轉寄此文章 Facebook Plurk Twitter 複製連結到剪貼簿 轉換為繁體 轉換為簡體 載入圖片

複製程式
 
import java.awt.*; 
import java.awt.event.*; 
import javax.swing.*; 
import javax.swing.event.*; 

public class NewAwtIo extends JFrame implements ActionListener { 
static String datamem[]=new String[10]; 
JLabel lb1; 
JButton bt1; 
JButton bt2; 
JTextField tx1; 
JTextField tx2; 
JTextField tx3; 
JTextField tx4; 
JFrame frame; 
static int i=1; 

public NewAwtIo() { 
  setLayout(null); 

  frame = new JFrame("I/O測試"); 
  frame.setForeground(Color.red); 

  lb1 = new JLabel("區域選擇"); 
  lb1.setForeground(Color.red); 
  lb1.setSize(240,40); 
  lb1.setLocation(10,20); 
  add(lb1); 

  tx1 = new JTextField(""); 
  tx1.setForeground(Color.red); 
  tx1.setSize(200,40); 
  tx1.setLocation(10,70); 
  add(tx1); 

  tx2 = new JTextField(""); 
  tx2.setForeground(Color.red); 
  tx2.setSize(200,40); 
  tx2.setLocation(10,150); 
  add(tx2); 

  tx3 = new JTextField(""); 
  tx3.setForeground(Color.red); 
  tx3.setSize(200,40); 
  tx3.setLocation(250,70); 
  add(tx3); 

  tx4 = new JTextField(""); 
  tx4.setForeground(Color.black); 
  tx4.setSize(200,40); 
  tx4.setLocation(250,150); 
  add(tx4); 

  bt1=new JButton("寫入"); 
  bt1.setSize(100,40); 
  bt1.setLocation(60,250); 
  bt1.setBorderPainted(true); 
  bt1.setEnabled(true); 
  add(bt1); 

  bt2=new JButton("寫出"); 
  bt2.setSize(100,40); 
  bt2.setLocation(300,250); 
  bt2.setBorderPainted(true); 
  bt2.setEnabled(true); 
  add(bt2); 

  bt1.addActionListener(this); 
  bt2.addActionListener(this); 

  tx1.setEditable(true); 
  tx2.setEditable(true); 
  tx3.setEditable(true); 
  setVisible(true); 
  setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
  setSize(500,400); //設定視窗大小 
  setVisible(true); //顯示視窗 
} 

public void setBackground(Color c) { 
  getContentPane().setBackground(c); 
} 

public void actionPerformed(ActionEvent e) { 
  int num1,num2; 

  if(e.getSource().equals(bt1)) { 
    num1 = Integer.parseInt(tx1.getText()); 
    datamem[num1] = tx2.getText(); 
  
    if((i%2) == 1) setBackground(Color.yellow); 
    else setBackground(Color.red); 
    i=i+1; 
  } else if (e.getSource().equals(bt2)) { 
    num2=Integer.parseInt(tx3.getText()); 
    tx4.setText(datamem[num2]); 
  
    if((i%2) == 1) setBackground(Color.blue); 
    else setBackground(Color.green); 
    i=i+1; 
  } 
} 

public static void main(String[] args) { 
  new NewAwtIo(); 
} 
} 
 


我是用您Compile好的.java程式去執行的...
之後再加上title那2段...好像也沒有顯示出來!
麻煩請再幫我看看哪邊有需要修正,謝謝。


[ 此文章被schumacher76在2009-03-19 22:42重新編輯 ]


獻花 x0 回到頂端 [6 樓] From:臺灣 | Posted:2009-03-19 22:37 |
overing 手機
個人文章 個人相簿 個人日記 個人地圖
初露鋒芒
級別: 初露鋒芒 該用戶目前不上站
推文 x0 鮮花 x55
分享: 轉寄此文章 Facebook Plurk Twitter 複製連結到剪貼簿 轉換為繁體 轉換為簡體 載入圖片

下面是引用 schumacher76 於 2009-03-19 22:37 發表的 : 到引言文.
上面並沒有看到你的
setTitle阿~ 表情
還有就是
setVisible(true)
這個你呼叫了兩次
應該說我那篇就犯這個錯誤了@@
只要留最後一次就好了


Zippy G2 600W
Intel C2Q Q9550
Gigabyte GA-X48-DQ6
Gigabyte GV-NX96T512H-B
G-skill pi 1100MHz 2Gx4
ACARD ANS-9010 32GB (Windows7 10sec boot!!)
WD 150GB(WD1500ADFD)/640GB(6400AASK)
獻花 x0 回到頂端 [7 樓] From:未知地址 | Posted:2009-03-19 22:57 |
schumacher76
個人文章 個人相簿 個人日記 個人地圖
路人甲
級別: 路人甲 該用戶目前不上站
推文 x0 鮮花 x0
分享: 轉寄此文章 Facebook Plurk Twitter 複製連結到剪貼簿 轉換為繁體 轉換為簡體 載入圖片

複製程式
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;

public class NewAwtIo extends JFrame implements ActionListener {
  static String datamem[]=new String[10];
  JLabel lb1;
  JButton bt1;
  JButton bt2;
  JTextField tx1;
  JTextField tx2;
  JTextField tx3;
  JTextField tx4;
  JFrame frame;
  static int i=1;

  public NewAwtIo() {
    setLayout(null);
 
    frame = new JFrame(""); 
    frame.setTitle("I/O測試");
    frame.setForeground(Color.red);
 
    lb1 = new JLabel("區域選擇");
    lb1.setForeground(Color.red);
    lb1.setSize(240,40);
    lb1.setLocation(10,20);
    add(lb1);
 
    tx1 = new JTextField("");
    tx1.setForeground(Color.red);
    tx1.setSize(200,40);
    tx1.setLocation(10,70);
    add(tx1);
 
    tx2 = new JTextField("");
    tx2.setForeground(Color.red);
    tx2.setSize(200,40);
    tx2.setLocation(10,150);
    add(tx2);
 
    tx3 = new JTextField("");
    tx3.setForeground(Color.red);
    tx3.setSize(200,40);
    tx3.setLocation(250,70);
    add(tx3);
 
    tx4 = new JTextField("");
    tx4.setForeground(Color.black);
    tx4.setSize(200,40);
    tx4.setLocation(250,150);
    add(tx4);
 
    bt1=new JButton("寫入");
    bt1.setSize(100,40);
    bt1.setLocation(60,250);
    bt1.setBorderPainted(true);
    bt1.setEnabled(true);
    add(bt1);
 
    bt2=new JButton("寫出");
    bt2.setSize(100,40);
    bt2.setLocation(300,250);
    bt2.setBorderPainted(true);
    bt2.setEnabled(true);
    add(bt2);
 
    bt1.addActionListener(this);
    bt2.addActionListener(this);
 
    tx1.setEditable(true);
    tx2.setEditable(true);
    tx3.setEditable(true);
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setSize(500,400); //設定視窗大小
    setVisible(true); //顯示視窗
  }
  
  public void setBackground(Color c) {
    getContentPane().setBackground(c);
  }

  public void actionPerformed(ActionEvent e) {
    int num1,num2;
 
    if(e.getSource().equals(bt1)) {
      num1 = Integer.parseInt(tx1.getText());
      datamem[num1] = tx2.getText();
   
      if((i%2) == 1) setBackground(Color.yellow);
      else setBackground(Color.red);
      i=i+1;
    } else if (e.getSource().equals(bt2)) {
      num2=Integer.parseInt(tx3.getText());
      tx4.setText(datamem[num2]);
   
      if((i%2) == 1) setBackground(Color.blue);
      else setBackground(Color.green);
      i=i+1;
    }
  }

  public static void main(String[] args) {
    new NewAwtIo();
  }
}

補上SetTitle跟刪除多餘的那一行...


獻花 x0 回到頂端 [8 樓] From:臺灣 | Posted:2009-03-20 00:44 |
overing 手機
個人文章 個人相簿 個人日記 個人地圖
初露鋒芒
級別: 初露鋒芒 該用戶目前不上站
推文 x0 鮮花 x55
分享: 轉寄此文章 Facebook Plurk Twitter 複製連結到剪貼簿 轉換為繁體 轉換為簡體 載入圖片

大概知道問題所在了
setTitle你只要這樣呼叫即可
因為這個NewAwtIo已經繼承了JFrame
所以自己就"是一個"JFrame了
不用再去另外產生一個
而你frame.setTitle是去設定新生出來的那一個
並不是最後顯示的那一個

一般在設計JAVA視窗程式的時候有兩種方式
打個比方~今天我需要個有含JLabel的JFrame
我可以用下面兩種方式來作

一種是在主程式進入點(main)裡面產生一個視窗( JFrame frame = new JFrame(); )
然後在上面另外加上要加的東西( JLabel label1 = new JLabel(); frame.add(label1); )
接著將視窗顯示出來( frame.setVisible(true); )

另一種是讓Class去繼承視窗Class( class NewFrame extends JFrame{...} )
這樣這個新Class也變成"一種視窗" 因為是繼承視窗類別而來的
所以原有視窗Class的 屬性(變數) 跟 方法(副程式) 也能在這新的Class上面使用
( NewFrame nFrame = new NewFrame(); nFrame.setTitle("New Frame"); )
然後在這新的Class當中定義好 這種新視窗有的東西
( class NewFrame extends JFrame{ JLabel label1 = new JLabel(); } )
接著在新視窗Class的建構子當中將Label加上( public NewFrame() { add(label1); )
雖然這種方式可能稍微蠻煩了點 但是相對於主程式來講 要做的事情就少很多了
主程式進入點(main)裡你指需要這樣寫( new NewFrame().setVisible(true); )
因為NewFrame這個Class裡面已經把自己需要做的事都做完了

以下就這兩種方式各作一份

產生視窗再加上的方式 ( 檔名:MakeFrame.java)
複製程式
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;

public class MakeFrame {
  public static void main(String[] args) {
    String[] datamem = new String[10];
    JLabel lb1;
    JButton bt1, bt2;
    JTextField tx1, tx2, tx3, tx4;
    JFrame frame = new JFrame();

    frame.setLayout(null);

    frame.setTitle("I/O測試");
    frame.getContentPane().setForeground(Color.red);

    lb1 = new JLabel("區域選擇");
    lb1.setForeground(Color.red);
    lb1.setSize(240,40);
    lb1.setLocation(10,20);
    frame.add(lb1);

    tx1 = new JTextField("");
    tx1.setForeground(Color.red);
    tx1.setSize(200,40);
    tx1.setLocation(10,70);
    frame.add(tx1);

    tx2 = new JTextField("");
    tx2.setForeground(Color.red);
    tx2.setSize(200,40);
    tx2.setLocation(250,70);
    frame.add(tx2);

    tx3 = new JTextField("");
    tx3.setForeground(Color.red);
    tx3.setSize(200,40);
    tx3.setLocation(10,150);
    frame.add(tx3);

    tx4 = new JTextField("");
    tx4.setForeground(Color.black);
    tx4.setSize(200,40);
    tx4.setLocation(250,150);
    frame.add(tx4);

    bt1=new JButton("寫入");
    bt1.setSize(100,40);
    bt1.setLocation(60,250);
    bt1.setBorderPainted(true);
    //bt1.setEnabled(true); 預設new JButton()就是Enable狀態了 所以這個可以省略
    frame.add(bt1);

    bt2=new JButton("寫出");
    bt2.setSize(100,40);
    bt2.setLocation(300,250);
    bt2.setBorderPainted(true);
    //bt2.setEnabled(true);
    frame.add(bt2);

    ActLis actLis = new ActLis(tx1,tx2,tx3,tx4,bt1,bt2,datamem,frame);
    bt1.addActionListener(actLis);
    bt2.addActionListener(actLis);

    //tx1.setEditable(true);//跟JButton一樣 預設就是Enable了 可省略
    //tx2.setEditable(true);
    //tx3.setEditable(true);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setSize(500,400); //設定視窗大小
    frame.setVisible(true); //顯示視窗
  }
  
  //其實這邊也是在做類似繼承的動作 只是這邊叫做"實做(implements)"
  static class ActLis implements ActionListener {
    JTextField tx1, tx2, tx3, tx4;
    JButton bt1, bt2;
    String[] datamem;
    JFrame frame;
    static int i = 1;
    
    public ActLis(JTextField t1, JTextField t2, JTextField t3, JTextField t4,
            JButton b1, JButton b2, String[] d, JFrame f) {
      tx1 = t1; tx2 = t2; tx3 = t3; tx4 = t4;
      bt1 = b1; bt2 = b2;
      datamem = d;
      frame = f;
    }
    
    public void actionPerformed(ActionEvent e) {
      int num1, num2;

      if(e.getSource().equals(bt1)) {
        num1 = Integer.parseInt(tx1.getText());
        datamem[num1] = tx2.getText();
 
        if((i%2) == 1) frame.setBackground(Color.yellow);
        else frame.setBackground(Color.red);
        i = i + 1;
      } else if (e.getSource().equals(bt2)) {
        num2 = Integer.parseInt(tx3.getText());
        tx4.setText(datamem[num2]);
 
        if((i%2) == 1) frame.setBackground(Color.blue);
        else frame.setBackground(Color.green);
        i = i + 1;
      }
    }
  }
}

繼承的方式( 檔名:NewFrame.java )
複製程式
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;

public class NewFrame extends JFrame implements ActionListener {
  static String datamem[]=new String[10];
  JLabel lb1;
  JButton bt1, bt2;
  JTextField tx1, tx2, tx3, tx4;
  static int i=1;

  public NewFrame() {
    setLayout(null);

    setTitle("I/O測試");
    setForeground(Color.red);

    lb1 = new JLabel("區域選擇");
    lb1.setForeground(Color.red);
    lb1.setSize(240,40);
    lb1.setLocation(10,20);
    add(lb1);

    tx1 = new JTextField("");
    tx1.setForeground(Color.red);
    tx1.setSize(200,40);
    tx1.setLocation(10,70);
    add(tx1);

    tx2 = new JTextField("");
    tx2.setForeground(Color.red);
    tx2.setSize(200,40);
    tx2.setLocation(250,70);
    add(tx2);

    tx3 = new JTextField("");
    tx3.setForeground(Color.red);
    tx3.setSize(200,40);
    tx3.setLocation(10,150);
    add(tx3);

    tx4 = new JTextField("");
    tx4.setForeground(Color.black);
    tx4.setSize(200,40);
    tx4.setLocation(250,150);
    add(tx4);

    bt1=new JButton("寫入");
    bt1.setSize(100,40);
    bt1.setLocation(60,250);
    bt1.setBorderPainted(true);
    bt1.setEnabled(true);
    add(bt1);

    bt2=new JButton("寫出");
    bt2.setSize(100,40);
    bt2.setLocation(300,250);
    bt2.setBorderPainted(true);
    bt2.setEnabled(true);
    add(bt2);

    bt1.addActionListener(this);
    bt2.addActionListener(this);

    tx1.setEditable(true);
    tx2.setEditable(true);
    tx3.setEditable(true);
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setSize(500,400); //設定視窗大小
    setVisible(true); //顯示視窗
  }

  public void setBackground(Color c) {
    getContentPane().setBackground(c);
  }

  public void actionPerformed(ActionEvent e) {
    int num1,num2;

    if(e.getSource().equals(bt1)) {
      num1 = Integer.parseInt(tx1.getText());
      datamem[num1] = tx2.getText();
 
      if((i%2) == 1) setBackground(Color.yellow);
      else setBackground(Color.red);
      i=i+1;
    } else if (e.getSource().equals(bt2)) {
      num2=Integer.parseInt(tx3.getText());
      tx4.setText(datamem[num2]);
 
      if((i%2) == 1) setBackground(Color.blue);
      else setBackground(Color.green);
      i=i+1;
    }
  }

  public static void main(String[] args) {
    new NewFrame();
  }
}


[ 此文章被overing在2009-03-20 19:37重新編輯 ]


Zippy G2 600W
Intel C2Q Q9550
Gigabyte GA-X48-DQ6
Gigabyte GV-NX96T512H-B
G-skill pi 1100MHz 2Gx4
ACARD ANS-9010 32GB (Windows7 10sec boot!!)
WD 150GB(WD1500ADFD)/640GB(6400AASK)
獻花 x0 回到頂端 [9 樓] From:未知地址 | Posted:2009-03-20 18:11 |

<<   1   2  下頁 >>(共 2 頁)
首頁  發表文章 發表投票 回覆文章
Powered by PHPWind v1.3.6
Copyright © 2003-04 PHPWind
Processed in 0.032083 second(s),query:16 Gzip disabled
本站由 瀛睿律師事務所 擔任常年法律顧問 | 免責聲明 | 本網站已依台灣網站內容分級規定處理 | 連絡我們 | 訪客留言