📄 javatemplate.java
字号:
// design a template for learning java Fundamentals
// 1Java基础学习资料模板是一个按钮驱动的SWING应用。
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.util.*;
import javax.swing.border.*;
public class javaTemplate
{
public static void main(String[] args)
{
// 增加线程,显示程序启动画面
// boolean b = false;
startJFrame start = new startJFrame();
// 调用javaTamplate构造方法完成有关初始化工作。
javaTemplate t = new javaTemplate();
// 创建javaframe 对象,显示主窗口。JavaFrame extends Jframe.
javaFrame frame = new javaFrame(t.fileName,t.fileContent,t.buttonName);
frame.setTitle(t.title+t.version);
frame.setIconImage(Toolkit.getDefaultToolkit().getImage(t.icon));
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLocation(200,100);
// 采用windows 窗口形式
try
{
UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
SwingUtilities.updateComponentTreeUI(frame);
}catch (Exception exc) {}
frame.setVisible(true);
// 结束主程序线程。此时AWT线程继续运行。
}
// 构造方法
public javaTemplate()
{ // 版本设置,标题,图标
version = "1.0版";
title = "-----java 学习基础 资料模板库----";
icon = "about.gif";
// 打开文件,获得 面板、按钮名称参数
buttonName = getButtonName("buttonname.txt");
fileName = getContent("filename.txt",true);
fileContent = getContent("filename.txt",false);
}
// 从文件 获得面板按钮名称
private String[] getButtonName(String str)
{
File file = new File(str);
String[] content = new String [8];
try
{
BufferedReader in = new BufferedReader(new FileReader(file));
String s = in.readLine();
for (int i=0; s != null;i++ )
{
content[i]=s;
s = in.readLine();
}
in.close();
}catch (IOException e)
{
e.printStackTrace();
}
return content;
}
// 从文件获得显示内容信息
private String[][] getContent(String str,boolean b)
{
File file = new File(str);
String[][] content = new String[8][];
try
{
BufferedReader in = new BufferedReader(new FileReader(file));
String s = in.readLine();
for (int i=0; s != null;i++ )
{
StringTokenizer t = new StringTokenizer(s);
int k=t.countTokens();
if(b)
{
content[i/2] = new String[k];
for(int j=0;t.hasMoreTokens();j++)
content[i/2][j] = t.nextToken();
b = !b;
}
else
{
b = !b;
}
s = in.readLine();
}
in.close();
}catch (IOException e)
{
e.printStackTrace();
}
return content;
}
static String version;
static String title;
static String icon;
static String[] buttonName ;
static String[][] fileName ;
static String[][] fileContent;
}
// 继承 JFrame ,框架设定
class javaFrame extends JFrame implements ActionListener
{
static String[] button;
static JPanel[] centerPanel ;
static int[] nButton;
public javaFrame(String[][] name,String[][] content,String[] button)
{
// 设置javaFrame相关参数:主窗口的大小设置为600*400像素 等
this.button = button;
this.setSize(WIDTH,HEIGHT);
Container con = getContentPane();
// 创建一个菜单条(MenuBar) 使用 javaMenuBar extends JMenuBar
javaMenuBar menu = new javaMenuBar();
// 新创建的菜单条设置为主窗口的菜单条
this.setJMenuBar(menu);
// 创建面板(JPanel)
// 创建 northPanel ,位于面板顶部,装载 显示按钮
JPanel northPanel = new JPanel();
JPanel southPanel = new JPanel();
Border border = BorderFactory.createEtchedBorder();
southPanel.setBorder(border);
northPanel.setBorder(border);
con.add(northPanel,BorderLayout.NORTH);
con.add(southPanel,BorderLayout.SOUTH);
// 在面板顶部装载norhPanel ,在nothPanel上创建 按钮
// 分别对应顶部按钮按钮按下每个TabbedPane,出现的相应子内容
int numOfButton = button.length;
centerPanel = new JPanel[numOfButton];
JButton bt[]= new JButton[numOfButton];
nButton = new int[numOfButton];
for(int j=0;j<numOfButton;j++)
{
bt[j] = new JButton(button[j]);
bt[j].setFont(new Font("宋体",Font.PLAIN,15));
centerPanel[j] = new JPanel();
northPanel.add(bt[j]);
centerPanel[j].setBorder(border);
con.add(centerPanel[j],BorderLayout.CENTER);
nButton[j]=j;
centerPanel[j].setLayout(new BorderLayout());
bt[j].addActionListener(this);
// 创建 TabbedPane,位于面板中间,装载 文本区
JTabbedPane tab = new JTabbedPane(SwingConstants.LEFT);
int len = name[j].length;
if(len==0)
len=1;
javaTextArea[] show = new javaTextArea[len];
for(int i=0;i<len;i++)
{
// 创建 JTextArea ,装载学习内容
show[i] = new javaTextArea(8,20,content[j][i]);
// 增加监听器
menuAction action = new menuAction(show[i]);
show[i].addMouseListener(action);
show[i].setEditable(false);
show[i].setLineWrap(true);
show[i].setWrapStyleWord(true);
// 加滚动条,
JScrollPane pane = new JScrollPane();
pane.setViewportView(show[i]);
// 创建TabbedPane
tab.setFont(new Font("宋体",Font.PLAIN,17));
tab.addTab(name[j][i],pane);
centerPanel[j].add(tab);
}
}
// southPanel 时间显示提醒休息,创建按钮 ,"退出"
JLabel label = new JLabel("北京时间:");
label.setFont(new Font("宋体",Font.PLAIN,15));
southPanel.add(label);
JLabel showTime = new JLabel();
showTime.setFont(new Font("宋体",Font.PLAIN,15));
southPanel.add(showTime);
JButton exit = new JButton("退出");
exit.setFont(new Font("宋体",Font.PLAIN,15));
southPanel.add(exit);
exit.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
System.exit(1);
}
});
time t= new time(showTime);
Thread thread = new Thread(t);
thread.start();
}
// 为按钮增加监听, 当所监听的组件出现动作时,调用相应的监听器方法:
public void actionPerformed(ActionEvent e)
{
String str = e.getActionCommand();
for(int i =0;i<button.length;i++)
{
if(i==nButton[i])
{
centerPanel[i].setVisible(false);
this.getContentPane().remove(centerPanel[i]);
}
}
for(int i =0;i<button.length;i++)
{
if(str.equals(button[i]))
{
this.getContentPane().add(centerPanel[i],BorderLayout.CENTER);
centerPanel[i].setVisible(true);
nButton[i] = i;
}
}
}
private static int WIDTH = 760;
private static int HEIGHT = 480;
// 文本区 创建弹出菜单 监听鼠标事件和菜单按钮事件
class menuAction extends MouseAdapter implements ActionListener
{
javaTextArea show;
public menuAction(javaTextArea show)
{
this.show = show;
pop = new JPopupMenu();
JMenuItem copy = new JMenuItem("复制(Copy)");
copy.addActionListener(this);
pop.add(copy);
JMenuItem select = new JMenuItem("全选(SelectAll)");
select.addActionListener(this);
pop.add(select);
}
final JPopupMenu pop ;
public void mouseReleased(MouseEvent e)
{
if(e.isPopupTrigger())
pop.show(e.getComponent(),e.getX(),e.getY());
}
public void actionPerformed(ActionEvent e)
{
String s = e.getActionCommand();
if(s.equals("复制(Copy)"))
show.copy();
if(s.equals("全选(SelectAll)"))
show.selectAll();
}
}
}
//1. 创建"文件"菜单,子菜单"退出"
//2. 创建"帮助"菜单,自菜单"java学习帮助","关于java学习"
//3. 为菜单增加监听器
class javaMenuBar extends JMenuBar
{
public javaMenuBar ()
{
// 创建"文件"菜单,子菜单"退出"
JMenu file= new JMenu ("文件");
file.setFont(new Font("宋体",Font.PLAIN,15));
JMenuItem exit = new JMenuItem("退出");
file.add(exit);
this.add(file);
// 创建"帮助"菜单,自菜单"java学习帮助","关于java学习"
JMenu help= new JMenu ("帮助");
help.setFont(new Font("宋体",Font.PLAIN,15));
JMenuItem learn = new JMenuItem("java学习帮助");
help.add(learn);
this.add(help);
// 为菜单增加监听器
exit.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
System.exit(1);
}
});
}
}
//使用javaTextAera extends JTextArea内容动态装入
class javaTextArea extends JTextArea
{
public javaTextArea(int rows,int cols,String file)
{
super(rows,cols);
this.setTabSize(2);
showFile(file);
}
void showFile(String fileName)
{
File file = new File(".\\content\\"+fileName+".txt");
try
{
BufferedReader in = new BufferedReader(new FileReader(file));
String s = in.readLine();
while ( s != null )
{
append(s+"\n");
validate();
s = in.readLine();
}
in.close();
}catch (IOException e)
{
e.printStackTrace();
}
}
}
//启动画面
class startJFrame extends JFrame
{
JProgressBar progress;
String str;
public startJFrame()
{
str = "java 学习";
this.setSize(300,300);
this.setTitle(str);
this.setIconImage(Toolkit.getDefaultToolkit().getImage("about.gif"));
this.setLocation(200,200);
progress = new JProgressBar();
this.getContentPane().add(progress,BorderLayout.SOUTH);
Icon icon = new ImageIcon ("b.gif");
this.getContentPane().add((new JButton(icon)),BorderLayout.CENTER);
str = "java 学习";
this.setVisible(true);
startThread start = new startThread(progress,this);
start.start();
}
}
//进度条线程
class startThread extends Thread
{
JProgressBar progress;
startJFrame frame;
public startThread(JProgressBar p,startJFrame f)
{
progress = p;
frame = f;
progress.setMinimum(0);
progress.setMaximum(100);
}
public void run()
{
for(int i = 0;i<100;i++)
{
progress.setValue(i);
try
{
Thread.sleep(30);
}catch(Exception e){}
}
frame.setVisible(false);
}
}
// 显示时间线程
class time implements Runnable
{
JLabel showtime ;
public time(JLabel showtime)
{
this.showtime= showtime;
}
public void run()
{
while(true)
{
Date peiking =new Date();
showtime.setText(peiking.toString()+" ");
try
{
Thread.sleep(1000);
}
catch(InterruptedException e)
{
e.printStackTrace();
}
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -