JAVA 图形界面swing 创建按钮、文本域

package org.rui.swin;

import java.awt.FlowLayout;

import javax.swing.JButton;
import javax.swing.JFrame;

import org.rui.utils.SwingConsole;

public class Button1  extends JFrame{
	
	private JButton button1=new JButton("button1"),
			button2=new JButton("button2");
	

	public Button1(){
		setLayout(new FlowLayout());
		add(button1);
		add(button2);
	}
	
	public static void main(String[] args) {
		SwingConsole.run(new Button1(),200,300);
	}
	

}


package org.rui.swin;

import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JTextField;
import org.rui.utils.SwingConsole;
/**
 * 捕获事件
 * @author PC
 *
 */
public class Button2 extends JFrame {

	
	private JButton but1 = new JButton("button1"),
			but2 = new JButton("button2");
	JTextField textf = new JTextField(10);

	//监听处理
	class ButtonListener implements ActionListener {
		@Override
		public void actionPerformed(ActionEvent arg0) {
			//System.out.println(((JButton) arg0.getSource()).getText());
			String name = ((JButton) arg0.getSource()).getText();
			textf.setText(name);
		}
	}

	// /
	private ButtonListener bl = new ButtonListener();
	public Button2() {
		but1.addActionListener(bl);
		but2.addActionListener(bl);
		setLayout(new FlowLayout());
		add(but1);
		add(but2);
		add(textf);
	}

	public static void main(String[] args) {
		SwingConsole.run(new Button2(), 300, 200);
	}

}


package org.rui.swin;

import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;

import org.rui.utils.SwingConsole;

public class TextArea extends JFrame {
	private JButton but1 = new JButton("add data"), but2 = new JButton(
			"clear data");
	private JTextArea t = new JTextArea(20, 40);
	private Map<String, String> m = new HashMap<String, String>();

	public TextArea() {
		m.put("CH", "中国");
		m.put("EN", "美国");
		m.put("CC", "天国");
		
		but1.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				for(Entry en:m.entrySet())
				{
					t.append(en.getKey()+" : "+en.getValue()+"\n");
				}
			}

		});
		but2.addActionListener(new ActionListener(){
			@Override
			public void actionPerformed(ActionEvent e) {
				t.setText(" ");
			}});
		setLayout(new FlowLayout());
		add(new JScrollPane(t));
		add(but1);
		add(but2);
	}
	
	public static void main(String[] args) {
		SwingConsole.run(new TextArea(), 500, 600);
	}

}


  • 1
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是一个基本的Java GUI程序,实现了您提到的三个功能: ```java import java.awt.*; import java.awt.event.*; import java.io.*; import javax.swing.*; public class TextProcessorGUI extends JFrame implements ActionListener { private JTextArea leftTextArea; private JTextArea rightTextArea; public TextProcessorGUI() { // 设置窗口标题 setTitle("文本处理程序"); // 创建文本域 leftTextArea = new JTextArea(); rightTextArea = new JTextArea(); // 创建按钮 JButton readButton = new JButton("读取"); readButton.addActionListener(this); JButton sortButton = new JButton("排序"); sortButton.addActionListener(this); JButton saveButton = new JButton("保存"); saveButton.addActionListener(this); // 创建面板 JPanel panel = new JPanel(new GridLayout(1, 3)); panel.add(readButton); panel.add(sortButton); panel.add(saveButton); // 将控件添加到窗口中 Container contentPane = getContentPane(); contentPane.add(leftTextArea, BorderLayout.WEST); contentPane.add(rightTextArea, BorderLayout.EAST); contentPane.add(panel, BorderLayout.SOUTH); // 设置窗口大小 setSize(500, 400); // 显示窗口 setVisible(true); } public void actionPerformed(ActionEvent e) { if (e.getActionCommand().equals("读取")) { // 读取指定txt文档中的内容 try { BufferedReader reader = new BufferedReader(new FileReader("input.txt")); String line; leftTextArea.setText(""); while ((line = reader.readLine()) != null) { leftTextArea.append(line + "\n"); } reader.close(); } catch (IOException ex) { ex.printStackTrace(); } } else if (e.getActionCommand().equals("排序")) { // 将以“DBa”和“RRi”开头的字符串提取出来,放到右边的文本域中 String[] lines = leftTextArea.getText().split("\n"); rightTextArea.setText(""); for (String line : lines) { if (line.startsWith("DBa") || line.startsWith("RRi")) { rightTextArea.append(line + "\n"); } } } else if (e.getActionCommand().equals("保存")) { // 将处理过后右边的文本保存到一个新的txt文档中 try { BufferedWriter writer = new BufferedWriter(new FileWriter("output.txt")); writer.write(rightTextArea.getText()); writer.close(); } catch (IOException ex) { ex.printStackTrace(); } } } public static void main(String[] args) { new TextProcessorGUI(); } } ``` 该程序使用了JavaSwing库,创建了一个包含两个文本域和三个按钮的窗口。单击“读取”按钮将读取指定的txt文档中的内容,并在左侧文本域中显示。单击“排序”按钮将提取以“DBa”和“RRi”开头的字符串,并在右侧文本域中显示。单击“保存”按钮将保存右侧文本域中的内容到一个新的txt文档中。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值