实验内容:
第一题:
1.定义一个窗口,有菜单,文件和编辑,有两个文本区。
2.文件中有菜单项:打开和保存。对打开菜单项设置快捷键“O”,对保存菜单项设置快捷键“Ctrl+s”,打开和保存菜单项之间有分割线。
3.编辑菜单中有复制,剪切,粘贴三个菜单项,要求点击菜单项时在文本区可操作。
第二题:
创建一个窗口,有三个组件如下要求:
1.有一个列表,其选项为你所在电脑的两个java源文件的名字。当选择一个选项后,会在文本区显示所选择的Java源文件的全部内容
2.有一个文本框和一个密码框:当在密码框输入密码时,密码框显示回显字符;回车后,在文本框显示密码。当在文本框输入信息后,将窗口名字改为文本框输入的信息。
3.窗口布局,自选
第三题:
定义一个窗口,有菜单:文件、编辑和计算。文件栏中可以执行对文件打开、新建和保存。编辑栏可进行复制、粘贴和剪切操作。计算栏可计算圆的面积。
第一题
代码实现:
package com.company;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JSplitPane;
import javax.swing.JTextArea;
import javax.swing.KeyStroke;
import java.awt.*;
import java.awt.event.*;
public class Main {
public static void main(String[] args) {
EditWindow win=new EditWindow(); }
}
class EditWindow extends JFrame implements ActionListener{
JMenuBar menubar;
JMenu menu1,menu2;
JMenuItem item1,item2,item3,item4,item5;
JSplitPane splitpane;
JTextArea text1,text2;
EditWindow(){
menubar=new JMenuBar();
menu1=new JMenu("菜单");
menu2=new JMenu("编辑");
item1=new JMenuItem("打开");
item2=new JMenuItem("保存");
item3=new JMenuItem("复制");
item4=new JMenuItem("剪切");
item5=new JMenuItem("粘贴");
item1.setAccelerator(KeyStroke.getKeyStroke('o')); item2.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_S,InputEvent.CTRL_MASK));
menu1.add(item1);
menu1.addSeparator();
menu1.add(item2);
menu2.add(item3);
menu2.addSeparator();
menu2.add(item4);
menu2.addSeparator();
menu2.add(item5);
menubar.add(menu1);
menubar.add(menu2);
setJMenuBar(menubar);
text1=new JTextArea();
text2=new JTextArea();
splitpane=new JSplitPane(JSplitPane.HORIZONTAL_SPLIT,text1,text2);
splitpane.setDividerLocation(120);
add(splitpane,BorderLayout.CENTER);
setBounds(300,300,400,300);
setVisible(true);
validate();
setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
item3.addActionListener(this);
item4.addActionListener(this);
item5.addActionListener(this);
}
public void actionPerformed(ActionEvent e) {
if(e.getSource()==item3) {
text1.copy();}
else if(e.getSource()==item4) {
text1.cut();
}
else if(e.getSource()==item5)
text2.paste(); }
}
程序截图:
第二题:
代码实现:
package com.company;
import javax.swing.*;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.io.*;
public class Main {
public static void main(String[] args) {
SwingUtilities.invokeLater(() -> {
MainFrame mainFrame = MainFrame.getInstance();
mainFrame.setBounds(10, 10, 450, 400);
mainFrame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
mainFrame.setVisible(true);
});
}
}
class WenjianLiebiao extends JComboBox<String> {
private static class MyItemListener implements ItemListener {
public void itemStateChanged(ItemEvent e) {
File dir = new File(System.getProperty("user.dir"));
File javaFile = null;
if (e.getItem().toString().equalsIgnoreCase("oop.java")) {
javaFile = new File(dir, "oop.java");
} else if (e.getItem().toString().equalsIgnoreCase("002.java")) {
javaFile = new File(dir, "002.java");
} else {
return;
}
if (!javaFile.exists()) return;
try (
FileReader fileReader = new FileReader(javaFile);
BufferedReader bufferedReader = new BufferedReader(fileReader);
) {
char[] buffer = new char[1024];
int bytesRead = 0;
WenbenKuang wenbenKuang = WenbenKuang.getInstance();
wenbenKuang.setText("");
do {
bytesRead = bufferedReader.read(buffer);
if (bytesRead > 0)
wenbenKuang.append(new String(buffer));
} while (bytesRead > 0);
} catch (FileNotFoundException ex) {
ex.printStackTrace();
} catch (IOException ex) {
ex.printStackTrace();
}
}
}
public WenjianLiebiao() {
addItem("oop.java");
addItem("002.java");
addItemListener(new MyItemListener());
}
}
class Input {
static class InputText extends JTextField {
private InputText() {
addActionListener(e -> {
MainFrame.getInstance().setTitle(getText());
});
}
private static final InputText INPUT_TEXT = new InputText();
public static InputText getInstance() {
return INPUT_TEXT;
}
}
static class InputPassword extends JPasswordField {
private InputPassword() {
addActionListener(e -> {
InputText.getInstance().setText(new String(getPassword()));
});
}
private static final InputPassword INPUT_PASSWORD = new InputPassword();
public static InputPassword getInstance() {
return INPUT_PASSWORD;
}
}
}
class WenbenKuang extends JTextArea {
private static final WenbenKuang JAVA_FILE_TEXT_AREA = new WenbenKuang();
private WenbenKuang() {
}
public static WenbenKuang getInstance() {
return JAVA_FILE_TEXT_AREA;
}
}
class MainFrame extends JFrame {
private static final MainFrame mainFrame = new MainFrame();
private MainFrame() {
setLayout(null);
Input.InputText inputText = Input.InputText.getInstance();
inputText.setBounds(10, 10, 100, 30);
Input.InputPassword inputPassword = Input.InputPassword.getInstance();
inputPassword.setBounds(120, 10, 100, 30);
WenjianLiebiao comboList = new WenjianLiebiao();
comboList.setBounds(230, 10, 100, 30);
WenbenKuang wenbenKuang = WenbenKuang.getInstance();
JScrollPane jScrollPane = new JScrollPane(wenbenKuang);
jScrollPane.setBounds(10, 45, 330, 300);
add(inputText);
add(inputPassword);
add(comboList);
add(jScrollPane);
}
public static MainFrame getInstance() {
return mainFrame;
}
}
程序截图:
第三题:
代码实现:
package com.company;
import javax.swing.*;
import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.io.*;
public class Main {
public static void main(String[] args) {
ZhuChuangKou zhuChuangKou = ZhuChuangKou.getInstance();
zhuChuangKou.setVisible(true);
}
}
class CaiDanLan extends JMenuBar {
static class FileOpenAction extends AbstractAction {
public void actionPerformed(ActionEvent e) {
WenBenKuang.getInstance().setText("");
JFileChooser fileChooser = new JFileChooser();
fileChooser.showOpenDialog(ZhuChuangKou.getInstance());
File file = fileChooser.getSelectedFile();
if (file == null) {
WenBenKuang.getInstance().setFileName(null);
return;}
FileSaveAction.duWenjian(file);}
}
static class CalcAction extends AbstractAction {
public void actionPerformed(ActionEvent e) {
JDialog dialog = new JDialog(ZhuChuangKou.getInstance(), true);
dialog.setSize(200, 200);
int x = ZhuChuangKou.getInstance().getX() + (ZhuChuangKou.getInstance().getWidth() - dialog.getWidth()) / 3;
int y = ZhuChuangKou.getInstance().getY() + (ZhuChuangKou.getInstance().getHeight() - dialog.getHeight()) / 3;
dialog.setLayout(null);
JTextField textField = new JTextField();
textField.setBounds(0, 0, 200, 25);
JLabel label = new JLabel();
label.setBounds(0, 30, 200, 25);
label.setText("面积: ");
textField.getDocument().addDocumentListener(new DocumentListener() {
public void insertUpdate(DocumentEvent e) {
parse();}
public void removeUpdate(DocumentEvent e) {
parse();}
public void changedUpdate(DocumentEvent e) {
parse();}
public void parse() {
try {
int r = Integer.parseInt(textField.getText());
label.setText("面积: " + (r * r * Math.PI));
} catch (Exception ex) {
label.setText("面积: ");
}
}
});
dialog.add(textField);
dialog.add(label);
dialog.setLocation(x, y);
dialog.setVisible(true);
}
}
public CaiDanLan() {
add(menu1());
add(menu2());
add(menu3());
}
private JMenu menu1() {
JMenu menu = new JMenu("文件");
menu.addSeparator();
JMenuItem open = new JMenuItem("打开"), create = new JMenuItem("新建"), save = new JMenuItem("保存");
open.addActionListener(new FileOpenAction());
create.addActionListener(e -> {
WenBenKuang.getInstance().setText("");
ZhuChuangKou.getInstance().setTitle("文本编辑器");
});
open.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_O, KeyEvent.CTRL_DOWN_MASK));
create.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_N, KeyEvent.CTRL_DOWN_MASK));
save.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_S, KeyEvent.CTRL_DOWN_MASK));
save.addActionListener(new FileSaveAction());
menu.add(open);
menu.add(create);
menu.add(save);
return menu;}
private JMenu menu2() {
JMenu menu = new JMenu("编辑");
JMenuItem copy = new JMenuItem("复制"), cut = new JMenuItem("剪切"), paste = new JMenuItem("粘贴");
menu.add(copy);
menu.addSeparator();
menu.add(cut);
menu.addSeparator();
menu.add(paste);
copy.addActionListener(e -> {
WenBenKuang.getInstance().copy();
});
paste.addActionListener(e -> {
WenBenKuang.getInstance().paste();
});
cut.addActionListener(e -> {
WenBenKuang.getInstance().cut();
});
return menu;
}
private JMenu menu3() {
JMenu menu = new JMenu("计算");
menu.addSeparator();
JMenuItem circle = new JMenuItem("求圆面积");
circle.addActionListener(new CalcAction());
menu.add(circle);
return menu;
}
private static class FileSaveAction implements ActionListener {
public void actionPerformed(ActionEvent e) {
String fileName = WenBenKuang.getInstance().getFileName();
if (fileName == null) {
JFileChooser fileChooser = new JFileChooser();
fileChooser.showSaveDialog(ZhuChuangKou.getInstance());
File file = fileChooser.getSelectedFile();
if (file == null) return;
if (!file.exists()) {
try {
boolean newFile = file.createNewFile();
if (!newFile) {
return;
}
} catch (IOException ex) {
ex.printStackTrace();}
}
WenBenKuang.getInstance().setFileName(file.getAbsolutePath());
duWenjian(file);
return;
}
try {
FileWriter out = new FileWriter(fileName, false);
BufferedWriter bufferedWriter = new BufferedWriter(out); WenBenKuang.getInstance().setText(WenBenKuang.getInstance().getText().replace("\0", ""));
bufferedWriter.write(WenBenKuang.getInstance().getText());
bufferedWriter.flush();
bufferedWriter.close();
out.close();
} catch (IOException ex) {
ex.printStackTrace();
}
}
private static void duWenjian(File file) {
try {
BufferedReader bufferedReader = new BufferedReader(new FileReader(file));
char[] buf = new char[1024];
while (true) {
int bytesRead = bufferedReader.read(buf);
if (bytesRead <= 0) break;
WenBenKuang textArea = WenBenKuang.getInstance();
textArea.setText(textArea.getText() + (new String(buf)));
}
WenBenKuang.getInstance().setFileName(file.getAbsolutePath());
} catch (IOException ex) {
ex.printStackTrace();}
}
}
}
class WenBenKuang extends JTextArea {
private String fileName = null;
private static final WenBenKuang WEN_BEN_KUANG = new WenBenKuang();
public static WenBenKuang getInstance() {
return WEN_BEN_KUANG;
}
public String getFileName() {
return fileName;
}
public void setFileName(String fileName) {
this.fileName = fileName;
}
}
class ZhuChuangKou extends JFrame {
private static final ZhuChuangKou ZHU_CHUANG_KOU = new ZhuChuangKou();
private ZhuChuangKou() {
CaiDanLan menuBar = new CaiDanLan();
setJMenuBar(menuBar);
JScrollPane scrollPane = new JScrollPane(WenBenKuang.getInstance());
add(scrollPane);
setBounds(100, 100, 400, 400);
setTitle("文本编辑器");
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
public static ZhuChuangKou getInstance() {
return ZHU_CHUANG_KOU;
}
}