java JTextArea的简单实例

创建一个JFrame,在一个输入框中输入数字,然后乘以2显示在另一个文本框中,点击相应按钮实现相应功能。

package com.demo;

import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextArea;

class TemperatureFrame extends JFrame{
	private JButton jFahrenheit = new JButton("Fahrenheit");
	private JButton jCelsius = new JButton("Celsius");
	private JButton jclr = new JButton("Clear");
	private JTextArea jTextFahrenheit = new JTextArea(1, 5);
	private JTextArea jTextCelsius = new JTextArea(1, 5);
	private JPanel jPanelText = new JPanel();
	private JPanel jPanelButton = new JPanel();
	private WindowListener actionListener = new WindowListener();
	public TemperatureFrame(){
		creatWindow();
	}
	private  void creatWindow(){
		this.setLocation(200, 200);
		this.setSize(300, 300);
	    this.setLayout(new BorderLayout());
	    this.add(jPanelText, BorderLayout.CENTER);
	    this.add(jPanelButton, BorderLayout.SOUTH);
	    jPanelText.setLayout(new FlowLayout());
	    jPanelText.add(jTextFahrenheit);		
	    jPanelText.add(jTextCelsius);
	    jPanelText.add(jclr);
	    jPanelButton.add(jFahrenheit);
	    jPanelButton.add(jCelsius);
	    jFahrenheit.addActionListener(actionListener);
	    jCelsius.addActionListener(actionListener);
	    jclr.addActionListener(actionListener);
		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		//this.pack();
		this.setVisible(true);
	}
class WindowListener implements ActionListener{

	@Override
	public void actionPerformed(ActionEvent e) {
		if(e.getSource() == jFahrenheit){
		
			try{
				String fahrenheitValue = jTextFahrenheit.getText();
				System.out.println(fahrenheitValue);
				double faValue = Double.parseDouble(fahrenheitValue);
				double ceValue = faValue * 2 - 1.0;
				String celsiusValue = String.valueOf(ceValue);
				jTextCelsius.setText(celsiusValue);
			}
			catch(Exception ex){
				JOptionPane.showMessageDialog( null, "Please input values");
			}
		}
		else if(e.getSource() == jCelsius){
			
			try{
				String ceValue = jTextCelsius.getText();
				double ce = Double.parseDouble(ceValue);
				double feValue = ce / 2;
				String fe = String.valueOf(feValue);
				jTextFahrenheit.setText(fe);
			}
			catch(Exception ex){
				JOptionPane.showMessageDialog(null, "Please input values");
			}
		}
		else if(e.getSource() == jclr){
			jTextFahrenheit.setText(null);
			jTextCelsius.setText(null);
		}
	}
	
}
}
public class Demo {

	public static void main(String[] args) {
		new TemperatureFrame();
	}
}
Swing提供了3种文本输入输出组件,分别是JTextField,JPasswordField, JTextArea。其中JTextField只能实现单行文本的输入输出, JPasswordField把输出的文字信息
设置为其他显示字符(密码输入框采用这种形式),JTextArea实现多行文本的输入输出。这3个子类都是JTextComponent的子类。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值