⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 temperatureframe.java

📁 这是用Java写的用来测试温度控制的源代码
💻 JAVA
字号:
package ex;

/**
   @version 1.32 2004-05-06
   @author Cay Horstmann
*/

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.*;

public class TemperatureFrame
{
   public static void main(String[] args)
   {  
      BoxLayoutFrame frame = new BoxLayoutFrame();
      frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      frame.setVisible(true);
   }
}

/**
   A frame that uses box layouts to organize various components.
*/
class BoxLayoutFrame extends JFrame
{  
   public BoxLayoutFrame()
   {  
      setTitle("BoxLayoutTest");
      setSize(DEFAULT_WIDTH, DEFAULT_HEIGHT);

      // construct the top horizontal box

      JLabel label1 = new JLabel("摄氏温度:");
      textField1 = new JTextField(10);
      textField1.setMaximumSize(textField1.getPreferredSize());

      hbox1 = Box.createHorizontalBox();
      hbox1.add(label1);
      // separate with a 10-pixel strut
      hbox1.add(Box.createHorizontalStrut(10));
      hbox1.add(textField1);

      // construct the middle horizontal box

      JLabel label2 = new JLabel("华氏温度:");
      textField2 = new JTextField(10);
      textField2.setMaximumSize(textField2.getPreferredSize());
     

      hbox2 = Box.createHorizontalBox();
      hbox2.add(label2);
      // separate with a 10-pixel strut
      hbox2.add(Box.createHorizontalStrut(10));
      hbox2.add(textField2);
      
      // construct the bottom horizontal box

      JButton button1 = new JButton("Ok");
      JButton button2 = new JButton("Reset");
      JButton button3 = new JButton("close");

      hbox3 = Box.createHorizontalBox();
      hbox3.add(button1);
      // use "glue" to push the two buttons apart
      hbox3.add(Box.createGlue());
      hbox3.add(button2);
      hbox3.add(Box.createGlue());
      hbox3.add(button3);
      button1.addActionListener(new OkHandle());
      button2.addActionListener(new CancelHandle()); 
      button3.addActionListener( new CloseHandle());

      //Radio button and panel
      jb1 = new JRadioButton("摄氏温度转华氏温度");
      jb2 = new JRadioButton("华氏温度转摄氏温度");
      ButtonGroup bg = new ButtonGroup();
      bg.add(jb1);
      bg.add(jb2);
      Box hbox4 =Box.createHorizontalBox();
      hbox4.add(jb1);
      hbox4.add(Box.createHorizontalStrut(10));
      hbox4.add(jb2);
      jb1.setSelected(true);
      
      jb1.addActionListener(new RBHandle());
      jb2.addActionListener(new RBHandle());
      
      //add the three horizontal boxes inside a vertical box
      vbox = Box.createVerticalBox();
      vbox.add(hbox4);
      vbox.add(hbox1);
      vbox.add(hbox2);
      vbox.add(Box.createGlue());
      vbox.add(hbox3);
      
      //初始化文本框2不可编辑
      textField2.setEditable(false);
      
      add(vbox, BorderLayout.CENTER);
   }
   
   class OkHandle implements ActionListener {
	   
	   public void actionPerformed(ActionEvent e) {
			try {
				//没有输入时
				if (textField1.getText().trim().length() == 0
						&& textField2.getText().trim().length() == 0)
					throw new Exception("请输入温度值");
				//两个文本框都有输入时
				if (textField1.getText().trim().length() > 0
						&& textField2.getText().trim().length() > 0)
					throw new Exception("请选择一种温度值输入");
				
				//有且仅有一个文本框中有输入时
				if (textField1.getText().trim().length() > 0) {
					String s = textField1.getText().trim();
					double d = Double.parseDouble(s);
					Temperature temper = new Temperature();
					temper.setCelsius(d);
					textField2.setText(temper.getFByStr());
				} else {
					String s = textField2.getText().trim();
					double d = Double.parseDouble(s);
					Temperature temper = new Temperature();
					temper.setFahr(d);
					textField1.setText(temper.getCByStr());
				}

			} catch (Exception e1) {
				JOptionPane.showMessageDialog(null, e1.getMessage());
			}
		}

	}
   
   class CancelHandle implements ActionListener{

	public void actionPerformed(ActionEvent e) {
		reset();
	}   
   }
   
   private void reset(){
	   textField1.setText("");
	   textField2.setText("");
   }
   
   class RBHandle implements ActionListener{

	public void actionPerformed(ActionEvent e) {
		  Object src = e.getSource();
		  if(src == jb1){
			  textField1.setEditable(true);
			  textField2.setEditable(false);
			  //调用自定义的reset方法,清空文本框中的内容
			  reset();
		  }else{
			  textField1.setEditable(false);
			  textField2.setEditable(true); 
//		      //调用自定义的reset方法,清空文本框中的内容
		      reset();
		  }
	}
	   
   } 

   private JTextField textField1,textField2;
   private Box hbox1,hbox2,hbox3,vbox;
   private JRadioButton jb1,jb2;
   public static final int DEFAULT_WIDTH = 300;
   public static final int DEFAULT_HEIGHT = 300;  
}

class CloseHandle implements ActionListener{

	public void actionPerformed(ActionEvent e) {
      //showConfirmDialog()return int,0=yes,1=no,2=cancel
	  if(JOptionPane.showConfirmDialog(null,"你是否要退出程序?")==0){
    	  System.exit(0); 
     }		
 }	
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -