编写两个文本框,在一个文本框中输入数字,同时在另一
个文本框中显示和以及平均值,并随着第一个文本框的变化不断变化
我主要看看事件处理怎么写啊??
java初学者。。。。。。。。。。。。。。。。。。。。。
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.io.*;
public class MyTextEditor extends JFrame implements ActionListener,ItemListener,MouseListener
{
private File file;
private JTextArea textarea;
private JRadioButtonMenuItem rbmi_red,rbmi_blue,rbmi_green;
private JMenuItem menuitem_copy,menuitem_cut,menuitem_paste,menuitem_seek;
private JMenuItem menuitem_song,menuitem_fang,menuitem_kai;//字体变量
private JMenuItem menuitem_normal,menuitem_bold,menuitem_italic;//字形变量
private JMenuItem menuitem_20,menuitem_30,menuitem_40;//字号变量
private JMenuItem menuitem_exit,menuitem_infor;
private JPopupMenu popupmenu;
private JMenuItem menuitem_red,menuitem_green,menuitem_blue;
private JDialog dialog,dialog1;
private JButton button_seek;
private JTextField textfield_seek;
private JLabel label_seek,label_infor;
String seek;
public MyTextEditor()
{
super("文本编辑器");
this.setSize(400,300);
this.setLocation(360,300);
this.setDefaultCloseOperation(HIDE_ON_CLOSE);
Container ss=this.getContentPane();
this.textarea = new JTextArea();
JScrollPane dd=new JScrollPane(textarea);
ss.add(dd);
textarea.addMouseListener(this);
this.addMenu();
this.setVisible(true);
this.Dialog();
this.Dialog1();
this.file = null;
}
public MyTextEditor(String filename)
{
this();
if (filename!=null)
{
this.file = new File(filename);
this.setTitle(filename);
this.textarea.setText(this.readFromFile());
}
}
public MyTextEditor(File file)
{
this();
if (file!=null)
{
this.file = file;
this.setTitle(this.file.getName());
this.textarea.setText(this.readFromFile());
}
}
public void Dialog() //建立对话框的方法
{
dialog=new JDialog(this,"查找",true);
dialog.setLayout(new FlowLayout());
dialog.setSize(200,90);
label_seek=new JLabel("查找内容");
dialog.add(label_seek);
textfield_seek=new JTextField(10);
dialog.add(textfield_seek);
button_seek=new JButton("查找");
dialog.add(button_seek);
button_seek.addActionListener(this);
}
public void Dialog1()
{
dialog1=new JDialog(this,"专利",true);
dialog1.setLayout(new FlowLayout());
dialog1.setSize(200,100);
label_infor=new JLabel("刘同虎制作");
dialog1.add(label_infor);
}
public void addMenu()
{
JMenuBar menubar = new JMenuBar();
this.setJMenuBar(menubar);
JMenu menu_file = new JMenu("文件"); //文件菜单
menubar.add(menu_file);
JMenuItem menuitem_open = new JMenuItem("打开");
menu_file.add(menuitem_open);
menuitem_open.addActionListener(this);
JMenuItem menuitem_save = new JMenuItem("保存");
menu_file.add(menuitem_save);
menuitem_save.addActionListener(this);
JMenuItem menuitem_saveas = new JMenuItem("另存为");
menu_file.add(menuitem_saveas);
menuitem_saveas.addActionListener(this);
menuitem_exit=new JMenuItem("退出" );
menu_file.add(menuitem_exit);
menuitem_exit.addActionListener(this);
menuitem_infor=new JMenuItem("信息");
menu_file.add(menuitem_infor);
menuitem_infor.addActionListener(this);
JMenu menu_editor=new JMenu("编辑");//编辑菜单
menubar.add(menu_editor);
menuitem_seek=new JMenuItem("查找");
menu_editor.add(menuitem_seek);
menuitem_seek.addActionListener(this);
menuitem_copy=new JMenuItem("复制");
menuitem_copy.addActionListener(this);
menu_editor.add(menuitem_copy);
menuitem_cut=new JMenuItem("剪切");
menu_editor.add(menuitem_cut);
menuitem_cut.addActionListener(this);
menuitem_paste=new JMenuItem("粘贴");
menu_editor.add(menuitem_paste);
menuitem_paste.addActionListener(this);
JMenuItem menu_color=new JMenu("颜色");//颜色菜单
menu_editor.add(menu_color);
ButtonGroup buttongroup=new ButtonGroup();
rbmi_red=new JRadioButtonMenuItem("红",true);
buttongroup.add(rbmi_red);
menu_color.add(rbmi_red);
rbmi_red.addItemListener(this);
rbmi_blue=new JRadioButtonMenuItem("蓝",true);
buttongroup.add(rbmi_blue);
menu_color.add(rbmi_blue);
rbmi_blue.addItemListener(this);
rbmi_green=new JRadioButtonMenuItem("绿",true);
buttongroup.add(rbmi_green);
menu_color.add(rbmi_green);
rbmi_green.addItemListener(this);
JMenu menu_font=new JMenu("设置字体");//设置字体菜单
menubar.add(menu_font);
menuitem_song=new JMenuItem("宋体");
menu_font.add(menuitem_song);
menuitem_song.addActionListener(this);
menuitem_fang=new JMenuItem("仿宋");
menu_font.add(menuitem_fang);
menuitem_fang.addActionListener(this);
menuitem_kai=new JMenuItem("楷体");
menu_font.add(menuitem_kai);
menuitem_kai.addActionListener(this);
JMenu menu_style=new JMenu("设置字形");//设置字形菜单
menubar.add(menu_style);
menuitem_bold=new JMenuItem("粗体");
menu_style.add(menuitem_bold);
menuitem_bold.addActionListener(this);
menuitem_italic=new JMenuItem("斜体");
menu_style.add(menuitem_italic);
menuitem_italic.addActionListener(this);
JMenu menu_size=new JMenu("设置字号"); //设置字号菜单
menubar.add(menu_size);
menuitem_20=new JMenuItem("20");
menu_size.add(menuitem_20);
menuitem_20.addActionListener(this);
menuitem_30=new JMenuItem("30");
menu_size.add(menuitem_30);
menuitem_30.addActionListener(this);
menuitem_40=new JMenuItem("40");
menu_size.add(menuitem_40);
menuitem_40.addActionListener(this);
popupmenu=new JPopupMenu(); //快捷菜单
JMenuItem menuitem_red=new JMenuItem("红色");
popupmenu.add(menuitem_red);
menuitem_red.addActionListener(this);
JMenuItem menuitem_green=new JMenuItem("绿色");
popupmenu.add(menuitem_green);
menuitem_green.addActionListener(this);
menuitem_blue=new JMenuItem("蓝色");
popupmenu.add(menuitem_blue);
menuitem_blue.addActionListener(this);
textarea.add(popupmenu); //向文本区内添加快捷菜单
}
public void writeToFile(String lines) //写文件
{
try
{
FileWriter fout = new FileWriter(this.file);
fout.write(lines+"\r\n");
fout.close();
}
catch (IOException ioex)
{
return;
}
}
public String readFromFile() //读文件
{
try
{
FileReader fin = new FileReader(this.file);
BufferedReader bin = new BufferedReader(fin);
String aline="", lines="";
do
{
aline = bin.readLine();
if (aline!=null)
lines += aline + "\r\n";
} while (aline!=null);
bin.close();
fin.close();
return lines;
}
catch (IOException ioex)
{
return null;
}
}试试这个
javascrict就可以实现了,onkeyup事件来处理,问题是没有明白“在一个文本框中输入数字”?一个文本框输入两个数字吗?一个数字有怎么求和和平均值呢?
编写两个文本框,在一个文本框中输入数字,同时在另一
个文本框中显示和以及平均值,并随着第一个文本框的变化不断变化
我主要看看事件处理怎么写啊??
和 或者是 平均数 不都需要2个数字吗,你输入一个数字?
楼上的方法正解啊
楼上正解