📄 test.java
字号:
}
class MenuFun //菜单中功能实现类
{
public static void cter(String s,JFrame jf,JLabel label)
{
if(s.equals("复制"))
copy(jf);
if(s.equals("粘帖"))
paste(jf,label);
if(s.equals("清空"))
clear(jf);
if(s.equals("字体"))
SetFont(jf,label);
if(s.equals("使用帮助"))
help();
if(s.equals("关于软件"))
about();
}
private static void about()//关于软件
{
String message=String.format("%s\n%s\n%s\n%s", "简用计算器 1.0 J2SE","版本: V1.0 测试版","作者: Tonysoft hangtao","欢迎访问 http://hangtao.5d6d.com");
JOptionPane.showMessageDialog(null, message, "关于软件 Tonysoft",JOptionPane.INFORMATION_MESSAGE);
}
private static void copy(JFrame jf)//复制屏幕上的数到系统剪切板
{
StringSelection temp=new StringSelection(""+Number.screen);
Clipboard cb=jf.getToolkit().getSystemClipboard();
cb.setContents(temp,null);
}
private static void paste(JFrame jf,JLabel label)//粘帖系统剪切板上的数到屏幕上
{
Clipboard cb=jf.getToolkit().getSystemClipboard();
Transferable contents=cb.getContents(jf);
DataFlavor flavor= DataFlavor.stringFlavor;
if( contents.isDataFlavorSupported(flavor))
try{ String s;
s=(String)contents.getTransferData(flavor);
Number.screen=Double.parseDouble(s);
label.setText(""+Number.screen);
}
catch(Exception ee){}
}
private static void clear(JFrame jf)
{
StringSelection temp=new StringSelection("");
Clipboard cb=jf.getToolkit().getSystemClipboard();
cb.setContents(temp,null);
}
private static void help()
{
String message=String.format("%s\n%s", "暂无详细帮助,如有使用疑问请联系作者!","感谢使用!");
JOptionPane.showMessageDialog(null, message, "使用帮助 Tonysoft",JOptionPane.INFORMATION_MESSAGE);
}
private static void SetFont(JFrame jf,JLabel label)
{
Font font=null;
font =NwFontChooserS.showDialog(jf, "", font);
label.setFont(font);
}
}
class Menubar//顶层菜单实现类
{
public static void setbar( JFrame f )
{
JMenuBar bar=new JMenuBar();
JMenu jm1=new JMenu("编辑");
JMenu jm2=new JMenu("设置");
JMenu jm3=new JMenu("帮助");
JMenuItem ji11=new JMenuItem("复制");
ji11.addActionListener(new Test.MenuListener());
JMenuItem ji12=new JMenuItem("粘帖");
ji12.addActionListener(new Test.MenuListener());
JMenuItem ji13=new JMenuItem("清空");
ji13.addActionListener(new Test.MenuListener());
JMenuItem ji21=new JMenuItem("字体");
ji21.addActionListener(new Test.MenuListener());
JMenuItem ji22=new JMenuItem("格式");
ji22.addActionListener(new Test.MenuListener());
JMenuItem ji31=new JMenuItem("使用帮助");
ji31.addActionListener(new Test.MenuListener());
JMenuItem ji32=new JMenuItem("关于软件");
ji32.addActionListener(new Test.MenuListener());
//为每个选项注册匿名监听器
jm1.add(ji11);
jm1.add(ji12);
jm1.add(ji13);
jm2.add(ji21);
jm2.add(ji22);
jm3.add(ji31);
jm3.add(ji32);
bar.add(jm1);
bar.add(jm2);
bar.add(jm3);
f.add(bar,BorderLayout.NORTH);
}
}
class Keyborder//键盘实现类
{
public static void setkeybord(JPanel jp)
{
JButton b1=new JButton("←");
b1.addActionListener(new Test.InputListener());
JButton b2=new JButton("CE");
b2.addActionListener(new Test.InputListener());
JButton b3=new JButton("C");
b3.addActionListener(new Test.InputListener());
JButton b4=new JButton("MC");
b4.addActionListener(new Test.InputListener());
JButton b5=new JButton("MR");
b5.addActionListener(new Test.InputListener());
JButton b6=new JButton("MS");
b6.addActionListener(new Test.InputListener());
JButton b7=new JButton("M+");
b7.addActionListener(new Test.InputListener());
b1.setForeground(Color.blue);
b2.setForeground(Color.blue);
b3.setForeground(Color.blue);
b4.setForeground(Color.blue);
b5.setForeground(Color.blue);
b6.setForeground(Color.blue);
b7.setForeground(Color.blue);
//*******************************************************
JButton n1=new JButton("1");
n1.addActionListener(new Test.InputListener());
JButton n2=new JButton("2");
n2.addActionListener(new Test.InputListener());
JButton n3=new JButton("3");
n3.addActionListener(new Test.InputListener());
JButton n4=new JButton("4");
n4.addActionListener(new Test.InputListener());
JButton n5=new JButton("5");
n5.addActionListener(new Test.InputListener());
JButton n6=new JButton("6");
n6.addActionListener(new Test.InputListener());
JButton n7=new JButton("7");
n7.addActionListener(new Test.InputListener());
JButton n8=new JButton("8");
n8.addActionListener(new Test.InputListener());
JButton n9=new JButton("9");
n9.addActionListener(new Test.InputListener());
JButton n0=new JButton("0");
n0.addActionListener(new Test.InputListener());
n0.setForeground(Color.red);
n1.setForeground(Color.red);
n2.setForeground(Color.red);
n3.setForeground(Color.red);
n4.setForeground(Color.red);
n5.setForeground(Color.red);
n6.setForeground(Color.red);
n7.setForeground(Color.red);
n8.setForeground(Color.red);
n9.setForeground(Color.red);
//***********************************************
JButton o1=new JButton("/");
o1.addActionListener(new Test.InputListener());
JButton o2=new JButton("×");
o2.setActionCommand("*");
o2.addActionListener(new Test.InputListener());
JButton o3=new JButton("+");
o3.addActionListener(new Test.InputListener());
JButton o4=new JButton("—");
o4.setActionCommand("-");
o4.addActionListener(new Test.InputListener());
JButton o5=new JButton("√");
o5.addActionListener(new Test.InputListener());
JButton o6=new JButton("%");
o6.addActionListener(new Test.InputListener());
JButton o7=new JButton(".");
o7.addActionListener(new Test.InputListener());
JButton o8=new JButton("=");
o8.addActionListener(new Test.InputListener());
JButton o9=new JButton("1/x");
o9.addActionListener(new Test.InputListener());
//////////////////////////////////
//************************************************
jp.setLayout(new GridLayout(5,6));
jp.add(b1);
jp.add(b4);
jp.add(b5);
jp.add(b6);
jp.add(b7);
jp.add(n0);
jp.add(n1);
jp.add(n2);
jp.add(n3);
jp.add(n4);
jp.add(n5);
jp.add(n6);
jp.add(n7);
jp.add(n8);
jp.add(n9);
jp.add(o1);
jp.add(o2);
jp.add(o3);
jp.add(o4);
jp.add(o5);
jp.add(o6);
jp.add(o7);
jp.add(o8);
jp.add(o9);
jp.add(b2);
jp.add(b3);
}
}
public class Test
{ final static JFrame jf=new JFrame("计算器1.0 Java");
//最顶级窗口
final static JPanel jp=new JPanel();//2级窗口
final static JPanel jp2=new JPanel();//第3级窗口
static JLabel label=new JLabel();
public static void main(String agrs[])
{
Number.lp="";
label.setFont( new Font( "黑体", Font.BOLD,25) );
label.setBackground(Color.darkGray);
jf.setLocation(200, 200);
jf.setVisible(true);
jf.setResizable(false);
jf.setSize(350,250);
jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Menubar.setbar(jf);
jp.setLayout(new BorderLayout());
label.setText("0.0");
label.setHorizontalAlignment(SwingConstants.RIGHT);
jp.add(label,BorderLayout.NORTH);
Keyborder.setkeybord(jp2);
jp.add(jp2,BorderLayout.CENTER);
jf.getContentPane().add(jp,BorderLayout.CENTER);
jp.add(label,BorderLayout.NORTH);
}
public static class InputListener implements ActionListener
{
//实现数字等按键的事件处理
public void actionPerformed(ActionEvent e)
{
switch (Input.numbertype(e.getActionCommand()))
{
case 1:
Input.cter(e.getActionCommand(),label);
break;
case 2:
Counter.cter(e.getActionCommand(),label);
break;
case 0:
Memory.cter(e.getActionCommand(),label);
break;
}
}
}
public static class MenuListener implements ActionListener
{
//实现菜单按钮的事件处理
public void actionPerformed(ActionEvent e)
{
MenuFun.cter(e.getActionCommand(),jf,label);
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -