我想只知道为什么点击菜单的某个功能无法实现,求解下。
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文件无法实现换行
我已经弄了很久了,分有点少,谁能帮下啊
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文件无法实现换行
我已经弄了很久了,分有点少,谁能帮下啊
...全文
相关推荐
仿网易点击左侧菜单右侧出现选项卡功能
仿网易邮箱的点击左侧菜单右侧出现可关闭的选项卡功能,jquery+css+div代码
VC++实现的数独游戏与自动求解功能
数独是一种经典的逻辑填数字游戏,广泛流行于全球各地。它由一个9x9的网格组成,这个大网格进一步被划分为9个3x3的小网格。数独游戏的目标是在每个小格子里填入1到9的数字,使得每一行、每一列以及每一个小网格内的数字都不重复,且每个数字恰好出现一次。
简单实现js点击展开二级菜单功能
主要教大家简单实现js点击展开二级菜单功能,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
Bootstrap导航菜单点击后无法自动添加active的处理方法
今天小编就为大家分享一篇Bootstrap导航菜单点击后无法自动添加active的处理方法,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
jQuery实现的点击显示隐藏下拉菜单功能完整示例
本文实例讲述了jQuery实现的点击显示隐藏下拉菜单功能。分享给大家供大家参考,具体如下: <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>toggle</title> [removed][removed] <style> *{margin: 0;padding: 0} .nav1>li>ul{ display: none} .nav1>li{


