Java 搭建一个可交互的窗体程序

简单地显示一个窗口所需的最少代码

import javax.swing.JFrame;

public class main {

	static class Win extends JFrame{//static不能少
		Win()
		{
			setVisible(true);//setv
		}
	}
	
	public static void main(String[] args) {
		// TODO 
		new Win();
	}
}

报错:No enclosing instance of type is accessible. Must qualify the allocation with an enclosing

解决方案一:

主程序是public static class main

在Java中,类中的静态方法不能直接调用动态方法。

静态main方法中只能调用静态类

在调用类前加static可解决

解决方案二(本质):不小心把窗体类写成了主类的内部类,应该写成外部类才对,这样就不用static了

import javax.swing.JFrame;

class Win extends JFrame{
		Win()
		{
			setVisible(true);//setv
		}
	}

public class main {
	public static void main(String[] args) {
		// TODO 
		new Win();
	}
}

显示一个单行文本框

类中

JTextArea path=new JTextArea();//JTA

构造函数中

add(path);

不可以使用path.add();

当有两个控件的时候,必须使用中间容器装载,不然有一个看不见

类中

JPanel top=new JPanel();//JPA

构造函数中

add(top);
top.setLayout(new GridLayout(2,1));//setl+gri
top.add(path);
top.add(text);

优化布局

一、将容器top放置于整个窗体的北区

add(top,BorderLayout.NORTH);//bord

二、将Text放置于容器Centre内部,Centre使用网格布局,仅有一行一列

类中

JPanel centre= new JPanel();

构造函数中

centre.setLayout(new GridLayout(1,1));

centre.add(text);

三、在容器top内部相对文本框的下方增加一个按钮栏bp,栏中存放按钮R

类中

JPanel bp=new JPanel();

构造函数中

top.add(bp,BorderLayout.CENTER);//若文本框指定了位置NORTH,按钮栏则不必指定位置Centre
bp.setLayout(new GridLayout(1,2));//一行两列
bp.add(R);

四、设置窗体位置、大小、标题和关闭方式

setLocation(400, 100);//setl

setTitle("文件读写");//sett

setSize(300, 200);//sets
setDefaultCloseOperation(EXIT_ON_CLOSE);//setd

这样,一个简易的窗体界面就布置好了

但是,这个界面目前只能看,没有交互

为窗体添加动作监听

在类名后声明接口

implements ActionListener //im act

为按键添加监听器

R.addActionListener(this);//adda this

在报错处自动添加动作实施函数

    public void actionPerformed(ActionEvent e) {
        // TODO 自动添加
        
    }

在函数内部添加按钮分支

Object s=e.getSource();//Object e.gets

if(s==R)
 {
            System.out.println("按下了读入键");
 }

运行效果:

这样,一个可交互的窗体就建立好了~

import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextArea;
import javax.swing.JTextField;

class Win extends JFrame implements ActionListener//im act
{
	JTextArea path=new JTextArea();//JTA
	JTextField text=new JTextField();//JTF
	JPanel top=new JPanel();//JPA
	JPanel centre= new JPanel();
	JPanel bp=new JPanel();
	JButton R=new JButton("读入");//JB
	Win()
	{
		add(top,BorderLayout.NORTH);//bord
		add(centre,BorderLayout.CENTER);
		top.setLayout(new GridLayout(2,1));//setl+gri
		centre.setLayout(new GridLayout(1,1));
		top.add(path,BorderLayout.NORTH);
		top.add(bp,BorderLayout.CENTER);
		bp.setLayout(new GridLayout(1,2));
		bp.add(R);
		centre.add(text);
		
		R.addActionListener(this);//adda this
		
		setLocation(400, 100);//setl
		setSize(300, 200);//sets
		setTitle("文件读写");//sett
		setDefaultCloseOperation(EXIT_ON_CLOSE);//setd
		setVisible(true);//setv
	}
	public void actionPerformed(ActionEvent e) {
		// TODO 自动添加
		Object s=e.getSource();
		if(s==R)
		{
			System.out.println("按下了读入键");
		}
	}
}


public class main {

	public static void main(String[] args) {
		// TODO 
		new Win();
	}
}

读写文件

获取路径

String pathString=path.getText();//gett

获取文件

File file=new File(pathString);

自动添加

读取文件

FileReader fileReader=new FileReader(file);//filer

有如下提示,则在动作实施函数中添加trycatch、将所有if语句丢进去

缓冲器内容读取(考试的时候、变量命名可以用自动的,节省时间)

BufferedReader bufferedReader=new BufferedReader(fileReader);//bu

文本读取

String line="";
String textString="";
while((line=bufferedReader.readLine())!=null)
{
         textString=textString+line+"\n";
}

文本显示

text.setText(textString);//sett

效果:

写入

FileWriter fileWriter=new FileWriter(file);//filew

fileWriter.write(text.getText());
fileWriter.close();

效果:

成功更改了文本内容

import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextArea;
import javax.swing.JTextField;

class Win extends JFrame implements ActionListener//im act
{
	JTextArea path=new JTextArea();//JTA
	JTextField text=new JTextField();//JTF
	JPanel top=new JPanel();//JPA
	JPanel centre= new JPanel();
	JPanel bp=new JPanel();
	JButton R=new JButton("读入");//JB
	JButton W=new JButton("写入");
	Win()
	{
		add(top,BorderLayout.NORTH);//bord
		top.setLayout(new GridLayout(2,1));//setl+gri
		top.add(path,BorderLayout.NORTH);
		top.add(bp);
		bp.setLayout(new GridLayout(1,2));
		bp.add(R);
		bp.add(W);
		
		add(centre,BorderLayout.CENTER);
		centre.setLayout(new GridLayout(1,1));
		centre.add(text);
		
		R.addActionListener(this);//adda this
		W.addActionListener(this);//adda this
		
		setLocation(400, 100);//setl
		setSize(300, 200);//sets
		setTitle("文件读写");//sett
		setDefaultCloseOperation(EXIT_ON_CLOSE);//setd
		setVisible(true);//setv
	}
	public void actionPerformed(ActionEvent e) {
		// TODO 自动添加
		Object s=e.getSource();
		try {
			if(s==R)
			{
				System.out.println("按下了读入键");
				String pathString=path.getText();//gett
				File file=new File(pathString);
				FileReader fileReader=new FileReader(file);//filer
				BufferedReader bufferedReader=new BufferedReader(fileReader);//bu
				String line="";
				String textString="";
				while((line=bufferedReader.readLine())!=null)
				{
					textString=textString+line+"\n";
				}
				text.setText(textString);//sett
			}
			if(s==W)
			{
				String pathString=path.getText();//gett
				File file=new File(pathString);
				FileWriter fileWriter=new FileWriter(file);//filew
				fileWriter.write(text.getText());
				fileWriter.close();
			}
		} catch (Exception e2) {
			// TODO: handle exception
		}
	}
}


public class main {

	public static void main(String[] args) {
		// TODO 
		new Win();
	}
}

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值