java action例子_简单的java UI例子(JFrame,JButton,ActionListener等)

package com.nwx.txtEditor.ui;

import java.awt.FlowLayout;

import javax.swing.JButton;

import javax.swing.JFrame;

import javax.swing.JLabel;

import javax.swing.JPanel;

import javax.swing.JTextArea;

import javax.swing.JTextField;

public class MainWindow extends JFrame {

private JButton autoSaveBtn, saveBtn, cancelBtn, exitBtn, autoNextBtn;

private JTextField saveDirTextField, fileNameTextField;

private JTextArea fileContentArea;

public JTextField getSaveDirTextField() {

return saveDirTextField;

}

public void setSaveDirTextField(JTextField saveDirTextField) {

this.saveDirTextField = saveDirTextField;

}

public JTextField getFileNameTextField() {

return fileNameTextField;

}

public void setFileNameTextField(JTextField fileNameTextField) {

this.fileNameTextField = fileNameTextField;

}

public JTextArea getFileContentArea() {

return fileContentArea;

}

public void setFileContentArea(JTextArea fileContentArea) {

this.fileContentArea = fileContentArea;

}

public MainWindow() {

FlowLayout flowLayoutLeft = new FlowLayout(FlowLayout.LEFT, 10, 10);

//FlowLayout flowLayoutCenter = new FlowLayout(FlowLayout.LEFT, 10, 10);

//GridLayout gridLayout = new GridLayout(16, 1);

this.setLayout(flowLayoutLeft);

// button panel

JPanel buttonPanel = new JPanel(flowLayoutLeft);

autoSaveBtn = new JButton("autoSave");

saveBtn = new JButton("save");

cancelBtn = new JButton("cancel");

exitBtn = new JButton("exit");

autoNextBtn = new JButton("autoNext");

autoSaveBtn.addActionListener(new TxtEditorActionListener(this));

saveBtn.addActionListener(new TxtEditorActionListener(this));

cancelBtn.addActionListener(new TxtEditorActionListener(this));

exitBtn.addActionListener(new TxtEditorActionListener(this));

autoNextBtn.addActionListener(new TxtEditorActionListener(this));

buttonPanel.add(autoSaveBtn);

buttonPanel.add(saveBtn);

buttonPanel.add(cancelBtn);

buttonPanel.add(exitBtn);

buttonPanel.add(autoNextBtn);

this.add(buttonPanel);

// save dir panel

JPanel saveDirPanel = new JPanel(flowLayoutLeft);

JLabel saveDirLabel = new JLabel("Save Dir: ");

saveDirTextField = new JTextField("C:/study/textEditor/textFiles/", 60);

saveDirPanel.add(saveDirLabel);

saveDirPanel.add(saveDirTextField);

this.add(saveDirPanel);

// file name panel

JPanel fileNamePanel = new JPanel(flowLayoutLeft);

JLabel fileNameLabel = new JLabel("FileName: ");

fileNameTextField = new JTextField(60);

fileNamePanel.add(fileNameLabel);

fileNamePanel.add(fileNameTextField);

this.add(fileNamePanel);

// file content text area

fileContentArea = new JTextArea(30, 65);

this.add(fileContentArea);

// EXIT_ON_CLOSE,关闭程序。(所有窗口和进程都会关闭)

// DISPOSE_ON_CLOSE,只关闭本窗口。

// HIDE_ON_CLOSE,只隐藏本窗口,不关闭。

// DO_NOTHING_ON_CLOSE,不做任何事,点击关闭无效。

this.setDefaultCloseOperation(EXIT_ON_CLOSE);

this.setSize(800, 700);

this.setResizable(false);

this.setVisible(true);

}

}

package com.nwx.txtEditor.ui;

import java.awt.event.ActionEvent;

import java.awt.event.ActionListener;

import java.io.IOException;

import java.util.Date;

import javax.swing.JOptionPane;

import org.apache.commons.lang3.StringUtils;

import com.nwx.study.io.ReadWriteWebFile;

public class TxtEditorActionListener implements ActionListener {

private MainWindow window;

public TxtEditorActionListener(MainWindow window) {

this.window = window;

}

@Override

public void actionPerformed(ActionEvent e) {

String cmd = e.getActionCommand();

String fileNameStr = window.getFileNameTextField().getText().trim();

String saveDirStr = window.getSaveDirTextField().getText().trim();

String fileContentStr = window.getFileContentArea().getText().trim();

if ("autoSave".equals(cmd)) {

if (StringUtils.isEmpty(fileNameStr) && StringUtils.isNotEmpty(fileContentStr)) {

try {

if (fileContentStr.indexOf("\n") > 0) {

fileNameStr = fileContentStr.substring(0, fileContentStr.indexOf("\n")).trim() + ".txt";

if (fileNameStr.length() > 30) {

fileNameStr = "";

}

}

if (StringUtils.isEmpty(fileNameStr)) {

fileNameStr = (new Date()).getTime() + ".txt";

}

window.getFileNameTextField().setText(fileNameStr);

ReadWriteWebFile.writeFile(fileContentStr, saveDirStr + "/" + fileNameStr);

JOptionPane.showMessageDialog(window, "SAVED SUCCESS", "INFO", JOptionPane.INFORMATION_MESSAGE);

} catch (IOException e1) {

JOptionPane.showMessageDialog(window, "SAVED FAILED", "WARNING", JOptionPane.WARNING_MESSAGE);

// e1.printStackTrace();

}

} else if (StringUtils.isNotEmpty(fileNameStr) && StringUtils.isNotEmpty(fileContentStr)) {

try {

ReadWriteWebFile.writeFile(fileContentStr, saveDirStr + "/" + fileNameStr);

JOptionPane.showMessageDialog(window, "SAVED SUCCESS", "INFO", JOptionPane.INFORMATION_MESSAGE);

} catch (IOException e1) {

JOptionPane.showMessageDialog(window, "SAVED FAILED", "WARNING", JOptionPane.WARNING_MESSAGE);

// e1.printStackTrace();

}

} else if (StringUtils.isEmpty(fileContentStr)) {

JOptionPane.showMessageDialog(window, "The fileName or fileContent is empty...", "WARNING",

JOptionPane.WARNING_MESSAGE);

}

} else if ("save".equals(cmd)) {

if (StringUtils.isEmpty(fileNameStr) || StringUtils.isEmpty(fileContentStr)) {

JOptionPane.showMessageDialog(window, "The fileName or fileContent is empty...", "WARNING",

JOptionPane.WARNING_MESSAGE);

} else {

try {

ReadWriteWebFile.writeFile(fileContentStr, saveDirStr + "/" + fileNameStr);

JOptionPane.showMessageDialog(window, "SAVED SUCCESS", "INFO", JOptionPane.INFORMATION_MESSAGE);

} catch (IOException e1) {

JOptionPane.showMessageDialog(window, "SAVED FAILED", "WARNING", JOptionPane.WARNING_MESSAGE);

// e1.printStackTrace();

}

}

} else if("cancel".equals(cmd)){

window.getFileNameTextField().setText("");

window.getFileContentArea().setText("");

} else if ("exit".equals(cmd)) {

int sure = JOptionPane.showConfirmDialog(window, "Are you sure to exit?", "WARNING",

JOptionPane.YES_NO_OPTION);

// System.out.println(sure);

if (0 == sure) {

System.exit(0);

}

} else if ("autoGetNext".equals(cmd)) {

}

}

}

package com.nwx.txtEditor.ui;

public class launcher {

public static void main(String[] args) {

new MainWindow();

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值