十年网站开发经验 + 多家企业客户 + 靠谱的建站团队
量身定制 + 运营维护+专业推广+无忧售后,网站问题一站解决
该项目代码包括两个文件:MainFrame.java和SelectTextFile.java。
成都创新互联公司长期为1000+客户提供的网站建设服务,团队从业经验10年,关注不同地域、不同群体,并针对不同对象提供差异化的产品和服务;打造开放共赢平台,与合作伙伴共同营造健康的互联网生态环境。为珠晖企业提供专业的成都网站设计、成都网站建设,珠晖网站改版等技术服务。拥有10多年丰富建站经验和众多成功案例,为您定制开发。
(SelectTextFile.java的代码这里就不贴了,太长了。你百度HI我吧^_^)
补充说明:我这个里面没有“新建”,但其实新建也是一样调用那个SelectTextFile.java而已,而且,新建的时候更简单,只须按照路径调用File的.createNewFile()就可以了。具体我就不添加了。有疑问的话你可以百度HI我)
1.MainFrame.java代码:
package MyProject;
import java.awt.BorderLayout;
import javax.swing.JPanel;
import javax.swing.JFrame;
import java.awt.Dimension;
import javax.swing.JMenuBar;
import javax.swing.JMenu;
import javax.swing.JMenuItem;
import javax.swing.JLabel;
import java.awt.Rectangle;
import javax.swing.JTextArea;
import javax.swing.JScrollPane;
import java.awt.datatransfer.*;
import java.io.*;
public class MainFrame extends JFrame {
private static final long serialVersionUID = 1L;
private JPanel jContentPane = null;
private JMenuBar jJMenuBar = null;
private JMenu jMenu = null;
private JMenu jMenu1 = null;
private JMenu jMenu2 = null;
private JMenuItem jMenuItem = null;
private JMenuItem jMenuItem1 = null;
private JMenuItem jMenuItem2 = null;
private JMenuItem jMenuItem3 = null;
private JLabel jLabel = null;
private JScrollPane jScrollPane = null;
private JTextArea jTextArea = null;
private JMenuItem jMenuItem4 = null;
private JMenuItem jMenuItem5 = null;
private JMenuItem jMenuItem6 = null;
private JMenuItem jMenuItem7 = null;
private static MainFrame myMainFrame=null;
private static String textstr="";///用于记录文本文件的路径
private File myFile=null;
private FileReader myrder=null;
private FileWriter mywr=null;
/**
* This is the default constructor
*/
public MainFrame() {
super();
initialize();
}
/**
* This method initializes this
*
* @return void
*/
private void initialize() {
this.setSize(412, 350);
this.setJMenuBar(getJJMenuBar());
this.setContentPane(getJContentPane());
this.setTitle("JFrame");
this.addWindowListener(new java.awt.event.WindowAdapter() {
public void windowActivated(java.awt.event.WindowEvent e) {
if(!textstr.equals("")){
try{
myFile=new File(textstr);
if(!myFile.exists()){
myFile.createNewFile();
}
myrder=new FileReader(myFile);
char[] mychar=new char[(int)myFile.length()];
myrder.read(mychar);
String tmp=new String(mychar);
jTextArea.setText(tmp);
myrder.close();
}
catch(Exception ee){
ee.printStackTrace();
}
}
}
public void windowClosing(java.awt.event.WindowEvent e) {
System.exit(0);
}
});
this.setVisible(true);
myMainFrame=this;
}
/**
* This method initializes jContentPane
*
* @return javax.swing.JPanel
*/
private JPanel getJContentPane() {
if (jContentPane == null) {
jLabel = new JLabel();
jLabel.setBounds(new Rectangle(15, 18, 65, 18));
jLabel.setText("文件内容:");
jContentPane = new JPanel();
jContentPane.setLayout(null);
jContentPane.add(jLabel, null);
jContentPane.add(getJScrollPane(), null);
}
return jContentPane;
}
/**
* This method initializes jJMenuBar
*
* @return javax.swing.JMenuBar
*/
private JMenuBar getJJMenuBar() {
if (jJMenuBar == null) {
jJMenuBar = new JMenuBar();
jJMenuBar.add(getJMenu());
jJMenuBar.add(getJMenu1());
jJMenuBar.add(getJMenu2());
}
return jJMenuBar;
}
/**
* This method initializes jMenu
*
* @return javax.swing.JMenu
*/
private JMenu getJMenu() {
if (jMenu == null) {
jMenu = new JMenu();
jMenu.setText("文件");
jMenu.add(getJMenuItem());
jMenu.add(getJMenuItem1());
jMenu.add(getJMenuItem2());
jMenu.add(getJMenuItem3());
}
return jMenu;
}
/**
* This method initializes jMenu1
*
* @return javax.swing.JMenu
*/
private JMenu getJMenu1() {
if (jMenu1 == null) {
jMenu1 = new JMenu();
jMenu1.setText("编辑");
jMenu1.add(getJMenuItem4());
jMenu1.add(getJMenuItem5());
jMenu1.add(getJMenuItem6());
}
return jMenu1;
}
/**
* This method initializes jMenu2
*
* @return javax.swing.JMenu
*/
private JMenu getJMenu2() {
if (jMenu2 == null) {
jMenu2 = new JMenu();
jMenu2.setText("帮助");
jMenu2.add(getJMenuItem7());
}
return jMenu2;
}
/**
* This method initializes jMenuItem
*
* @return javax.swing.JMenuItem
*/
private JMenuItem getJMenuItem() {
if (jMenuItem == null) {
jMenuItem = new JMenuItem();
jMenuItem.setText("打开");
jMenuItem.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent e) {
MainFrame.this.myMainFrame.setEnabled(false);
SelectTextFile mysl=new SelectTextFile();
mysl.setVisible(true);
}
});
}
return jMenuItem;
}
/**
* This method initializes jMenuItem1
*
* @return javax.swing.JMenuItem
*/
private JMenuItem getJMenuItem1() {
if (jMenuItem1 == null) {
jMenuItem1 = new JMenuItem();
jMenuItem1.setText("关闭");
jMenuItem1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent e) {
try{
myFile=null;
}
catch(Exception ee){
ee.printStackTrace();
}
jTextArea.setText("");
}
});
}
return jMenuItem1;
}
/**
* This method initializes jMenuItem2
*
* @return javax.swing.JMenuItem
*/
private JMenuItem getJMenuItem2() {
if (jMenuItem2 == null) {
jMenuItem2 = new JMenuItem();
jMenuItem2.setText("保存");
jMenuItem2.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent e) {
try{
String tmp=jTextArea.getText();
char[] mychar=tmp.toCharArray();
myFile.delete();
myFile.createNewFile();
mywr=new FileWriter(myFile);
mywr.write(mychar);
mywr.close();
}
catch(Exception ee){
ee.printStackTrace();
}
}
});
}
return jMenuItem2;
}
/**
* This method initializes jMenuItem3
*
* @return javax.swing.JMenuItem
*/
private JMenuItem getJMenuItem3() {
if (jMenuItem3 == null) {
jMenuItem3 = new JMenuItem();
jMenuItem3.setText("退出");
jMenuItem3.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent e) {
System.exit(0);
}
});
}
return jMenuItem3;
}
/**
* This method initializes jScrollPane
*
* @return javax.swing.JScrollPane
*/
private JScrollPane getJScrollPane() {
if (jScrollPane == null) {
jScrollPane = new JScrollPane();
jScrollPane.setBounds(new Rectangle(15, 46, 371, 225));
jScrollPane.setViewportView(getJTextArea());
}
return jScrollPane;
}
/**
* This method initializes jTextArea
*
* @return javax.swing.JTextArea
*/
private JTextArea getJTextArea() {
if (jTextArea == null) {
jTextArea = new JTextArea();
}
return jTextArea;
}
/**
* This method initializes jMenuItem4
*
* @return javax.swing.JMenuItem
*/
private JMenuItem getJMenuItem4() {
if (jMenuItem4 == null) {
jMenuItem4 = new JMenuItem();
jMenuItem4.setText("复制");
jMenuItem4.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent e) {
MainFrame.this.setClipboardText(MainFrame.this.getToolkit().getSystemClipboard(),jTextArea.getSelectedText());
}
});
}
return jMenuItem4;
}
/**
* This method initializes jMenuItem5
*
* @return javax.swing.JMenuItem
*/
private JMenuItem getJMenuItem5() {
if (jMenuItem5 == null) {
jMenuItem5 = new JMenuItem();
jMenuItem5.setText("剪切");
jMenuItem5.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent e) {
MainFrame.this.setClipboardText(MainFrame.this.getToolkit().getSystemClipboard(),jTextArea.getSelectedText());
jTextArea.setText(jTextArea.getText().substring(0,jTextArea.getSelectionStart()));
}
});
}
return jMenuItem5;
}
/**
* This method initializes jMenuItem6
*
* @return javax.swing.JMenuItem
*/
private JMenuItem getJMenuItem6() {
if (jMenuItem6 == null) {
jMenuItem6 = new JMenuItem();
jMenuItem6.setText("黏贴");
jMenuItem6.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent e) {
try{
jTextArea.setText(jTextArea.getText().substring(0,jTextArea.getSelectionStart()));
jTextArea.setText(jTextArea.getText()+(MainFrame.this.getClipboardText(MainFrame.this.getToolkit().getSystemClipboard())));
}
catch(Exception ee){
ee.printStackTrace();
}
}
});
}
return jMenuItem6;
}
/**
* This method initializes jMenuItem7
*
* @return javax.swing.JMenuItem
*/
private JMenuItem getJMenuItem7() {
if (jMenuItem7 == null) {
jMenuItem7 = new JMenuItem();
jMenuItem7.setText("关于记事本");
jMenuItem7.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent e) {
////暂无代码!!
}
});
}
return jMenuItem7;
}
public static void main(String args[]){
new MainFrame();
}
public static MainFrame getMyMainFrame() {
return myMainFrame;
}
public static void setMyMainFrame(MainFrame myMainFrame) {
MainFrame.myMainFrame = myMainFrame;
}
public static String getTextstr() {
return textstr;
}
public static void setTextstr(String textstr) {
MainFrame.textstr = textstr;
}
protected static String getClipboardText(Clipboard clip) throws Exception{
Transferable clipT = clip.getContents(null);// 获取剪切板中的内容
if (clipT != null) {
if (clipT.isDataFlavorSupported(DataFlavor.stringFlavor)) // 检查内容是否是文本类型
return (String)clipT.getTransferData(DataFlavor.stringFlavor);
}
return null;
}
protected static void setClipboardText(Clipboard clip, String writeMe) {
Transferable tText = new StringSelection(writeMe);
clip.setContents(tText, null);
}
} // @jve:decl-index=0:visual-constraint="10,10"
Print.java--打印内容定义 [code] import java.awt.*; import java.awt.event.*; import java.awt.print.*; import java.awt.image.*; import javax.swing.*; import javax.swing.event.*; import javax.swing.border.*; import javax.swing.event.*; import java.util.*; import java.io.*; public class Print implements Printable{ int m_wPage; int m_hPage; int m_orientation; Printable m_target; int maxNumPage=1; String title="数据表格打印"; Font titleFont=new Font("黑体",Font.BOLD,14); boolean hasTail=true; int tailAlign=0; int headAlign=0; int topSpace=0; int leftSpace=0; int yStart=0; int yEnd=0; int xStart=topSpace; int xEnd=0; int x=0,y=0; String strTemp="打印内容"; public void doPrint(){ try{ m_orientation=PageFormat.PORTRAIT; //设置打印对象,默认纸张 PrinterJob prnJob=PrinterJob.getPrinterJob(); PageFormat pageFormat=prnJob.defaultPage(); pageFormat.setOrientation(m_orientation); m_wPage=(int)(pageFormat.getWidth()); m_hPage=(int)(pageFormat.getHeight()); //将待打印的窗体根据默认纸张设置传入打印对象 prnJob.setPrintable(this,pageFormat); if(!prnJob.printDialog()) return; prnJob.print(); }catch(PrinterException ex){ ex.printStackTrace(); System.err.println("打印错误:"+ex.toString()); } } /** * 初始化打印参数 */ public void initPrintParameter() { } /** *构造打印内容,以送打印机打印 */ public int print(Graphics pg,PageFormat pageFormat, int pageIndex) throws PrinterException{ //初始化打印参数 initPrintParameter(); //将画布设置为页面大小 pg.translate((int)pageFormat.getImageableX(), (int)pageFormat.getImageableY()); int wPage=0; int hPage=0; //根据打印机页面设置调整画布大小 if(pageFormat.getOrientation()==pageFormat.PORTRAIT){ wPage=(int)pageFormat.getImageableWidth(); hPage=(int)pageFormat.getImageableHeight(); } else{ wPage=(int)pageFormat.getImageableWidth(); wPage+=wPage/2; hPage=(int)pageFormat.getImageableHeight(); pg.setClip(0,0,wPage,hPage); } wPage=wPage-2*leftSpace; hPage=hPage-2*topSpace; xStart=leftSpace; xEnd=wPage-2; //为画布设置颜色和字体 int y=topSpace; pg.setFont(titleFont); pg.setColor(Color.black); //画标题,并使其居中 Font fn=pg.getFont(); FontMetrics fm=pg.getFontMetrics(); y+=fm.getAscent(); alignText(title,pg,y,xStart,xEnd,headAlign); y+=30; x=leftSpace+2; Font headerFont=new Font("宋体",Font.BOLD,14); pg.setFont(headerFont); fm=pg.getFontMetrics(); int h=fm.getAscent(); yStart=y-1; y+=h; pg.setFont(headerFont); fm=pg.getFontMetrics(); int header=y; h=fm.getHeight(); //计算行高,每页行数,总行数和指定页码的起始行、结束行 int rowH=Math.max(h,10); int tailH=rowH+30; int rowPerPage=0; int leftPix=0; if(hasTail){ rowPerPage=(hPage-header-tailH)/rowH; leftPix=(hPage-header-tailH)%rowH; yEnd=hPage-leftPix-tailH+2; } else{ rowPerPage=(hPage-header)/rowH; leftPix=(hPage-header)%rowH; yEnd=hPage-leftPix+2; } pg.drawString(strTemp,x,y); //画表格边框 pg.drawLine(xStart,yStart,xStart,yEnd); pg.drawLine(xStart,yStart,xEnd,yStart); pg.drawLine(xEnd,yStart,xEnd,yEnd); pg.drawLine(xStart,yEnd,xEnd,yEnd); //打印页码 if(hasTail){ int pageNumber=pageIndex+1; String s="第"+pageNumber+"页"; alignText(s,pg,yEnd+30,xStart,xEnd,tailAlign); } System.gc(); return PAGE_EXISTS; } /** * 文字排列,坐标在y处,显示范围(start-end) * 0表示居中显示,1表示左对齐,2表示右对齐 */ private void alignText(String s,Graphics pg,int y,int start, int end,int mode){ Font fn=pg.getFont(); FontMetrics fm=pg.getFontMetrics(); int wString=fm.stringWidth(s); int x=start; switch(mode) { case 0: if((end-start-wString)0) x=start+(end-start-wString)/2; break; case 1: break; case 2: if((end-start-wString)0) x=start+(end-start-wString); break; } pg.drawString(s,x,y); } public static void main(String[] args){ Print p=new Print(); p.doPrint(); } } [code] 运行方法: javac -d . Print.java java Print 自己运行一下 from:
采纳哦
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.border.*;
class test implements ActionListener
{
JFrame frame;
JButton b1,b2,b3,b4,b5,b6,b7,b8,b9,b10,b11,b12,b13,b14,b15,b16,b17,b18,b19,b20,b21,b22,b23,b24,b25,b26,b27,b28,b29,b30,b31;
JTextArea ta;
JPanel p1,p2,p3,p4;
JMenuBar mb;
JMenu m1,m2,m3;
JMenuItem mt1,mt2,mt3,mt4,mt5,mt6,mt7;
JRadioButton rb1,rb2;
ButtonGroup bg;
Double d1=0.0,d2=0.0,d3=0.0,d4=1.0,d5=1.0;
String s1="",s2="",s3="",s4="";
int a=0;
char c1;
int i=0;
public static void main(String[] args)
{
test that=new test();
that.go();
}
public void go()
{
frame=new JFrame("计算器");
Container cp= frame.getContentPane();
cp.setLayout(new FlowLayout());
b1=new JButton("7");b2=new JButton("8");b3=new JButton("9");b4=new JButton("/");b5=new JButton("1/x");b6=new JButton("sin");b7=new JButton("log");
b8=new JButton("4");b9=new JButton("5");b10=new JButton("6");b11=new JButton("*");b12=new JButton("x^y");b13=new JButton("cos");b14=new JButton("ln");
b15=new JButton("1");b16=new JButton("2");b17=new JButton("3");b18=new JButton("-");b19=new JButton(new ImageIcon("lanying.gif"));b20=new JButton("tan");b21=new JButton("x^3");
b22=new JButton("0");b23=new JButton("+/-");b24=new JButton(".");b25=new JButton("+");b26=new JButton("√x");b27=new JButton("cot");b28=new JButton("x^2");
b29=new JButton("Backspace");b30=new JButton("C");b31=new JButton("=");
mb=new JMenuBar();
m1=new JMenu("文件(F)");m2=new JMenu("编辑(E)");m3=new JMenu("帮助(H)");
mt1=new JMenuItem("清零");mt2=new JMenuItem("退出");mt3=new JMenuItem("复制");mt4=new JMenuItem("粘贴");mt5=new JMenuItem("版本");mt6=new JMenuItem("标准型");mt7=new JMenuItem("科学型");
ta=new JTextArea(1,30);
p1=new JPanel();p2=new JPanel();p3=new JPanel();p4=new JPanel();
rb1=new JRadioButton("科学型");rb2=new JRadioButton("标准型");
bg=new ButtonGroup();
b1.setForeground(Color.blue);b1.setBackground(Color.white);b2.setForeground(Color.blue);b2.setBackground(Color.white);
b3.setForeground(Color.blue);b3.setBackground(Color.white);b8.setForeground(Color.blue);b8.setBackground(Color.white);
b9.setForeground(Color.blue);b9.setBackground(Color.white);b10.setForeground(Color.blue);b10.setBackground(Color.white);
b15.setForeground(Color.blue);b15.setBackground(Color.white);b16.setForeground(Color.blue);b16.setBackground(Color.white);
b17.setForeground(Color.blue);b17.setBackground(Color.white);b22.setForeground(Color.blue);b22.setBackground(Color.white);
b23.setForeground(Color.blue);b23.setBackground(Color.white);b24.setForeground(Color.blue);b24.setBackground(Color.white);
b4.setForeground(Color.red);b4.setBackground(Color.white);b11.setForeground(Color.red);b11.setBackground(Color.white);
b18.setForeground(Color.red);b18.setBackground(Color.white);b25.setForeground(Color.red);b25.setBackground(Color.white);
b5.setForeground(Color.blue);b5.setBackground(Color.white);b6.setForeground(Color.blue);b6.setBackground(Color.white);
b7.setForeground(Color.blue);b7.setBackground(Color.white);b12.setForeground(Color.blue);b12.setBackground(Color.white);
b13.setForeground(Color.blue);b13.setBackground(Color.white);b14.setForeground(Color.blue);b14.setBackground(Color.white);
b19.setForeground(Color.blue);b19.setBackground(Color.white);b20.setForeground(Color.blue);b20.setBackground(Color.white);
b21.setForeground(Color.blue);b21.setBackground(Color.white);b26.setForeground(Color.blue);b26.setBackground(Color.white);
b27.setForeground(Color.blue);b27.setBackground(Color.white);b28.setForeground(Color.blue);b28.setBackground(Color.white);
b29.setForeground(Color.red);b29.setBackground(Color.white);b30.setForeground(Color.red);b30.setBackground(Color.white);
b31.setForeground(Color.red);b31.setBackground(Color.white);
bg.add(rb1);bg.add(rb2);
p1.setBackground(Color.yellow);
cp.setBackground(Color.CYAN);
m1.setMnemonic(KeyEvent.VK_F);m2.setMnemonic(KeyEvent.VK_E);m3.setMnemonic(KeyEvent.VK_H);
m1.add(mt6);m1.add(mt7);m1.addSeparator();m1.add(mt1);m1.addSeparator();m1.add(mt2);m2.add(mt3);m2.addSeparator();m2.add(mt4);m3.add(mt5);
mb.add(m1);mb.add(m2);mb.add(m3);
frame.setJMenuBar(mb);
p2.setLayout(new GridLayout(4,7));
p3.setLayout(new GridLayout(1,3));
ta.setEditable(false);
p1.add(ta);
p2.add(b1);p2.add(b2);p2.add(b3);p2.add(b4);p2.add(b5);p2.add(b6);p2.add(b7);
p2.add(b8);p2.add(b9);p2.add(b10);p2.add(b11);p2.add(b12);p2.add(b13);p2.add(b14);
p2.add(b15);p2.add(b16);p2.add(b17);p2.add(b18);p2.add(b19);p2.add(b20);p2.add(b21);
p2.add(b22);p2.add(b23);p2.add(b24);p2.add(b25);p2.add(b26);p2.add(b27);p2.add(b28);
p3.add(b29);p3.add(b30);p3.add(b31);
Border etched=BorderFactory.createEtchedBorder();
Border border=BorderFactory.createTitledBorder(etched,"计算类型");
p4.add(rb1);p4.add(rb2);
p4.setBorder(border);
b2.setActionCommand("8");
b2.addActionListener(this);
cp.add(p1);cp.add(p4);cp.add(p2);cp.add(p3);
frame.setSize(400,330);
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
b1.setActionCommand("7");
b1.addActionListener(this);
b2.setActionCommand("8");
b2.addActionListener(this);
b3.setActionCommand("9");
b3.addActionListener(this);
b4.setActionCommand("/");
b4.addActionListener(this);
b5.setActionCommand("1/x");
b5.addActionListener(this);
b6.setActionCommand("sin");
b6.addActionListener(this);
b7.setActionCommand("log");
b7.addActionListener(this);
b8.setActionCommand("4");
b8.addActionListener(this);
b9.setActionCommand("5");
b9.addActionListener(this);
b10.setActionCommand("6");
b10.addActionListener(this);
b11.setActionCommand("*");
b11.addActionListener(this);
b12.setActionCommand("x^y");
b12.addActionListener(this);
b13.setActionCommand("cos");
b13.addActionListener(this);
b14.setActionCommand("ln");
b14.addActionListener(this);
b15.setActionCommand("1");
b15.addActionListener(this);
b16.setActionCommand("2");
b16.addActionListener(this);
b17.setActionCommand("3");
b17.addActionListener(this);
b18.setActionCommand("-");
b18.addActionListener(this);
b19.setActionCommand("x!");
b19.addActionListener(this);
b20.setActionCommand("tan");
b20.addActionListener(this);
b21.setActionCommand("x^3");
b21.addActionListener(this);
b22.setActionCommand("0");
b22.addActionListener(this);
b23.setActionCommand("+/-");
b23.addActionListener(this);
b24.setActionCommand(".");
b24.addActionListener(this);
b25.setActionCommand("+");
b25.addActionListener(this);
b26.setActionCommand("√x");
b26.addActionListener(this);
b27.setActionCommand("cot");
b27.addActionListener(this);
b28.setActionCommand("x^2");
b28.addActionListener(this);
b29.setActionCommand("Backspace");
b29.addActionListener(this);
b30.setActionCommand("C");
b30.addActionListener(this);
b31.setActionCommand("=");
b31.addActionListener(this);
rb1.setActionCommand("kxx");
rb1.addActionListener(this);
rb2.setActionCommand("bzx");
rb2.addActionListener(this);
}
public void actionPerformed(ActionEvent e) //throws Exception
{
if (e.getActionCommand()=="bzx")
{
b5.setEnabled(false);b6.setEnabled(false);b7.setEnabled(false);
b12.setEnabled(false);b13.setEnabled(false);b14.setEnabled(false);
b19.setEnabled(false);b20.setEnabled(false);b21.setEnabled(false);
b26.setEnabled(false);b27.setEnabled(false);b28.setEnabled(false);
}
if (e.getActionCommand()=="kxx")
{
b5.setEnabled(true);b6.setEnabled(true);b7.setEnabled(true);
b12.setEnabled(true);b13.setEnabled(true);b14.setEnabled(true);
b19.setEnabled(true);b20.setEnabled(true);b21.setEnabled(true);
b26.setEnabled(true);b27.setEnabled(true);b28.setEnabled(true);
}
if (e.getActionCommand()=="1")
{
ta.append("1");
}
if (e.getActionCommand()=="2")
{
ta.append("2");
}
if (e.getActionCommand()=="3")
{
ta.append("3");
}
if (e.getActionCommand()=="4")
{
ta.append("4");
}
if (e.getActionCommand()=="5")
{
ta.append("5");
}
if (e.getActionCommand()=="6")
{
ta.append("6");
}
if (e.getActionCommand()=="7")
{
ta.append("7");
}
if (e.getActionCommand()=="8")
{
ta.append("8");
}
if (e.getActionCommand()=="9")
{
ta.append("9");
}
if (e.getActionCommand()=="0")
{
ta.append("0");
}
if (e.getActionCommand()=="+")
{
s1=ta.getText();
d1 = Double.parseDouble(s1);
ta.setText("");
i=1;
}
if (e.getActionCommand()=="-")
{
s1=ta.getText();
d1 = Double.parseDouble(s1);
ta.setText("");
i=2;
}
if (e.getActionCommand()=="*")
{
s1=ta.getText();
d1 = Double.parseDouble(s1);
ta.setText("");
i=3;
}
if (e.getActionCommand()=="/")
{
s1=ta.getText();
d1 = Double.parseDouble(s1);
ta.setText("");
i=4;
}
if (e.getActionCommand()=="=")
{
s2=ta.getText();
d2=Double.parseDouble(s2);
if(i==1)
{
d3=d1+d2;
ta.setText( d3.toString());
}
if(i==2)
{
d3=d1-d2;
ta.setText( d3.toString());
}
if(i==3)
{
d3=d1*d2;
ta.setText( d3.toString());
}
if(i==4)
{
if(d2==0.0)
ta.setText("ERROR");
else
{
d3=d1/d2;
ta.setText( d3.toString());
}
}
if (i==5)
{
s2=ta.getText();
d2 = Double.parseDouble(s2);
for (int l=1;l=d2 ; l++)
{
d5=d5*d1;
}
ta.setText( d5.toString());
}
}
if (e.getActionCommand()=="C")
{
ta.setText("");
d4=1.0;
d5=1.0;
}
/*if (e.getActionCommand()=="Backspace")
{
s3=ta.getText();
a=s3.length();
//ta.cut(ta.select(a-1,a));
s4=ta.getText(1,3);
ta.setText(s4);
}
*/
if (e.getActionCommand()=="1/x")
{
s1=ta.getText();
d1 = Double.parseDouble(s1);
d2=1/d1;
ta.setText( d2.toString());
}
if (e.getActionCommand()==".")
{
ta.append(".");
}
if (e.getActionCommand()=="+/-")
{
s1=ta.getText();
d1 = Double.parseDouble(s1);
d2=0-d1;
ta.setText( d2.toString());
}
if (e.getActionCommand()=="x^2")
{
s1=ta.getText();
d1 = Double.parseDouble(s1);
d2=d1*d1;
ta.setText( d2.toString());
}
if (e.getActionCommand()=="x^3")
{
s1=ta.getText();
d1 = Double.parseDouble(s1);
d2=d1*d1*d1;
ta.setText( d2.toString());
}
if (e.getActionCommand()=="x^y")
{
s1=ta.getText();
d1 = Double.parseDouble(s1);
ta.setText("");
i=5;
// d2=d1*d1*d1;
// ta.setText( d2.toString());
}
if (e.getActionCommand()=="√x")
{
s1=ta.getText();
d1 = Double.parseDouble(s1);
d2=Math.sqrt(d1);
ta.setText( d2.toString());
}
if (e.getActionCommand()=="x!")
{
s1=ta.getText();
d1 = Double.parseDouble(s1);
if (d10)
{
ta.setText( "error");
}
else if (d1==0)
{
ta.setText( "0.0");
}
else {
for (int k=1;k=d1 ;k++ )
d4=d4*k;
ta.setText( d4.toString());
}
}
if (e.getActionCommand()=="sin")
{
s1=ta.getText();
d1 = Double.parseDouble(s1);
d2=Math.sin(3.1415926*d1/180);
ta.setText( d2.toString());
}
}
}
public int print(Graphics gra, PageFormat pf, int pageIndex) throws PrinterException {
System.out.println("pageIndex=" + pageIndex);
Component c = null;
//print string
String str = "
Hello Word!
";
//转换成Graphics2D
Graphics2D g2 = (Graphics2D) gra;
//设置打印颜色为黑色
g2.setColor(Color.black);
//打印起点坐标
double x = pf.getImageableX();
double y = pf.getImageableY();
switch (pageIndex) {
case 0:
//设置打印字体(字体名称、样式和点大小)(字体名称可以是物理或者逻辑名称)
//Java平台所定义的五种字体系列:Serif、SansSerif、Monospaced、Dialog 和 DialogInput
Font font = new Font("新宋体", Font.PLAIN, 9);
g2.setFont(font);//设置字体
//BasicStroke bs_3=new BasicStroke(0.5f);
float[] dash1 = {2.0f};
//设置打印线的属性。
//1.线宽 2、3、不知道,4、空白的宽度,5、虚线的宽度,6、偏移量
g2.setStroke(new BasicStroke(0.5f, BasicStroke.CAP_BUTT, BasicStroke.JOIN_MITER, 2.0f, dash1, 0.0f));
//g2.setStroke(bs_3);//设置线宽
float heigth = font.getSize2D();//字体高度
System.out.println("x=" + x);
// -1- 用Graphics2D直接输出
//首字符的基线(右下部)位于用户空间中的 (x, y) 位置处
//g2.drawLine(10,10,200,300);
Image src = Toolkit.getDefaultToolkit().getImage("F:\\workspace\\QQ.png");
g2.drawImage(src, (int) x, (int) y, c);
int img_Height = src.getHeight(c);
int img_width = src.getWidth(c);
//System.out.println("img_Height="+img_Height+"img_width="+img_width) ;
g2.drawString(str, (float) x, (float) y + 1 * heigth + img_Height);
g2.drawLine((int) x, (int) (y + 1 * heigth + img_Height + 10), (int) x + 200, (int) (y + 1 * heigth + img_Height + 10));
g2.drawImage(src, (int) x, (int) (y + 1 * heigth + img_Height + 11), c);
return PAGE_EXISTS;
default:
return NO_SUCH_PAGE;
}
}
1、首先在电脑中新建一个记事本,将记事本的后缀改为“.java”,如下图所示。
2、然后使用记事本的方式打开,输入java程序代码,如下图所示。
3、接着在键盘上按“win+R”快捷键键打开运行,输入“cmd”,如下图所示。
4、在命令行输入“D:”,按“Enter”键进去D盘,再输入“cd Desktop”进去Desktop文件夹,如下图所示。
5、最后再输入“javac Test.java”,按“Enter”键编译java程序,如下图所示就完成了。