【JAVA】Java简单组件的应用

import java.awt.BorderLayout;
import java.awt.Frame;
import java.awt.Panel;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.IOException;
import java.util.Scanner;

import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import javax.swing.JTextArea;
import javax.swing.JTextField;
public class Main {

	 static int flag;

	public static void main(String[] args) {
        JFrame frame = new JFrame("HKY");
        frame.setBounds(400, 200, 500, 400);
        frame.setLayout(new BorderLayout());
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        JPanel northPanel = new JPanel();
        JTextField textField = new JTextField(20);
        JButton button = new JButton("确认");
        northPanel.add(textField);
        northPanel.add(button);
        frame.add(northPanel, BorderLayout.NORTH);

        JPanel centerPanel = new JPanel();
        JTextArea textArea = new JTextArea(10, 30);
        centerPanel.add(textArea);
        frame.add(centerPanel, BorderLayout.CENTER);

        String[] options = {"新建", "打开", "保存","另存为"};
        JComboBox<String> comboBox = new JComboBox<>(options);
        frame.add(comboBox, BorderLayout.SOUTH);
/下面的代码是设置监听事件
        comboBox.addActionListener(new ActionListener()
        		{


					@Override
					public void actionPerformed(ActionEvent e) {
						// TODO Auto-generated method stub
						JComboBox<String> cb = (JComboBox<String>) e.getSource();
						String option = (String) cb.getSelectedItem();
	                    if(option.equals("新建"))
	                    {
	                    	 flag = 1;
	                    	System.out.println("请输入文件地址");
	                    }
	                    if(option.equals("打开"))
	                    {
	                    	 flag = 2;
	                    }
	                    if (option.equals("保存")) {
	                        flag = 3;
	                        System.out.println("请输入要保存文件的路径");
	                    }
	                    if (option.equals("另存为")) {
	                        flag = 4;
	                        System.out.println("请输入要另存为的文件路径");
	                    }
  					}
        	
        		}
        		);
        button.addActionListener(new ActionListener()
		{
			@Override
			public void actionPerformed(ActionEvent e) {
				// TODO Auto-generated method stub
	               if(flag==1)
	               {
	                   String filepath = (String)textField.getText();//"E://123.txt"
	                   File file = new File(filepath);
	                   try {
	                       file.createNewFile();
	                       System.out.println("文件创建成功!");
	                   } catch (IOException e1) {
	                       throw new RuntimeException(e1);
	                   }

	               }
	               if(flag==2)
	               {
	            	   System.out.println("请输入您要打开文件的路径");
	            	   String filepath = (String)textField.getText();
	            	   openFile(filepath, textArea);
	               }
	               if (flag == 3) {
	                   String filepath = textField.getText();
	                   saveFile(filepath, textArea.getText());
	               }
	               if (flag == 4) {
	                   String filepath = textField.getText();
	                   saveFileAs(filepath, textArea.getText());
	               }
			}
	
		});
        frame.setVisible(true);
	}
	private static void openFile(String filePath, JTextArea textArea) {
        try {
            FileReader reader = new FileReader(filePath);
            char[] buffer = new char[1024];
            int len = 0;
            while ((len = reader.read(buffer)) != -1
            textArea.append(new String(buffer, 0, len));
            }
            reader.close();
        } catch (IOException e1) {
            throw new RuntimeException(e1);
        }
        
    }
	private static void saveFile(String filePath, String content) {
	    try {
	        FileOutputStream outputStream = new FileOutputStream(filePath);
	        outputStream.write(content.getBytes());
	        outputStream.close();
	        System.out.println("文件保存成功!");
	    } catch (IOException e) {
	        throw new RuntimeException(e);
	    }
	}
	private static void saveFileAs(String filePath,String content) 
	{ JFrame saveFrame = new JFrame("另存为");
	saveFrame.setBounds(400, 200, 400, 200); 
	saveFrame.setLayout(new BorderLayout()); 
	JTextField saveTextField = new JTextField(20); 
	JButton saveButton = new JButton("保存"); 
	JPanel savePanel = new JPanel(); 
	savePanel.add(saveTextField); 
	savePanel.add(saveButton); 
	saveFrame.add(savePanel, BorderLayout.NORTH); 
	saveButton.addActionListener(new ActionListener() 
	{ @Override public void actionPerformed(ActionEvent e) 
	{ String filePath = saveTextField.getText(); 
	saveFile(filePath, content);
	saveFrame.dispose(); 
	System.out.println("文件另存为成功!"); 
	} });
	saveFrame.setVisible(true); }
}
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

是唐不是傻

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值