目录
3.43
import java.io.*; import java.awt.*; import java.awt.event.* ; //**********found********** import javax.swing.*; public class Java_3 implements ActionListener { JTextArea ta; JFrame f ; JLabel label; JButton bs,br; public static void main(String[ ] args){ Java_3 t = new Java_3(); t.go(); } void go(){ f = new JFrame("Save data"); f.setSize( 20, 400); //**********found********** f.setLayout(new FlowLayout()); label = new JLabel("请输入需要保存的文本 :"); ta = new JTextArea(3,20); bs = new JButton("保存"); br = new JButton("读取"); f.add(label); f.add(ta); f.add(bs); f.add(br); //**********found********** bs.addActionListener(this); //**********found********** br.addActionListener(new ReadFile()); f.setSize(400,400); f.pack( ); f.setVisible(true) ; f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); } public void actionPerformed(ActionEvent event){ try{ FileWriter out = new FileWriter("out.txt"); String str = ta.getText(); //**********found********** out.write(str); out.close(); } catch( Exception e){ } ta.setText(" "); } class ReadFile implements ActionListener{ public void actionPerformed(ActionEvent event){ String cc; StringBuffer str = new StringBuffer(""); try{ FileReader in = new FileReader("out.txt"); //**********found********** BufferedReader bin= new BufferedReader(in); while ( (cc = bin.readLine())!= null) str.append(cc); bin.close(); ta.setText(str.toString()); } catch( Exception e){ } } } }
3.44
import java.awt.*; import java.awt.event.*; import javax.swing.*; //**********found********** public class Java_3 extends JFrame { private JTextField username; private JPasswordField password; private JLabel jl1; private JLabel jl2; private JLabel jl3; private JLabel jl4; private JButton bu1; private JButton bu2; private JButton bu3; private JCheckBox jc1; private JCheckBox jc2; private JComboBox jcb; public Java_3() { this.setTitle("QQ2022正式版"); //**********found********** init(); this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // 设置布局方式为绝对定位 this.setLayout(null); this.setBounds(0, 0, 355, 265); // 设置窗体的标题图标 Image image = new ImageIcon("a.png").getImage(); this.setIconImage(image); // 窗体大小不能改变 this.setResizable(false); // 居中显示 this.setLocationRelativeTo(null); this.setVisible(true); } public void init() { //**********found********** Container con = this.getContentPane(); jl1 = new JLabel(); // 设置背景图片 Image image1 = new ImageIcon("background.jpg").getImage(); jl1.setIcon(new ImageIcon(image1)); jl1.setBounds(0, 0, 355, 265); jl2 = new JLabel(); Image image2 = new ImageIcon("a.gif").getImage(); jl2.setIcon(new ImageIcon(image2)); jl2.setBounds(40, 95, 50, 60); username = new JTextField(); username.setBounds(50, 50, 150, 20); jl3 = new JLabel("注册账号"); jl3.setBounds(210, 50, 70, 20); password = new JPasswordField(); password.setBounds(50, 80, 150, 20); jl4 = new JLabel("找回密码"); jl4.setBounds(210, 80, 70, 20); jc1 = new JCheckBox("记住密码"); jc1.setBounds(125, 135, 80, 15); jc2 = new JCheckBox("自动登录"); jc2.setBounds(215, 135, 80, 15); jcb = new JComboBox(); jcb.addItem("在线"); jcb.addItem("隐身"); jcb.addItem("离开"); jcb.setBounds(40, 135, 55, 20); //**********found********** bu1 = new JButton("登录"); bu1.setBounds(250, 200, 65, 20); //**********found********** bu1.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { String str=e.getActionCommand(); if("登录".equals(str)){ String getName =username.getText(); JOptionPane.showConfirmDialog(null, "您输入的用户名是"+getName); } } }); bu2 = new JButton("多账号"); bu2.setBounds(25, 200, 75, 20); bu3 = new JButton("设置"); bu3.setBounds(140, 200, 65, 20); // 所有组件用容器装载 jl1.add(jl2); jl1.add(jl3); jl1.add(jl4); jl1.add(jc1); jl1.add(jc2); jl1.add(jcb); jl1.add(bu1); jl1.add(bu2); jl1.add(bu3); con.add(jl1); con.add(username); con.add(password); } public static void main(String[] args) { Java_3 qq = new Java_3(); } }
3.45
import javax.swing.*; import java.awt.*; import java.awt.event.*; public class Java_3 extends JFrame implements ActionListener{ JTextField nval = new JTextField(10); //**********Found********** JButton calcBtn = new JButton("计算") ; JTextArea result = new JTextArea(10,20); void initFrame(){ Container content = getContentPane(); JPanel calcPanel = new JPanel(); calcPanel.add(new JLabel("N值")); //**********Found********** calcPanel.add(nval); calcPanel.add(calcBtn) ; content.add(calcPanel,"North"); //**********Found********** calcBtn.addActionListener(this); content.add(result,"Center"); result. setEditable(false) ; } public Java_3(){ super("计算素数"); setSize(500,200); initFrame(); setVisible(true); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); } public void actionPerformed(ActionEvent e){ if(e.getSource()==calcBtn){ int N=Integer.parseInt(nval.getText()); int [] prime=new int[N/3+2]; prime[0]=2;prime[1]=3; int k=2; for(int m=5;m<=N;m+=2){ int j=1,isprime=1; int kk=Math.round((float)Math.sqrt(m)); while(prime[j]<=kk){ if(m%prime[j]==0){ //**********Found********** isprime=0; break; }else //**********Found********** j++ ; } if(isprime==1) prime[k++]=m; } //**********Found********** String str="Total prime number: "+k ; result.setText(""); result.append(str ); } } public static void main(String[] args){ new Java_3(); } }
3.46
//**********Found********** import javax.swing.*; import java.awt.*; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.event.KeyEvent; import java.net.*; public class Java_3 extends JFrame { public Java_3() { //**********Found********** setTitle("三国攻略游戏"); setBounds(650, 350, 650, 400); setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); Container container = getContentPane(); setLayout(new BorderLayout()); JPanel p1 = new JPanel(); JLabel bxx = new JLabel("北京天下第一攻略游戏有限公司荣誉出品 "); p1.add(bxx); this.add(BorderLayout.SOUTH, p1); bxx.setBackground(Color.white); //**********Found********** JMenuBar m = new JMenuBar(); setJMenuBar(m); JMenu m1 = new JMenu("魏国"); JMenu m2 = new JMenu("蜀国"); JMenu m3 = new JMenu("吴国"); JMenu n1 = new JMenu("曹操"); JMenu n2 = new JMenu("夏侯惇"); JMenu n3 = new JMenu("司马懿"); JMenu n4 = new JMenu("刘备"); JMenu n5 = new JMenu("关羽"); JMenu n6 = new JMenu("赵云"); JMenuItem n31 = new JMenuItem("指挥能力"); JMenuItem n32 = new JMenuItem("战斗技能"); JMenuItem n33 = new JMenuItem("管理能力"); JMenuItem n21 = new JMenuItem("指挥能力"); JMenuItem n22 = new JMenuItem("战斗技能"); JMenuItem n23 = new JMenuItem("管理能力"); m.add(m1); m.add(m2); m.add(m3); m1.add(n1); m1.add(n2); m1.add(n3); m2.add(n4); m2.add(n5); m2.add(n6); n4.add(n31); n4.add(n32); n4.add(n33); n1.add(n21); n1.add(n22); n1.add(n23); //**********Found********** n31.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent arg0) { JLabel l1 = new JLabel(); JLabel l2 = new JLabel(); JDialog dialog = new JDialog(); dialog.setLayout(new GridLayout(2, 1, 5, 5)); dialog.add(l1); dialog.add(l2); dialog.setModal(true); dialog.setSize(354, 200); dialog.setLocationByPlatform(true); dialog.setTitle("刘备指挥能力"); dialog.setVisible(true); } }); //**********Found********** n32.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent arg0) { JLabel l1 = new JLabel(); JLabel l2 = new JLabel(); JDialog dialog = new JDialog(); dialog.setLayout(new GridLayout(2, 1, 5, 5)); dialog.add(l1); dialog.add(l2); dialog.setModal(true); dialog.setSize(500, 400); dialog.setLocationByPlatform(true); dialog.setTitle("刘备战斗技能"); dialog.setVisible(true); } }); n33.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent arg0) { JLabel l1 = new JLabel(); JLabel l2 = new JLabel(); JDialog dialog = new JDialog(); dialog.setLayout(new GridLayout(2, 1, 10, 10)); dialog.add(l1); dialog.add(l2); dialog.setModal(true); dialog.setSize(500, 400); dialog.setLocationByPlatform(true); dialog.setTitle("刘备管理能力"); dialog.setVisible(true); } }); //**********Found********** setVisible(true); } public static void main(String[] args) { new Java_3(); } }
3.47
import java.io.*; import java.awt.*; import java.awt.event.* ; import javax.swing.*; //**********Found********** public class Java_3 implements ActionListener { JTextArea ta1,ta2; JFrame f ; JLabel label1,label2; JButton bs,br; //**********Found********** char[] aa={'A','B','C','D','E'}; String ss; public static void main(String args[ ]){ Java_3 t = new Java_3(); t.go(); } void go(){ f = new JFrame("Data Input and Output"); f.setSize( 20, 200); //**********Found********** f.setLayout(new GridLayout(2,3,5,5)); label1 = new JLabel(" 数组内容:"); ta1 = new JTextArea(2,20); ss=new String(aa); ta1.setText(ss); bs = new JButton("保存"); label2 = new JLabel(" 已保存:"); ta2 = new JTextArea(2,20); br = new JButton("读取"); f.add(label1); f.add(ta1); f.add(bs); f.add(label2); f.add(ta2); f.add(br); bs.addActionListener(this); //**********Found********** br.addActionListener( new ReadFile() ); f.setSize(400,400); f.pack( ); f.setVisible(true) ; f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); } public void actionPerformed(ActionEvent event){ try{ DataOutputStream out = new DataOutputStream(new FileOutputStream("out.txt")); for (int i=0; i<=aa.length; i++) //**********Found********** out.writeChar(aa[i]); out.close(); } catch( Exception e){ } } class ReadFile implements ActionListener{ public void actionPerformed(ActionEvent event){ int i=0; char c; char[] a1= new char[50]; String str ; DataInputStream in; try{ //**********Found********** in = new DataInputStream( new FileInputStream("out.txt")); while ( i<aa.length){ c = in.readChar(); a1[i++]=c; } in.close(); str = new String(a1); ta2.setText(str); } catch( Exception e){ } } } }
3.48
import javax.swing.*; import java.awt.*; import java.awt.event.*; public class Java_3 extends JFrame implements ActionListener { private double x=0; private double y=0; JTextField xval1 = new JTextField(10); JTextField xval2 = new JTextField(10); JButton calcBtn = new JButton("计算"); JTextArea result = new JTextArea(10,20); void initFrame() { Container content = getContentPane(); content.setLayout(new BorderLayout()); JPanel calcPanel = new JPanel(); calcPanel.setLayout(new FlowLayout()); calcPanel.add(new JLabel("角度1")); calcPanel.add(xval1); //***************************Found********************* calcPanel.add( new JLabel("角度2") ); calcPanel.add(xval2); //***************************Found********************* calcPanel.add(calcBtn); content.add(calcPanel,"North"); calcBtn.addActionListener(this); content.add(result,"Center"); //***************************Found********************* result.setEditable(false); } public Java_3() { super("计算两角和余弦函数"); setSize(500,200); initFrame(); setVisible(true); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); } public void actionPerformed(ActionEvent e) { //***************************Found********************* if (e.getSource()== calcBtn ){ x=Double.parseDouble(xval1.getText())/180*3.1415926; //***************************Found********************* x=x+Double.parseDouble( xval2.getText() )/180*3.1415926; //***************************Found********************* y= Math.cos(x); String str="cos( ("+xval1.getText()+"+"+xval2.getText()+") *3.1415926/180)= "+y+'\n'; result. append(str) ; } } public static void main(String[] args) { new Java_3(); } }
3.49
import java.io.*; //**********Found********** import java.awt.event.* ; import javax.swing.*; public class Java_3 { static JTextArea ta; JFrame frame ; public static void main(String args[ ]){ Java_3 t = new Java_3(); //**********Found********** t.frameAndMenu(); } void frameAndMenu(){ frame = new JFrame(); frame.setSize(400,150); frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); JMenuBar menuBar = new JMenuBar(); //**********Found********** JMenu menu = new JMenu("Menu"); JMenuItem menuItemReadFile = new JMenuItem("ReadFile"); //**********Found********** menuItemReadFile.addActionListener( new ReadFile()); JMenuItem menuItemExit = new JMenuItem("Exit"); menuItemExit.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { System.exit(0); } }); menu.add(menuItemReadFile); menu.add(menuItemExit); menuBar.add(menu); frame.setJMenuBar(menuBar); ta = new JTextArea(10,100); frame.add(ta); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setLocation(600, 300); frame.setVisible(true); } } //**********Found********** class ReadFile implements ActionListener{ public void actionPerformed(ActionEvent event){ String cc; StringBuffer str = new StringBuffer(""); try{ FileReader in = new FileReader("java3.txt"); //**********Found********** BufferedReader bin= new BufferedReader(in); while ( (cc = bin.readLine())!= null) str.append(cc); bin.close(); Java_3.ta.setText(str.toString()); } catch( Exception e){ } } }
3.50
//**********Found********** import java.io.*; import java.awt.event.* ; import javax.swing.*; //**********Found********** public class Java_3 implements ActionListener{ JTextArea ta; JFrame frame ; public static void main(String args[ ]){ Java_3 fr = new Java_3(); fr.frameAndMenu(); } void frameAndMenu(){ frame = new JFrame(); frame.setSize(200,150); frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); JMenuBar menuBar = new JMenuBar(); JMenu menu = new JMenu("Menu"); JMenuItem menuItemSaveFile = new JMenuItem("SaveFile"); //**********Found********** menuItemSaveFile.addActionListener(this); JMenuItem menuItemExit = new JMenuItem("Exit"); //**********Found********** menuItemExit.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { System.exit(0); } }); menu.add(menuItemSaveFile); menu.add(menuItemExit); //**********Found********** menuBar.add(menu); frame.setJMenuBar(menuBar); ta = new JTextArea(3,20); frame.add(ta); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setLocation(600, 300); frame.setVisible(true); } public void actionPerformed(ActionEvent event){ try{ FileWriter out = new FileWriter("java_3.txt"); //**********Found********** String str = ta.getText(); out.write(str); out.close(); } catch( Exception e){ } ta.setText(" "); } }
3.51
import javax.swing.*; import java.awt.*; import java.awt.event.*; public class Java_3 extends JFrame implements ActionListener{ private double x=0; private double y=0; JTextField xval = new JTextField(10); JButton calcBtn = new JButton("计算"); JTextArea result = new JTextArea(10,20); boolean isAngleRad=true; JButton angleUnitBtn=new JButton("设成角度"); void initFrame(){ Container content = getContentPane(); content.setLayout(new BorderLayout()); JPanel calcPanel = new JPanel(); calcPanel.setLayout(new FlowLayout()); //***************************Found********************* calcPanel.add( new JLabel("角的值") ); calcPanel.add(xval); calcPanel.add(calcBtn) ; calcPanel.add(angleUnitBtn); content.add(calcPanel,"North"); calcBtn.addActionListener(this); //***************************Found********************* angleUnitBtn.addActionListener(this) ; content.add(result,"Center"); //***************************Found********************* result.setEditable(false); } public Java_3(){ super("计算余弦函数"); setSize(500,200); initFrame(); setVisible(true); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); } public void actionPerformed(ActionEvent e){ if (e.getSource()== calcBtn){ String str; if(isAngleRad) { x=Double.parseDouble(xval.getText()); str="cos("+x+")="; }else { x=Double.parseDouble(xval.getText())/180*3.1415926; //***************************Found********************* str="cos("+xval.getText() +"*3.14159/180)="; }; y=Math.cos(x); result.append(str+y+'\n'); }else if(e.getSource()== angleUnitBtn){ if(isAngleRad) angleUnitBtn.setText("设成弧度"); else //***************************Found********************* angleUnitBtn.setText( "设成角度"); //***************************Found********************* isAngleRad= !isAngleRad ; } } public static void main(String[] args){ new Java_3(); } }
3.52
import java.awt.*; import java.awt.event.*; import javax.swing.*; public class Java_3 { public static void main(String[] args) { JFrame f = new JFrame("计算"); f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); f.add(new MyPanel()); f.setSize(300, 130); //**********Found********** f.setVisible(true); } } class MyPanel extends JPanel { JTextField t1, t2; JButton b1, b2; JLabel l1, l3; MyPanel() { setLayout(new BorderLayout()); t1 = new JTextField(5); t2 = new JTextField(5); l1 = new JLabel("op"); JLabel l2 = new JLabel("="); l3 = new JLabel("00"); JPanel p1 = new JPanel(); p1.add(t1); p1.add(l1); p1.add(t2); p1.add(l2); p1.add(l3); JPanel p2 = new JPanel(); b1 = new JButton("加"); b2 = new JButton("减"); p2.add(b1); p2.add(b2); add(p1, BorderLayout.CENTER); //**********Found********** add(p2, BorderLayout.SOUTH); //**********Found********** b1.addActionListener(new BListener() ); //**********Found********** b2.addActionListener(new BListener() ); } //**********Found********** private class BListener implements ActionListener { public void actionPerformed(ActionEvent e) { int a = 0, b = 0; try { //**********Found********** a = Integer.parseInt(t1.getText() ); //**********Found********** b = Integer.parseInt(t2.getText() ); } catch (Exception ee) { } //**********Found********** if (e.getSource() == b1) { l1.setText("+"); l3.setText(String.valueOf(a+b)); } else { l1.setText("-"); l3.setText(String.valueOf(a-b)); } } } }
3.53
import javax.swing.*; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; public class Java_3 { public static void main(String[] args) { JFrame jFrame = new JFrame("测试窗口"); jFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); jFrame.setSize(300,120); //**********Found********** JPanel jPanel = new JPanel(); jFrame.add(jPanel); jPanelDefault(jPanel); //**********Found********** jFrame.setVisible(true); } static JLabel jl = new JLabel("请输入:"); static JTextField jtf = new JTextField(); public static void jPanelDefault(JPanel jPanel){ jPanel.setLayout(null); jl.setBounds(30, 10, 80, 20); jPanel.add(jl); jtf.setColumns(20); jtf.setBounds(120, 10, 100,20); jPanel.add(jtf); JButton jb0 = new JButton("确认"); jb0.setBounds(60, 40, 60, 20); //**********Found********** jb0.addActionListener(new ActionListener() { //**********Found********** public void actionPerformed(ActionEvent e) { //**********Found********** JOptionPane.showMessageDialog(null, "保存成功:" + jtf.getText()); } }); jPanel.add(jb0); JButton jb1 = new JButton("取消"); jb1.setBounds(160, 40, 60, 20); jb1.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { //**********Found********** System.exit(0); } }); jPanel.add(jb1); } }
3.54
import java.io.*; import java.awt.*; import java.awt.event.* ; import javax.swing.*; public class Java_3 implements ActionListener { JTextArea ta1,ta2,ta3; JFrame f ; JLabel label1,label2; JButton bs,br; JPanel jp1,jp2; String ss; public static void main(String [ ]args){ Java_3 t = new Java_3(); t.go(); } void go(){ f = new JFrame("Data Input and Output"); f.setLayout(new GridLayout(2,1)); label1 = new JLabel("姓名: "); ta1 = new JTextArea(1,10); bs = new JButton("保存到文件"); label2 = new JLabel(" 电话: "); ta2 = new JTextArea(1,15); ta3 = new JTextArea(1,25); br = new JButton("读取并确认"); jp1 = new JPanel(); jp2 = new JPanel(); jp1.add(label1); jp1.add(ta1); jp1.add(label2); jp1.add(ta2); jp1.add(bs); //**********Found********** f.add(jp1); jp2.add(ta3); jp2.add(br); f.add(jp2); //**********Found********** bs.addActionListener(this); //**********Found********** br.addActionListener( new ReadFile()); f.pack( ); f.setLocationRelativeTo(null); f.setVisible(true) ; f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); } public void actionPerformed(ActionEvent event){ try{ //**********Found********** FileWriter fw = new FileWriter(out.txt); BufferedWriter bf = new BufferedWriter(fw); ss=ta1.getText(); bf.write(ss,0,ss.length()); bf.newLine(); ss=ta2.getText(); bf.write(ss,0,ss.length()); bf.close(); } catch( Exception e){ } } class ReadFile implements ActionListener{ public void actionPerformed(ActionEvent event){ int i=0; char c; char[] a1= new char[50]; String str ; try{ BufferedReader in = new BufferedReader( new FileReader("out.txt")); //**********Found********** ss=in.readLine(); str = " 姓名:" +ss; str = str+" 电话:"+in.readLine(); in.close(); //**********Found********** ta3.setText(str); } catch( Exception e){ } } } }