一道JAVA题,会的帮下忙
发布网友
发布时间:2023-07-19 11:18
我来回答
共5个回答
热心网友
时间:2023-07-26 22:04
写了个,代码如下:
import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
public class SaveContent {
public static void main(String[] args) throws Exception {
SaveContentFrame sf = new SaveContentFrame();
sf.launchFrame();
}
}
class SaveContentFrame {
JTextField jtf;
JTextArea jta;
JPanel jp;
JButton jb;
public void launchFrame() {
JFrame frame = new JFrame("内容存储器"); // 创建 frame对象
frame.setSize(400, 300); // 设置fram大小
frame.setLocation(300, 250);
jta = new JTextArea();
jtf = new JTextField();
frame.getContentPane().add(new JScrollPane(jta));
jp = new JPanel();
jp.setLayout(new BorderLayout());
jp.add(jtf, BorderLayout.CENTER);
jb = new JButton("保存");
jb.addActionListener(new ActionListener() { // 注册*
public void actionPerformed(ActionEvent arg0) {
saveContent();
}
});
jp.add(jb, BorderLayout.EAST);
frame.getContentPane().add(jp, "South");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
public void saveContent() { // 输出文字
String file = this.jtf.getText();
if(file==null || file.length()==0) {
file = "c:\\untitle.txt";
}
String content = jta.getText();
if(content==null || content.length()==0) {
JOptionPane.showMessageDialog(null, "内容不能为空,请输入内容!");
return;
}
byte[] b = content.getBytes();
try {
FileOutputStream fos = new FileOutputStream(file);
fos.write(b);
fos.flush();
fos.close();
JOptionPane.showMessageDialog(null, "保存成功!");
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
JOptionPane.showMessageDialog(null, "该文件不存在,请重新输入文件名!");
} catch (IOException e) {
// TODO Auto-generated catch block
JOptionPane.showMessageDialog(null, "保存失败!");
}
}
}
热心网友
时间:2023-07-26 22:04
我给你写个例子:
import java.awt.BorderLayout;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.io.BufferedWriter;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.FilterWriter;
import java.io.IOException;
import javax.swing.JButton;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.WindowConstants;
import javax.swing.SwingUtilities;
public class Test extends javax.swing.JFrame {
private JPanel jPanel1;
private JButton bntok;
private JTextArea jTextArea1;
private JScrollPane jScrollPane1;
private JTextField filename;
public static void main(String[] args) {
Test inst = new Test();
}
public Test() {
super();
initGUI();
}
private void initGUI() {
try {
setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
{
jPanel1 = new JPanel();
getContentPane().add(jPanel1, BorderLayout.CENTER);
jPanel1.setLayout(null);
{
filename = new JTextField();
jPanel1.add(filename);
filename.setBounds(48, 12, 108, 22);
}
{
jScrollPane1 = new JScrollPane();
jPanel1.add(jScrollPane1);
jScrollPane1.setBounds(48, 56, 290, 140);
{
jTextArea1 = new JTextArea();
jScrollPane1.setViewportView(jTextArea1);
jTextArea1.setText("");
}
}
{
bntok = new JButton();
jPanel1.add(bntok);
bntok.setText("保存");
bntok.setBounds(198, 12, 77, 32);
bntok.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
String file = filename.getText();
if (file.length() == 0) {
file = "untitle.txt";
}
try {
BufferedWriter writer = new BufferedWriter(
new FileWriter(file));
writer.write(jTextArea1.getText());
writer.flush();
writer.close();
JOptionPane
.showMessageDialog(Test.this, "保存成功");
} catch (IOException e1) {
// TODO Auto-generated catch block
JOptionPane
.showMessageDialog(Test.this, "保存失败");
}
}
});
}
}
pack();
setSize(400, 300);
} catch (Exception e) {
e.printStackTrace();
}
}
}
热心网友
时间:2023-07-26 22:05
这是如何创建文件的例子,你看看,其他的都是很简单的了
import java.io.*;
public class Demo
{
public static void main( String[] args)
{
File dirFile;
File tempFile;
boolean bFile;
String sFileName;
bFile = false;
try
{
dirFile = new File("E:\\test");
bFile = dirFile.exists();
if( bFile == true )
{
System.out.println("The folder exists.");
}
else
{
System.out.println("The folder do not exist,now trying to create a one...");
bFile = dirFile.mkdir();
if( bFile == true )
{
System.out.println("Create successfully!");
}
else
{
System.out.println("Disable to make the folder,please check the disk is full or not.");
System.exit(1);
}
}
System.out.println("Now we put files in to the folder...");
for(int i=0; i<5; i++)
{
sFileName = new String("E:\\test\\");
sFileName += String.valueOf(i);
tempFile = new File(sFileName);
bFile = tempFile.createNewFile();
}
}catch(IOException e)
{
// Exception hadler
}
if( bFile == true )
System.out.println("Successfully put files in to the folder!");
else
System.out.println("Sorry sir,i don't finish the job!");
}
}
热心网友
时间:2023-07-26 22:05
确实很简单嘛!
你用图形工具画一下上面的控件,然后就需要写一个button的onClick事件了!这过程你都写出来了,根本不用我说了
热心网友
时间:2023-07-26 22:06
不会 swing编程的人 当然不会 不过你可以到网上找找吧 我现在写这个也不太回了狠久没用了