如何向java jframe中添加下拉列表 按钮 文本框 最后把信息存储在文件里
3个回答
展开全部
lz 你好
(ps: lz 加点儿分吧 太少了……)
具体代码如下:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.io.*;
public class Test extends JFrame {
private JLabel name,phone,sex;
private JTextField inputName,inputPhone;
private JComboBox sexBox;
private String[] item = {"男", "女"};
private JButton save;
public Test() {
super("信息管理");
setSize(240,300);
setLayout(new FlowLayout(FlowLayout.CENTER, 10, 30));
name = new JLabel("姓名:");
phone = new JLabel("手机号:");
sex = new JLabel("性别:");
inputName = new JTextField(14);
inputPhone = new JTextField(14);
sexBox = new JComboBox(item);
save = new JButton("储存");
save.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if(inputName.getText().equals("") || inputPhone.getText().equals("")) {
return;
}
try {
File f = new File("info.txt");
BufferedWriter bw = new BufferedWriter(new FileWriter(f, true));
if(f.length() == 0){
bw.write("姓名\t性别\t手机号");
bw.newLine();
}
String str = inputName.getText()+"\t"+sexBox.getSelectedItem()+"\t"+inputPhone.getText();
bw.write(str);
bw.newLine();
bw.close();
} catch (Exception ex) {
}
}
});
getContentPane().setBackground(Color.WHITE);
getContentPane().add(Box.createHorizontalStrut(5));
getContentPane().add(name);
getContentPane().add(inputName);
getContentPane().add(phone);
getContentPane().add(inputPhone);
getContentPane().add(Box.createHorizontalStrut(35));
getContentPane().add(sex);
getContentPane().add(sexBox);
getContentPane().add(Box.createHorizontalStrut(35));
getContentPane().add(save);
setLocationRelativeTo(null);
setVisible(true);
setDefaultCloseOperation(3);
}
public static void main (String[] args) {
new Test();
}
}
希望能帮助你哈
追问
大哥~谢谢你,能注释一下吗?非常感谢~!
展开全部
写了一个例子,你可以看一下。该例子可以读取txt文件并显示在文本框中,也可以读取图片文件,显示在JLabel上,有问题再追问,good luck!
import java.awt.TextArea;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextField;
public class Test2 extends JFrame implements ActionListener {
JButton button;
JButton Select;
JButton btnOK;
JTextField textfield;
JPanel p;
JFileChooser fc = new JFileChooser();
TextArea area;
public Test2() {
p = new JPanel(); // 建立一个面板
this.getContentPane().add(p);// 把面板添加到框架
p.add(new JButton("文本"));// 把一个文本按钮添加到面板
textfield = new JTextField(10);
p.add(textfield); // 把一个文本框添加到面板
Select = new JButton("浏览");
p.add(Select); // 把一个浏览按钮添加到面板
Select.addActionListener(this);
btnOK = new JButton("确定");
p.add(btnOK);// 把一个确定按钮添加到面板
btnOK.addActionListener(this);
}
public void actionPerformed(ActionEvent e) {
// 当按下选择按钮,打开一个文件选择,文本框显示文件路径
if (e.getSource() == Select) {
int intRetVal = fc.showOpenDialog(this);
if (intRetVal == JFileChooser.APPROVE_OPTION) {
textfield.setText(fc.getSelectedFile().getPath());
}
} else if (e.getSource() == btnOK) { // 当按下确定按钮,生成一个新框架,框架里面有一个文本域,显示打开文件的内容
JFrame f = new JFrame();
f.setSize(400, 400);
f.setLocationRelativeTo(null);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
String extensionName = getExtensionName(textfield.getText());
if ("txt".equals(extensionName)) {
f.setTitle("显示文本");
area = new TextArea();
//获取文本值
String text = readTxt(textfield.getText());
area.setText(text);
f.add(area);
f.setVisible(true);
} else if ("jpg".equals(extensionName) || "png".equals(extensionName) || "gif".equals(extensionName)) {
f.setTitle("显示图片");
Icon img = new ImageIcon(textfield.getText());
JLabel label = new JLabel(img);
//添加滚动条
JScrollPane jsp = new JScrollPane(label);
f.add(jsp);
f.setVisible(true);
} else {
JOptionPane.showMessageDialog(null, "请选择txt/jpg/png/gif格式的文件!");
}
}
}
/**
* @Description:获取文件后缀名
* @param filename
* @return
* @throws
*/
private String getExtensionName(String filename) {
if ((filename != null) && (filename.length() > 0)) {
int dot = filename.lastIndexOf('.');
if ((dot > -1) && (dot < (filename.length() - 1))) {
return filename.substring(dot + 1);
}
}
return filename;
}
/**
* @Description:读取文件
* @param path - 文件地址
* @return
* @throws
*/
private String readTxt(String path) {
if (path == null || "".equals(path)) {
return "";
}
StringBuffer sb = new StringBuffer();
File file = new File(path);
InputStreamReader read = null;
BufferedReader reader = null;
try {
read = new InputStreamReader(new FileInputStream(file), "gb2312");
reader = new BufferedReader(read);
String line;
while ((line = reader.readLine()) != null) {
sb.append(line);
sb.append("\n");
}
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (read != null) {
try {
read.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if (reader != null) {
try {
reader.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
return sb.toString();
}
public static void main(String[] args) {
Test2 frame = new Test2();
frame.setSize(400, 400);
frame.setLocationRelativeTo(null);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
}
import java.awt.TextArea;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextField;
public class Test2 extends JFrame implements ActionListener {
JButton button;
JButton Select;
JButton btnOK;
JTextField textfield;
JPanel p;
JFileChooser fc = new JFileChooser();
TextArea area;
public Test2() {
p = new JPanel(); // 建立一个面板
this.getContentPane().add(p);// 把面板添加到框架
p.add(new JButton("文本"));// 把一个文本按钮添加到面板
textfield = new JTextField(10);
p.add(textfield); // 把一个文本框添加到面板
Select = new JButton("浏览");
p.add(Select); // 把一个浏览按钮添加到面板
Select.addActionListener(this);
btnOK = new JButton("确定");
p.add(btnOK);// 把一个确定按钮添加到面板
btnOK.addActionListener(this);
}
public void actionPerformed(ActionEvent e) {
// 当按下选择按钮,打开一个文件选择,文本框显示文件路径
if (e.getSource() == Select) {
int intRetVal = fc.showOpenDialog(this);
if (intRetVal == JFileChooser.APPROVE_OPTION) {
textfield.setText(fc.getSelectedFile().getPath());
}
} else if (e.getSource() == btnOK) { // 当按下确定按钮,生成一个新框架,框架里面有一个文本域,显示打开文件的内容
JFrame f = new JFrame();
f.setSize(400, 400);
f.setLocationRelativeTo(null);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
String extensionName = getExtensionName(textfield.getText());
if ("txt".equals(extensionName)) {
f.setTitle("显示文本");
area = new TextArea();
//获取文本值
String text = readTxt(textfield.getText());
area.setText(text);
f.add(area);
f.setVisible(true);
} else if ("jpg".equals(extensionName) || "png".equals(extensionName) || "gif".equals(extensionName)) {
f.setTitle("显示图片");
Icon img = new ImageIcon(textfield.getText());
JLabel label = new JLabel(img);
//添加滚动条
JScrollPane jsp = new JScrollPane(label);
f.add(jsp);
f.setVisible(true);
} else {
JOptionPane.showMessageDialog(null, "请选择txt/jpg/png/gif格式的文件!");
}
}
}
/**
* @Description:获取文件后缀名
* @param filename
* @return
* @throws
*/
private String getExtensionName(String filename) {
if ((filename != null) && (filename.length() > 0)) {
int dot = filename.lastIndexOf('.');
if ((dot > -1) && (dot < (filename.length() - 1))) {
return filename.substring(dot + 1);
}
}
return filename;
}
/**
* @Description:读取文件
* @param path - 文件地址
* @return
* @throws
*/
private String readTxt(String path) {
if (path == null || "".equals(path)) {
return "";
}
StringBuffer sb = new StringBuffer();
File file = new File(path);
InputStreamReader read = null;
BufferedReader reader = null;
try {
read = new InputStreamReader(new FileInputStream(file), "gb2312");
reader = new BufferedReader(read);
String line;
while ((line = reader.readLine()) != null) {
sb.append(line);
sb.append("\n");
}
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (read != null) {
try {
read.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if (reader != null) {
try {
reader.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
return sb.toString();
}
public static void main(String[] args) {
Test2 frame = new Test2();
frame.setSize(400, 400);
frame.setLocationRelativeTo(null);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询