广告广告
  加入我的最爱 设为首页 风格修改
首页 首尾
 手机版   订阅   地图  繁体 
您是第 6737 个阅读者
 
<<   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.023344 second(s),query:16 Gzip disabled
本站由 瀛睿律师事务所 担任常年法律顾问 | 免责声明 | 本网站已依台湾网站内容分级规定处理 | 连络我们 | 访客留言