实现记事本————使用GUI
使用GUI组件实现记事本,要求有弹出窗口,以及模态和非模态对话框
代码实现
package GuIExample;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class Win {
//实现模拟记事本
public static void main(String[] args) {
//创建窗口
JFrame jFrame = new JFrame("记事本");
//设置窗口
jFrame.setSize(800, 600); //设置大小
jFrame.setLocationRelativeTo(null); //居中显示
jFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //关闭
//实现文本区
JTextArea jTextArea = new JTextArea();
jTextArea.setLineWrap(true); //设置自动换行
JScrollPane scrollPane = new JScrollPane(jTextArea);
jFrame.add(scrollPane); //加入窗口
//设置菜单
JMenuBar bar = new JMenuBar(); //创建菜单栏
JMenu menuFile = new JMenu("文件(F)"); //创建指定文字的菜单
JMenuItem item1 = new JMenuItem("新建(N)"); //创建菜单内的内容
item1.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
newFile();
}
});
JMenuItem item2 = new JMenuItem("打开(O)");
//设置非模态对话框
item2.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
if (e.getSource() == item2) {
openFile();
}
}
});
JMenuItem item3 = new JMenuItem("保存(S)");
//设置模态窗口 保存文件
item3.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
if (e.getSource() == item3) {
saveFile();
}
}
});
JMenuItem item4 = new JMenuItem("另存为");
item4.addActionListener(new AbstractAction() {
@Override
public void actionPerformed(ActionEvent e) {
saveFile();
}
});
JMenuItem item5 = new JMenuItem("页面设置(U)");
JMenuItem item6 = new JMenuItem("打印(P)");
JMenuItem item7 = new JMenuItem("退出(X)");
//菜单项加入菜单
menuFile.add(item1);
menuFile.add(item2);
menuFile.add(item3);
menuFile.add(item4);
menuFile.addSeparator(); //添加菜单分割条
menuFile.add(item5);
menuFile.add(item6);
menuFile.addSeparator();
menuFile.add(item7);
//创建编辑菜单栏
JMenu editMenu = new JMenu("编辑");
JMenu formatMenu = new JMenu("格式");
JMenu viewMenu = new JMenu("查看");
JMenu helpMenu = new JMenu("帮助");
//加入菜单栏
bar.add(menuFile);
bar.add(editMenu);
bar.add(formatMenu);
bar.add(viewMenu);
bar.add(helpMenu);
jFrame.setJMenuBar(bar);
jFrame.setVisible(true);
}
/**
* 新建时应该是一个模态窗口
*/
private static void newFile() {
JDialog jDialog = new JDialog();
jDialog.setSize(260, 150);
jDialog.setLocationRelativeTo(null);
jDialog.setModal(true); //设置为模态窗口
jDialog.setLayout(new GridLayout(2, 1));
JPanel panel1 = new JPanel();
JPanel panel2 = new JPanel();
JLabel jLabel = new JLabel("是否将更改保存到 无标题?");
JButton button1 = new JButton("保存(S)");
JButton button2 = new JButton("不保存(N)");
JButton button3 = new JButton("取消");
panel1.add(jLabel);
panel2.add(button1);
panel2.add(button2);
panel2.add(button3);
jDialog.add(panel1);
jDialog.add(panel2);
jDialog.setVisible(true);
}
/**
* 设置打开文件非模态窗口
*/
private static void openFile() {
JFrame jFrame = new JFrame();
FileDialog jDialog = new FileDialog(jFrame);
jDialog.setSize(260, 150);
jDialog.setLocationRelativeTo(null); //居中显示
jDialog.setModal(true); //设置为模态窗口
// JLabel jLabel = new JLabel("这是一个模态窗口");
//jDialog.add(jLabel);
jDialog.setVisible(true);
}
/**
* 设置保存文件弹出来模态窗口
*/
private static void saveFile() {
JFrame jFrame = new JFrame();
FileDialog jDialog = new FileDialog(jFrame);
jDialog.setSize(260, 150);
jDialog.setLocationRelativeTo(null); //居中显示
//jDialog.setDefaultCloseOperation(JDialog.EXIT_ON_CLOSE); //设置关闭
//设置当前窗口为非模态
jDialog.setModal(true);
JLabel jLabel = new JLabel("这是一个模态对话框");
jDialog.add(jLabel);
jDialog.setVisible(true);
}
}
运行结果
主页面
菜单栏
菜单相关组件实现
打开文件
仅仅就是一个窗口,未实现真正意义上的打开文件
新建文件
未实现保存,不保存,取消的监听器功能,只是简单的UI界面
总结
这的意义在于实现GUI组件的运用和窗口的实现,JDialog窗口模态和非模态的区分。可以使用构造方法创建JDialog对象时设置为模态或非模态。也可以在创建对象后使用setModal()方法设置。传入参数为true时为模态窗口,false时为非模态窗口
模态窗口
用户需要处理完当前会话框时才可以使用其他窗口
/**
* 新建时应该是一个模态窗口
*/
private static void newFile() {
JDialog jDialog = new JDialog();
jDialog.setSize(260, 150);
jDialog.setLocationRelativeTo(null);
jDialog.setModal(true); //设置为模态窗口
jDialog.setLayout(new GridLayout(2, 1));
JPanel panel1 = new JPanel();
JPanel panel2 = new JPanel();
JLabel jLabel = new JLabel("是否将更改保存到 无标题?");
JButton button1 = new JButton("保存(S)");
JButton button2 = new JButton("不保存(N)");
JButton button3 = new JButton("取消");
panel1.add(jLabel);
panel2.add(button1);
panel2.add(button2);
panel2.add(button3);
jDialog.add(panel1);
jDialog.add(panel2);
jDialog.setVisible(true);
}
非模态窗口
允许处理当前对话框时同时使用其他对话框,比如JFrame窗口
package GuIExample;
import javax.swing.*;
public class JFrameExample {
//窗口 JFrame 包含边框,标题栏和窗口图标(最大化,最小化,关闭)
//用来容纳按钮,文本框和其他可视化组件
public static void main(String[] args) {
JFrame win = new JFrame("小明的窗口");
JButton bt = new JButton("我是按钮");
//将按钮添加到窗口的内容面板中
// win.add(bt);
//设置窗口的位置
win.setLocation(400, 300);
//调用setLocattionRelativeTo(null)方法可以在屏幕上居中显示框架
//win.setLocationRelativeTo(null);
//设置窗口的大小 如果不设置窗口大小,只会显示标题栏
win.setSize(300, 250);
//设置关闭行为 当框架关闭时结束程序。如果不使用这条语句,就会造成关闭框架后程序并不会结束。
win.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//设置窗口可见
win.setVisible(true);
}
}
欢迎大家指正,over
作者:爱吃水果的cc