我想只知道为什么点击菜单的某个功能无法实现,求解下。

qq_33689013 2016-06-19 04:44:31
import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.IOException;
import java.io.RandomAccessFile;

/**
* Created by myroom on 2016/6/18.
*/
public class JieMian {
public static void main(String ags[]){
new Win();
}
}
class InputArea extends JPanel implements ActionListener{
Box box1,box2,box3;
JTextField name;
JTextField message;
JTextField phonenumber;
JButton button;
File f=null;
RandomAccessFile out;
InputArea(File f){
this.f=f;
name=new JTextField(12);
message=new JTextField(12);
phonenumber=new JTextField(12);
button=new JButton("录入");
button.addActionListener(this);
box1=Box.createVerticalBox();
box1.add(new JLabel(" 输入姓名 "));
box1.add(Box.createVerticalStrut(8));
box1.add(new JLabel(" 输入地址 "));
box1.add(Box.createVerticalStrut(8));
box1.add(new JLabel(" 输入电话 "));
box1.add(Box.createVerticalStrut(8));
box2=Box.createVerticalBox();
box2.add(name);
box2.add(Box.createVerticalStrut(8));
box2.add(message);
box2.add(Box.createVerticalStrut(8));
box2.add(phonenumber);
box2.add(Box.createVerticalStrut(8));
box2.add(button);
box2.add(Box.createVerticalStrut(8));
box3=Box.createHorizontalBox();
box3.add(box1);
box3.add(Box.createVerticalStrut(10));
box3.add(box2);
this.add(box3);
}

@Override
public void actionPerformed(ActionEvent e) {

try {
RandomAccessFile out=new RandomAccessFile(f,"rw");
if(f.exists()){
long length=f.length();
out.seek(length);
}
out.writeUTF("姓名:"+name.getText());
out.writeUTF("地址:"+message.getText());
out.writeUTF("电话:"+phonenumber.getText());
out.close();
}
catch(IOException ee){}
}
}

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.*;

/**
* Created by myroom on 2016/6/18.
*/
public class Win extends JFrame implements ActionListener {
File file = null;
JMenuBar bar;
JMenu fileMenu;
JMenuItem inputMenuItem, showMenuItem;
JTextArea show;
InputArea inputMessage;
CardLayout card = null;
JPanel p;

Win() {
file = new File("通讯录.txt");
inputMenuItem = new JMenuItem("录入");
showMenuItem = new JMenuItem("显示");
bar = new JMenuBar();
fileMenu = new JMenu("菜单");
fileMenu.add(inputMenuItem);
fileMenu.add(showMenuItem);
bar.add(fileMenu);
setJMenuBar(bar);
inputMenuItem.addActionListener(this);
showMenuItem.addActionListener(this);
inputMessage = new InputArea(file);
show = new JTextArea(20,20);
card = new CardLayout();
p = new JPanel();
p.setLayout(card);
p.add("inputMenuItem", inputMessage);
p.add("showMenuItem", show);
add(p, BorderLayout.CENTER);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
setBounds(100, 50, 420, 380);
validate();
}

@Override
public void actionPerformed(ActionEvent e) {

if (e.getSource()==inputMenuItem) {
card.show(p, "inputMenuItem");
}
else if (e.getSource()==showMenuItem) {
int number = 1;
show.setText(null);
card.show(p, "showMenuitem");
try {
RandomAccessFile in = new RandomAccessFile(file, "r");
String name = null;
while ((name = in.readUTF()) != null) {
show.append("\r\n" + number + " " + name);
show.append("\t" + in.readUTF());
show.append("\t" + in.readUTF());
show.append("\r\n-------------------------------- ");
number++;
}
in.close();
} catch (Exception ee) {}
}
}
}

具体有以下问题,他出现的界面文字与文本框不对齐,点击菜单的显示没反应,同时.txt文件无法实现换行
我已经弄了很久了,分有点少,谁能帮下啊
...全文
153 回复 打赏 收藏 举报
写回复
用AI写文章
回复
切换为时间正序
请发表友善的回复…
发表回复

51,407

社区成员

发帖
与我相关
我的任务
社区描述
Java相关技术讨论
javaspring bootspring cloud 技术论坛(原bbs)
社区管理员
  • Java相关社区
  • 小虚竹
  • 谙忆
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧