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

qq_33689013 qq_33689013 墨衍会员 · AI 创作全网分发 墨衍智能分发 通过墨衍一键同步至各平台 可同步平台 CSDN微信公众号微博知乎掘金百家号博客园抖音小红书 你的文章也可以这样分发 墨衍会员 ¥399起/年,写一次发全网 我也要 + 加入社区
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文件无法实现换行
我已经弄了很久了,分有点少,谁能帮下啊
...全文

相关推荐

评论
收藏 已收藏
分享
返回顶部
qq_33689013
Java 51,407 成员 86,128 内容