随便写了一些,练练SWING界面

本文介绍了一个使用Java Swing实现的简易文本编辑器,包括文件读写功能和简单的对话框提示。该编辑器集成了文本区域、按钮和下拉菜单等组件,并通过监听器实现了保存和读取文件的功能。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

package MyText2;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.FocusEvent;
import java.awt.event.FocusListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.*;

import javax.swing.*;


class CreateIcon implements Icon 
{
	int Height;
	int Width;
	@Override
	public int getIconHeight() {
		// TODO 自动生成的方法存根
		return Height;
	}

	@Override
	public int getIconWidth() {
		// TODO 自动生成的方法存根
		return Width;
	}
	public CreateIcon(int Width, int Height)
	{
		this.Width = Width;
		this.Height = Height;
	}
	@Override
	public void paintIcon(Component arg0, Graphics arg1, int arg2, int arg3) {
		// TODO 自动生成的方法存根
		arg1.fillOval(arg2, arg3, Width, Height);
		arg1.fillOval(arg2 + 70, arg3, Width, Height);
	}
	
}
class CreateWriteDialog extends JDialog
{
	public CreateWriteDialog(JFrame jframe)
	{
		super(jframe, "提示");
		Container container = getContentPane();
		container.setBackground(Color.white);
		Icon icon = new CreateIcon(10, 10);
		JLabel jlabel = new JLabel("保存成功", icon, SwingConstants.CENTER);
		container.add(jlabel);
		setVisible(true);
		setBounds(450, 300, 200, 100);
	}
}

class CreateReadDialog extends JDialog
{
	public CreateReadDialog(JFrame jframe)
	{
		super(jframe, "提示");
		Container container = getContentPane();
		container.setBackground(Color.white);
		Icon icon = new CreateIcon(10, 10);
		JLabel jlabel = new JLabel("读取成功", icon, SwingConstants.CENTER);
		container.add(jlabel);
		setVisible(true);
		setBounds(450, 300, 200, 100);
	}
}

class CreateComboBox extends AbstractListModel implements ComboBoxModel
{
	String str;
	String data[] = {"A", "B", "C", "D"};
	@Override
	public Object getElementAt(int arg0) {
		// TODO 自动生成的方法存根
		return data[arg0];
	}

	@Override
	public int getSize() {
		// TODO 自动生成的方法存根
		return data.length;
	}

	@Override
	public Object getSelectedItem() {
		// TODO 自动生成的方法存根
		return str;
	}

	@Override
	public void setSelectedItem(Object arg0) {
		// TODO 自动生成的方法存根
		str = (String)arg0;
	}
	
}

public class MyText {
	public static void main (String []args)
	{
		final JFrame jframe = new JFrame("界面");
		Container container = jframe.getContentPane();
		container.setBackground(Color.white);
		container.setLayout(new BorderLayout());
		/*************************************/
		JPanel textpanel = new JPanel(new BorderLayout());
		final JTextArea textarea = new JTextArea();
		textarea.addFocusListener(new FocusListener()
		{
			public void focusLost(FocusEvent e)
			{
				JOptionPane.showMessageDialog(null, "别忘了保存文档哦");
			}
			public void focusGained(FocusEvent e)
			{
				
			}
		});
		jframe.addWindowListener(new WindowAdapter(){
			public void windowOpened(WindowEvent e) {
				textarea.requestFocus();
			}
		});
		textarea.setLineWrap(true);
		JScrollPane scrollpane = new JScrollPane(textarea);
		textpanel.add(scrollpane);
		container.add(BorderLayout.CENTER, textpanel);
		/*************************************/
		JPanel buttonpanel = new JPanel(new FlowLayout());
		JLabel jlabel = new JLabel("选择选项");
		JComboBox combobox = new JComboBox(new CreateComboBox());
		JButton writebutton = new JButton("保存");
		writebutton.addActionListener(new ActionListener()
		{
			public void actionPerformed(ActionEvent e)
			{
				new CreateWriteDialog(jframe);
				File file1 = new File("G:/helloworld");
				try {
					File file = new File(file1, "helloworld.txt");
					if(!file.exists())
						file.createNewFile();
					FileWriter write = new FileWriter(file);
					BufferedWriter in = new BufferedWriter(write);
					String str = textarea.getText();
					in.write(str);
					in.close();
					write.close();
				} catch (IOException e1) {
					// TODO 自动生成的 catch 块
					e1.printStackTrace();
				}
				
			}
		});
		JButton readbutton = new JButton("读取");
		readbutton.addActionListener(new ActionListener()
		{
			public void actionPerformed(ActionEvent e)
			{
				new CreateReadDialog(jframe);
				File file1 = new File("G:/helloworld");
				try{
					File file = new File(file1, "helloworld.txt");
					FileReader read = new FileReader(file);
					BufferedReader in = new BufferedReader(read);
					String str = in.readLine();
					textarea.setText(str);
					read.close();
					in.close();
				}catch(IOException e1)
				{
					e1.printStackTrace();
				}
			}
		});
		buttonpanel.add(jlabel);
		buttonpanel.add(combobox);
		buttonpanel.add(writebutton);
		buttonpanel.add(readbutton);
		container.add(BorderLayout.SOUTH, buttonpanel);
		/*************************************/
		jframe.setVisible(true);
		jframe.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
		jframe.setBounds(400, 250, 400, 300);
	}
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值