Java程序设计之 “图形用户界面类编程的四个编程例题”

题一:点击B1与B2的按钮,其值可以显示在文本框中

题二:简单的数字猜大小游戏

题三:简易记事本

题四:温度转换器

题一

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
class Incrementor implements ActionListener{
    JTextField numberTxf;
    JButton incrementBtn,decrementBtn;
    public void makeGUI(){
        JFrame frm = new JFrame("Incrementor");
        Container c=frm.getContentPane();
        c.setLayout(new FlowLayout());      
        incrementBtn = new JButton("b1");
        c.add(incrementBtn);
        incrementBtn.addActionListener(this);
        decrementBtn= new JButton("b2");
        c.add(decrementBtn);
        decrementBtn.addActionListener(this);
        numberTxf = new JTextField(" ",20); 
        c.add(numberTxf);
        frm.setSize(300,100);
        frm.setVisible(true);
   }
	public void actionPerformed(ActionEvent e) {
       String newNum = null ;
        if (e.getActionCommand().equals("b1")){
            newNum=e.getActionCommand();
        }
       else if (e.getActionCommand().equals("b2")) {
           newNum=e.getActionCommand();
        }
      numberTxf.setText(String.valueOf(newNum));
    }
    public static void main(String args[]) {
     Incrementor i = new Incrementor();
     i.makeGUI();  
    }
}

题二

import java.awt.*;
import java.awt.event.ActionEvent;
import java.util.Random;
import javax.swing.*;
import java.awt.event.*;
@SuppressWarnings("serial")
public class test03 extends JFrame implements ActionListener{
	JTextField []t={new JTextField("",15),new JTextField("输入你所猜的数字",15) ,new JTextField("",15)};
	JButton guess1;
	test03(String sTitle){
		super(sTitle);
		init();	
}
	public void init()
	{	
		Container c=getContentPane();
		//c.setSize(300, 300);
	    c.setLayout(new FlowLayout());
		c.add(t[0]);
		c.add(new JLabel("提示为:"));
		c.add(t[1]);
        guess1=new JButton("猜猜看");
		c.add(guess1);
		guess1.addActionListener(this);
	}
	public void actionPerformed(ActionEvent e)
	{
		int pq;
		Random r=new Random();
	    pq=r.nextInt(101)+0;  
		t[2].setText(String.valueOf(pq));
	    String L=t[0].getText();
		int p=Integer.parseInt(L);
		int p1=Integer.parseInt(t[2].getText());
		if(e.getSource()==guess1)
		{
			if(p>p1)
			{
				t[1].setText("大了");
			}
			else if(p==p1)
			{
				t[1].setText("恭喜");
			}
			else 
			{
				t[1].setText("小了");
			}
		}	 		
	}
	public static void main(String args[]){
		test03 f=new test03("猜数字");
		f.setSize(400, 300);
		f.setVisible(true);
	}
}

题三

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.io.*;
 class ChineseTextEdit extends JFrame implements ActionListener
 {
	 JTextArea text; 
     JButton save,cancel,exit; 
     String fileName="myText.txt";
     public ChineseTextEdit() throws IOException
     {
       setTitle("文本编辑器");
       text=new JTextArea();
       JScrollPane area=new JScrollPane(text);
       text.setText(readFile(fileName));
       save=new JButton(" 保 存 ");
       cancel=new JButton(" 取 消 ");
       exit=new JButton(" 退 出 ");
       JPanel p1=new JPanel();
       p1.add(save);
       save.addActionListener(this);
       p1.add(cancel);
       cancel.addActionListener(this);
       p1.add(exit);
       exit.addActionListener(this);
       setSize(400,200);
       setVisible(true);     
    }

     public void actionPerformed(ActionEvent e)
     {
	      if(e.getSource()==save)
	       {		
			   BufferedWriter h=null;
	    	   try
	    	   {
	    		   h= new BufferedWriter(new FileWriter(fileName));    
	    		   String str = text.getText();   
	    		   h.write(str);   
	    		   h.flush();	
			   } catch(IOException e1){System.out.println("操作失败...");
		   }
	 }
     else if(e.getSource()==cancel)
     {
          text.setText("");     }
 
      else if(e.getSource()==exit)
      {
         setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);      }
 
   }
public String readFile(String fileName) throws IOException{
 
	String line = null;
	BufferedReader k=null;
	PrintWriter  fw =null;
	File d=new File(fileName);
	if(d.exists())
	{
		FileReader fr = null;
		fr = new FileReader(fileName);
		BufferedReader br=new BufferedReader(fr);
		int j=0;
			while(br.ready())
			{
				line+=br.readLine();
			}
	}
	else
	{	
		fw=new PrintWriter(d);	
	}
  return line;
}
   public static void main(String args[]) throws IOException
   {
   new ChineseTextEdit();
   }
}

题四

import java.awt.*;
import java.awt.event.ActionEvent;
import javax.swing.*;
import java.awt.event.*;
public class TempTrans extends JFrame implements ActionListener{
	JTextField []t={new JTextField("",5),new JTextField("",5)};
	JButton guess1;
	TempTrans(String sTitle){
		super(sTitle);
		init1();	
}
	public void init1()
	{	
		Container c=getContentPane();
	    c.setLayout(new FlowLayout());
		c.add(new JLabel("华氏温度:"));
		c.add(t[0]);
		c.add(new JLabel("摄氏温度:"));
		c.add(t[1]);
        guess1=new JButton("转换");
		c.add(guess1);
		guess1.addActionListener(this);
		t[1].setEditable(false);
	}
	public void actionPerformed(ActionEvent e)
	{
		    String L=t[0].getText();
		  Double p=Double.parseDouble(L);
				if(e.getSource()==guess1)
				{
					Double k;
					k=(p-32)*(5/9);
					System.out.println(k);
		java.text.DecimalFormat df = new java.text.DecimalFormat("###.##");
					String s=df.format(k);
					t[1].setText(s);
				}
		    }	 	
	}
	public static void main(String args[]){

		TempTrans f=new TempTrans("华氏-摄氏度转换器");
		f.setSize(300,100);
		f.setVisible(true);
	}
}

 

  • 2
    点赞
  • 30
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值