目录
import java.text.DecimalFormat; import javax.swing.*; import java.awt.*; import java.awt.event.*; //*********Found******** public class Java_3 extends JFrame implements ActionListener { private JTextField input1, input2, output; private int number1, number2; private double result; // 初始化 public Java_3() { //*********Found******** super( "示范异常" ); Container c = getContentPane(); c.setLayout( new GridLayout( 3, 2 ) ); c.add( new JLabel( "输入分子", SwingConstants.RIGHT ) ); input1 = new JTextField( 10 ); c.add( input1 ); c.add( new JLabel( "输入分母和回车", SwingConstants.RIGHT ) ); input2 = new JTextField( 10 ); c.add( input2 ); input2.addActionListener( this ); c.add( new JLabel( "计算结果", SwingConstants.RIGHT ) ); output = new JTextField(); c.add( output ); setSize( 425, 100 ); show(); } //处理 GUI 事件 public void actionPerformed( ActionEvent e ) { DecimalFormat precision3 = new DecimalFormat( "0.000" ); output.setText( "" ); // 空的JTextField输出 //*********Found******** try{ number1 = Integer.parseInt( input1.getText() ); number2 = Integer.parseInt( input2.getText() ); result = quotient( number1, number2 ); //*********Found******** output.setText(precision3.format(result)); } catch ( NumberFormatException nfe ) { JOptionPane.showMessageDialog( this, "你必须输入两个整数", "非法数字格式", JOptionPane.ERROR_MESSAGE ); } catch ( Exception dbze ) { //*********Found******** JOptionPane.showMessageDialog( this, "除法异常", "除数为零", JOptionPane.ERROR_MESSAGE ); } } // 定义求商的方法,如遇除数为零时,能抛出异常。 public double quotient( int numerator, int denominator ) throws Exception { if ( denominator == 0 ) throw new Exception(); return ( double ) numerator / denominator; } public static void main( String args[] ) { Java_3 app = new Java_3(); app.addWindowListener( new WindowAdapter() { public void windowClosing( WindowEvent e ) { e.getWindow().dispose(); System.exit( 0 ); } } ); } } /* JOptionPane类的常用静态方法如下: showInputDialog() showConfirmDialog() showMessageDialog() showOptionDialog() */
import java.awt.*; import java.awt.font.*; import java.awt.geom.*; import javax.swing.*; public class Java_3 { public static void main(String[] args) { FontFrame frame = new FontFrame(); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setVisible(true); } } //*********Found******** class FontFrame extends JFrame { public FontFrame() { setTitle("沁园春.雪"); setSize(DEFAULT_WIDTH, DEFAULT_HEIGHT); FontPanel panel = new FontPanel(); Container contentPane = getContentPane(); //*********Found******** contentPane.add(panel); } public static final int DEFAULT_WIDTH = 300; public static final int DEFAULT_HEIGHT = 200; } //*********Found******** class FontPanel extends JPanel { public void paintComponent(Graphics g) { super.paintComponent(g); Graphics2D g2 = (Graphics2D)g; String message = "数风流人物,还看今朝!"; Font f = new Font("隶书", Font.BOLD, 24); g2.setFont(f); FontRenderContext context = g2.getFontRenderContext(); Rectangle2D bounds = f.getStringBounds(message, context); double x = (getWidth() - bounds.getWidth()) / 2; double y = (getHeight() - bounds.getHeight()) / 2; double ascent = -bounds.getY(); double baseY = y + ascent; g2.setPaint(Color.RED); //*********Found******** g2.drawString(message, (int)x, (int)(baseY)); } }
public class Java_3{ public static void main(String[] args) { String text = "Beijing, the Capital City, is the political," + "cultural and diplomatic centre of China. It has" + "become a modern international cosmopolitan city" + "with more than 11 million people. The Capital" + "International Airport, 23.5 km from the city centre," + "is China's largest and most advanced airport."; int vowels = 0 ; //*********Found********* int textLength= text.length(); for(int i = 0; i < textLength; i++) { //*********Found********* char ch = Character.toLowerCase(text.charAt(i)); if(ch == 'a' || ch == 'e' || ch == 'i' || ch == 'o' || ch == 'u') { //*********Found********* vowels++; } } System.out.println("The text contained vowels: " + vowels + "\n" ); } }
import java.awt.*; import javax.swing.*; public class Java_3{ public static void main(String[] args){ BeijingFrame frame = new BeijingFrame(); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.show(); } } //*********Found********* class BeijingFrame extends JFrame{ public BeijingFrame(){ setTitle("Welcome to Beijing"); Container contentPane = getContentPane(); BeijingPanel panel = new BeijingPanel(); contentPane.add(panel); pack(); } } class BeijingPanel extends JPanel{ public BeijingPanel(){ //*********Found********* setLayout(new BorderLayout() ); ImageIcon icon = new ImageIcon("tiantan.jpg"); //*********Found********* jLC = new JLabel( icon ); add(jLC, BorderLayout.CENTER); lpanel = new JPanel(); jLS = new JLabel("The Temple of Heaven"); lpanel.add(jLS); add(lpanel, BorderLayout.SOUTH); } private JLabel jLC; private JLabel jLS; private JPanel panel; private JPanel lpanel; }
//*********Found********** import javax.swing.JFrame; import java.awt.*; public class Java_3{ static final int WIDTH=300; static final int HEIGHT=200; public static void main(String[] args){ //*********Found********** JFrame jf=new JFrame(); jf.setSize(WIDTH,HEIGHT); jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //*********Found********** jf.setTitle("股票分析系统"); Toolkit kit=Toolkit.getDefaultToolkit(); Dimension screenSize=kit.getScreenSize(); int width=screenSize.width; int height=screenSize.height; int x=(width-WIDTH)/2; int y=(height-HEIGHT)/2; jf.setLocation (x,y); //*********Found********** jf.setVisible(true); } }
import java.io.*; public class Java_3{ public static int data[]={32,18,41,23,2,56,36,67,59,20}; public static void main(String args[]){ int i; //*********Found********** int index=data.length; System.out.println("排序前:"); for(i=0;i=j;i--){ if(data[i] 3.7
import java.awt.*; import java.awt.event.*; import javax.swing.*; public class Java_3{ public static void main(String[] args){ MulticastFrame frame = new MulticastFrame(); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.show(); } } class MulticastFrame extends JFrame{ public MulticastFrame(){ setTitle("MulticastTest"); setSize(WIDTH, HEIGHT); MulticastPanel panel = new MulticastPanel(); Container contentPane = getContentPane(); //*********Found********** contentPane.add(panel); } public static final int WIDTH = 300; public static final int HEIGHT = 200; } class MulticastPanel extends JPanel{ public MulticastPanel(){ JButton newButton = new JButton("New"); add(newButton); ActionListener newListener = new ActionListener(){ public void actionPerformed(ActionEvent event){ makeNewFrame(); } }; newButton.addActionListener(newListener); closeAllButton = new JButton("Close all"); add(closeAllButton); } private void makeNewFrame(){ final BlankFrame frame = new BlankFrame(); frame.show(); ActionListener closeAllListener = new ActionListener(){ public void actionPerformed(ActionEvent event){ //*********Found********** frame.dispose(); //使窗口隐藏或消除 } }; //*********Found********** closeAllButton.addActionListener(closeAllListener); } private JButton closeAllButton; } class BlankFrame extends JFrame{ public BlankFrame(){ //*********Found********** counter++ ; setTitle("Frame " + counter); setSize(WIDTH, HEIGHT); setLocation(SPACING * counter, SPACING * counter); } public static final int WIDTH = 200; public static final int HEIGHT = 150; public static final int SPACING = 30; private static int counter = 0; } 3.8
import javax.swing.*; import java.awt.event.*; import java.io.*; import java.awt.*; //*********Found********** public class Java_3 implements ActionListener{ JFrame f; JTextArea ta; JFileChooser fc; Container c; File myFile; public static void main(String args[]){ Java_3 demo=new Java_3(); demo.go(); } void go(){ JFrame f=new JFrame("File Chooser Demo"); JButton b=new JButton("Open file"); ta=new JTextArea("Where is your file path?",10,30); //*********Found********** b.addActionListener(this); c=f.getContentPane(); f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); f.getContentPane().add("South",b); f.getContentPane().add("Center",ta); f.setSize(300,300); f.setVisible(true); } public void actionPerformed(ActionEvent e){ fc=new JFileChooser(); //*********Found********** int selected=fc.showOpenDialog(c); if (selected==JFileChooser.APPROVE_OPTION){ myFile=fc.getSelectedFile(); //*********Found********** ta.setText("You have selected file: "+myFile.getName()); } } }3.9
import javax.swing.*; import java.awt.*; import java.awt.event.*; //*********Found********** public class Java_3 extends WindowAdapter implements ActionListener { private JFrame frame; private JTextField name; private JPasswordField pass; private JLabel nameLabel; private JLabel passLabel; private JPanel textPanel; private JPanel labelPanel; private JButton button; private JTextArea textArea; public void initGUI() { frame=new JFrame("Frame with Dialog"); frame.addWindowListener(this); button=new JButton("JDialog"); //*********Found********** button.addActionListener(this); textArea=new JTextArea("",3,10); frame.getContentPane().add(textArea,BorderLayout.CENTER); frame.getContentPane().add(button,BorderLayout.NORTH); frame.setSize(400,300); frame.setVisible(true); } public void actionPerformed(ActionEvent e) { final JDialog dia=new JDialog(frame,"login information"); JButton ok=new JButton("ok"); ok.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { textArea.setText(""); textArea.append("name:"+name.getText()+"\r\n"); textArea.append("passWord:"+new String(pass.getPassword())+"\r\n"); //*********Found********** dia.setVisible(false); //隐藏对话框 } }); name=new JTextField("",10); pass=new JPasswordField("",10); pass.setEchoChar('*'); textPanel=new JPanel(); textPanel.setLayout(new GridLayout(2,1,10,10)); textPanel.add(name); textPanel.add(pass); nameLabel=new JLabel("name"); passLabel=new JLabel("passWord"); labelPanel=new JPanel(); labelPanel.setLayout(new GridLayout(2,1,20,20)); labelPanel.add(nameLabel); labelPanel.add(passLabel); dia.getContentPane().add(labelPanel,BorderLayout.WEST); dia.getContentPane().add(textPanel,BorderLayout.CENTER); dia.getContentPane().add(ok,BorderLayout.SOUTH); dia.setSize(200,130); dia.setVisible(true); } public void windowClosing(WindowEvent event) { frame.setVisible(false); System.exit(0); } public static void main(String args[]) { Java_3 example=new Java_3(); //*********Found********** example.initGUI(); } }3.10
import java.awt.*; import java.awt.event.*; //*********Found********** import javax.swing.*; public class Java_3 { public static void main(String[ ] args) { JFrame frame = new JFrame("Demo"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //*********Found********** frame.getContentPane().add(new Change()); frame.pack(); frame.setVisible(true); } } class Change extends JPanel { int count = 200; JLabel l1; JButton b1, b2; public Change( ) { setPreferredSize(new Dimension(280, 60)); l1 = new JLabel("200"); b1 = new JButton("增大"); b2 = new JButton("减小"); add(l1); //*********Found********** add(b1); //*********Found********** add(b2); b1.addActionListener( new BListener( ) ); b2.addActionListener( new BListener( ) ); } //*********Found********** private class BListener implements ActionListener { //*********Found********** public void actionPerformed(ActionEvent e) { if (e.getSource( ) == b1) { count++; } else { count--; } l1.setText("" + count); } } } 3.11
import java.awt.*; import java.awt.event.*; //*********Found********** import javax.swing.*; //*********Found********** public class Java_3 extends JPanel{ private int counter = 0; private JButton closeAllButton; public Java_3() { JButton newButton = new JButton("New"); //*********Found********** add(newButton); newButton.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent evt){ CloseFrame f = new CloseFrame(); counter++; f.setTitle("窗体 " + counter); f.setSize(200, 150); f.setLocation(30 * counter, 30 * counter); //*********Found********** f.setVisible(true); closeAllButton.addActionListener(f); } }); closeAllButton = new JButton("Close all"); add(closeAllButton); } public static void main(String[ ] args) { JFrame frame = new JFrame(); frame.setTitle("多窗体测试"); frame.setSize(300, 200); frame.addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent e) { System.exit(0); } }); Container contentPane = frame.getContentPane(); contentPane.add(new Java_3()); frame.setVisible(true) ; } } //*********Found********** class CloseFrame extends JFrame implements ActionListener { public void actionPerformed(ActionEvent evt) { setVisible(false); } } 3.12
import java.io.*; import java.awt.event.* ; import javax.swing.*; public class Java_3 implements ActionListener{ public static void main(String args[]){ Java_3 t = new Java_3(); //*********Found********** JFrame f = new JFrame("Test"); JButton b = new JButton("复制文件"); b.setSize(100,40); b.addActionListener(t); f.setSize(400,400); //*********Found********** f.getContentPane().add(b); f.pack(); f.setVisible(true) ; f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); } public void actionPerformed(ActionEvent event){ try{ //*********Found********** FileInputStream in=new FileInputStream("a.txt"); //*********Found********** FileOutputStream out=new FileOutputStream("b.txt"); int c; while ((c = in.read()) != -1) out.write(c); in.close(); out.close(); } catch( Exception e){ } } }3.13
//*********Found********** import javax.swing.*; import java.awt.event.*; import java.io.*; import java.awt.*; //*********Found********** public class Java_3 implements ActionListener { JFrame f; JPanel p; JColorChooser cc; Container c; Color myColor; JMenuBar mb; JMenu m1; JMenuItem mi1; public static void main(String args[]) { Java_3 demo = new Java_3(); demo.go(); } void go() { JFrame f = new JFrame("File Chooser Demo"); mb = new JMenuBar(); f.setJMenuBar(mb); m1 = new JMenu("Edit"); mb.add(m1); mi1 = new JMenuItem("Choose Color"); m1.add(mi1); mi1.addActionListener(this); //*********Found********** c =f .getContentPane(); f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); p = new JPanel(); myColor = Color.red; p.setBackground(myColor); c.add("Center", p); f.setSize(300, 300); //*********Found********** f.setVisible(true); } public void actionPerformed(ActionEvent e) { //*********Found********** cc = new JColorChooser(); myColor = cc.showDialog(c, "Choose window background color", Color.white); p.setBackground(myColor); } }3.14
import java.awt.*; import java.awt.event.*; import javax.swing.*; public class Java_3 { public static void main(String[ ] args) { JFrame frame = new JFrame("Demo"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //*********Found********** frame.getContentPane().add(new Change()); frame.pack(); //*********Found********** frame.setVisible(true); } } class Change extends JPanel{ int count = 200; JLabel l1; JButton b1, b2; public Change() { setPreferredSize(new Dimension(280, 60)); l1 = new JLabel("200"); b1 = new JButton("增大"); b2 = new JButton("减小"); add(l1); add(b1); add(b2); b1.addActionListener(new BListener1()); //*********Found********** b2.addActionListener(new BListener2()); } private class BListener1 implements ActionListener { public void actionPerformed(ActionEvent e) { count++; l1.setText("" + count); } } private class BListener2 implements ActionListener { public void actionPerformed(ActionEvent e) { //*********Found********** count --; l1.setText("" + count); } } } 3.15
import javax.swing.*; import java.awt.event.*; import java.io.*; import java.awt.*; //*********Found********** public class Java_3 implements ActionListener{ JFrame f; JPanel p; JColorChooser cc; Container c; Color myColor; JMenuBar mb; JMenu m1; JMenuItem mi1; public static void main(String args[]){ Java_3 demo=new Java_3(); demo.go(); } void go(){ JFrame f=new JFrame("File Chooser Demo"); mb=new JMenuBar(); f.setJMenuBar(mb); //*********Found********** m1=new JMenu("Edit"); mb.add(m1); mi1=new JMenuItem("Choose Color"); m1.add(mi1); //*********Found********** mi1.addActionListener(this); c=f.getContentPane(); f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); p=new JPanel(); myColor=Color.red; p.setBackground(myColor); c.add("Center",p); f.setSize(300,300); f.setVisible(true); } public void actionPerformed(ActionEvent e){ cc=new JColorChooser(); //*********Found********** myColor=cc.showDialog(c,"Choose window background color",Color.white); p.setBackground(myColor); } }3.16
import javax.swing.*; //*********Found******** import java.awt.event.*; import java.io.*; import java.awt.*; //*********Found******** public class Java_3 implements ActionListener{ JFrame f; JTextArea ta; //*********Found******** JFileChooser fc; Container c; File myFile; public static void main(String args[]){ Java_3 demo = new Java_3(); demo.go(); } void go(){ f = new JFrame("File Chooser Demo"); //*********Found******** JButton b = new JButton("Open file"); ta = new JTextArea("Where is your file path?",10,30); //*********Found******** b.addActionListener(this); c = f.getContentPane(); f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //*********Found******** f.getContentPane().add("South", b); f.getContentPane().add("Center",ta); f.setSize(300,300); f.setVisible(true); } public void actionPerformed(ActionEvent e){ fc = new JFileChooser(); int selected = fc.showOpenDialog(c); if (selected==JFileChooser.APPROVE_OPTION){ myFile = fc.getSelectedFile(); ta.setText("You have selected file: "+ myFile.getName()); } } } 3.17
//*********Found******** public class Java_3 extends Thread{ static RegistrationAgent agent; static boolean timetoquit=false; public static void main(String[] args){ agent = new RegistrationAgent(); Thread[] t= new Thread[3]; for (int i=0; i<3; i++){ t[i] = new Java_3(); //*********Found******** t[i].start(); } } public void run( ){ //*********Found******** while (!timetoquit){ boolean r = agent.reg(); if (!r) timetoquit = true; try{ Thread.sleep(2); }catch(Exception e){} } } } class RegistrationAgent { private int quota = 0; public boolean reg(){ synchronized(this){ if( quota < 10){ //*********Found******** quota++; System.out.print(Thread.currentThread().getName()); System.out.println( " Registered one student, and total " + quota +" students registered."); return true; } else //*********Found******** return false; } } } 3.18
import java.awt.*; import java.awt.event.*; import java.io.*; import javax.imageio.*; import javax.swing.*; public class Java_3 extends JFrame { private JLabel label; private JFileChooser fileChooser; private ImagePanel panel; public Java_3() { setTitle("图片浏览器"); setSize(500, 400); fileChooser = new JFileChooser(); fileChooser.setCurrentDirectory(new File("."));//设置默认路径为当前目录 JMenuBar menuBar = new JMenuBar(); setJMenuBar(menuBar); JMenu menu = new JMenu("文件"); menuBar.add(menu); JMenuItem openItem = new JMenuItem("打开图片"); menu.add(openItem); panel = new ImagePanel(); add(panel); //*********Found******** openItem.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent event){ int result = fileChooser.showOpenDialog(null); if(result==JFileChooser.APPROVE_OPTION){ String name = fileChooser.getSelectedFile().getPath(); //*********Found******** panel.setImage(name); panel.repaint(); } } }); JMenuItem exitItem = new JMenuItem("退出图片"); menu.add(exitItem); exitItem.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent event){ System.exit(0); } }); } public static void main(String[] args) { //*********Found******** Java_3 frame = new Java_3 (); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //*********Found******** frame.setVisible(true); } } //*********Found******** class ImagePanel extends JPanel { private Image image; private int showWidth; private int showHeight; public void setImage(String fileName) { try { image = ImageIO.read(new File(fileName)); } catch (IOException e) { e.printStackTrace(); } } public void paintComponent(Graphics g) { super.paintComponent(g); if (image == null) return; int imageWidth = image.getWidth(this); int imageHeight = image.getHeight(this); int width = getWidth(); int height = getHeight(); if(imageWidth>width){ this.showWidth = width; }else{ this.showWidth = imageWidth; } if(imageHeight>height){ this.showHeight = height; }else{ this.showHeight = imageHeight; } g.drawImage(image, 0, 0, showWidth, showHeight, null, null); } } 3.19
import java.io.*; import java.lang.Thread; class MyThread extends Thread{ public int x = 0; //*********Found******** public void run(){ System.out.println(++x); } } //*********Found******** class RThread implements Runnable{ private int x = 0; public void run(){ System.out.println(++x); } } public class Java_3 { public static void main(String[] args) throws Exception{ for(int i=0;i<5;i++){ Thread t = new MyThread(); //*********Found******** t.start(); } Thread.sleep(1000); System.out.println(); //*********Found******** RThread r = new RThread(); //*********Found******** for(int i=0;i<5;i++){ Thread t = new Thread(r); t.start(); } } }3.20
//打印无符号整数位 import java.awt.*; import java.awt.event.*; import javax.swing.*; public class Java_3 extends JFrame { public Java_3(){ super( "打印无符号整数位" ); Container c = getContentPane(); c.setLayout( new FlowLayout() ); c.add( new JLabel( "请输入整数: " ) ); final JTextField output = new JTextField( 33 ); JTextField input = new JTextField( 10 ); input.addActionListener( new ActionListener() { //*********Found******** public void actionPerformed( ActionEvent e ){ int val = Integer.parseInt( e.getActionCommand() ); //*********Found******** output.setText( getBits( val ) ); } } ); c.add( input ); c.add( new JLabel( "该数的二进制位表示是" ) ); output.setEditable( false ); //*********Found******** c.add( output ); setSize( 720, 70 ); setVisible(true); } private String getBits( int value ){ int displayMask = 1 << 31; StringBuffer buf = new StringBuffer( 35 ); for ( int c = 1; c <= 32; c++ ) { buf.append( ( value & displayMask ) == 0 ? '0' : '1' ); value <<= 1; if ( c % 8 == 0 ) buf.append( ' ' ); } return buf.toString(); } public static void main( String args[] ){ Java_3 app = new Java_3(); app.addWindowListener( new WindowAdapter() { //*********Found******** public void windowClosing( WindowEvent e ){ System.exit( 0 ); } } ); } }3.21
import java.io.*; public class Java_3 { public static void main(String[] args) { Java_3 exceptionExample = new Java_3(); //*********Found********** try { FileInputStream fi = new FileInputStream("C:" + "\\" + "abc.txt"); } //*********Found********** catch(FileNotFoundException ex) { //*********Found********** System.out.println(ex.getMessage()+ "请确认文件路径及文件名是否正确!"); } } } 3.22
import java.awt.*; import javax.swing.*; //*********Found********** public class Java_3 extends JApplet { //*********Found********** public void init() { Container contentPane = getContentPane(); JLabel label = new JLabel("One World One Dream",SwingConstants.CENTER); label.setFont(new Font("Arial", Font.BOLD, DEFAULT_SIZE)); //*********Found********** contentPane.add(label); } private static final int DEFAULT_SIZE = 24; } 3.23
import java.awt.*; import java.awt.event.*; import java.io.*; import javax.swing.*; public class Java_3 { public static void main(String[] args) { JFrame frame = new ImageViewerFrame(); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //*********Found********** frame.setVisible(true); } } class ImageViewerFrame extends JFrame { private JLabel label; private JLabel labelT; private JFileChooser chooser; private JComboBox faceCombo; private static final int DEFAULT_SIZE = 24; public static final int DEFAULT_WIDTH = 570; public static final int DEFAULT_HEIGHT = 400; public ImageViewerFrame() { setTitle("ImageViewer"); setSize(DEFAULT_WIDTH, DEFAULT_HEIGHT); label = new JLabel(); Container contentPane = getContentPane(); contentPane.add(label,BorderLayout.CENTER); chooser = new JFileChooser(); chooser.setCurrentDirectory(new File(".")); JMenuBar menuBar = new JMenuBar(); setJMenuBar(menuBar); //*********Found********** JMenu menu = new JMenu("File"); menuBar.add(menu); JMenuItem openItem = new JMenuItem("Open"); //*********Found********** menu.add(openItem); openItem.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent evt) { int r = chooser.showOpenDialog(null); if(r == JFileChooser.APPROVE_OPTION) { //*********Found********** String name = chooser.getSelectedFile().getPath(); label.setIcon(new ImageIcon(name)); } } }); labelT = new JLabel("红军不怕远征难"); labelT.setFont(new Font("隶书", Font.PLAIN, DEFAULT_SIZE)); contentPane.add(labelT, BorderLayout.NORTH ); faceCombo = new JComboBox(); faceCombo.setEditable(true); faceCombo.addItem("隶书"); faceCombo.addItem("华文新魏"); faceCombo.addItem("华文行楷"); faceCombo.addItem("华文隶书"); faceCombo.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent event) { //*********Found********** labelT.setFont(new Font((String)faceCombo.getSelectedItem(), Font.PLAIN, DEFAULT_SIZE)); } }); JPanel comboPanel = new JPanel(); comboPanel.add(faceCombo); contentPane.add(comboPanel, BorderLayout.SOUTH); } } 3.24
import java.awt.event.*; import java.awt.*; import java.awt.font.*; import java.awt.geom.*; import javax.swing.*; public class Java_3 { public static void main(String[] args) { FontFrame frame = new FontFrame(); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setVisible(true); } } //*********Found********** class FontFrame extends JFrame { public FontFrame() { setTitle("北京 2008"); setSize(DEFAULT_WIDTH, DEFAULT_HEIGHT); FontPanel panel = new FontPanel(); Container contentPane = getContentPane(); //*********Found********** contentPane.add(panel); } public static final int DEFAULT_WIDTH = 400; public static final int DEFAULT_HEIGHT = 250; } class FontPanel extends JPanel { public FontPanel() { JButton yellowButton = new JButton("Yellow"); JButton blueButton = new JButton("Blue"); JButton redButton = new JButton("Green"); add(yellowButton); add(blueButton); add(redButton); ColorAction yellowAction = new ColorAction(Color.YELLOW); ColorAction blueAction = new ColorAction(Color.BLUE); ColorAction greenAction = new ColorAction(Color.GREEN); yellowButton.addActionListener(yellowAction); blueButton.addActionListener(blueAction); redButton.addActionListener(greenAction); } public void paintComponent(Graphics g) { super.paintComponent(g); Graphics2D g2 = (Graphics2D)g; String message = "同一个世界,同一个梦想!"; Font f = new Font("隶书", Font.BOLD, 27); g2.setFont(f); FontRenderContext context = g2.getFontRenderContext(); Rectangle2D bounds = f.getStringBounds(message, context); double x = (getWidth() - bounds.getWidth()) / 2; double y = (getHeight() - bounds.getHeight()) / 2; double ascent = -bounds.getY(); double baseY = y + ascent; g2.setPaint(Color.RED); g2.drawString (message, (int)x, (int)(baseY)); } //*********Found********** private class ColorAction implements ActionListener { public ColorAction(Color c) { BackgroundColor = c; } //*********Found********** public void actionPerformed(ActionEvent event) { setBackground(BackgroundColor); } private Color BackgroundColor; } } 3.25
import javax.swing.*; import java.awt.event.*; import java.io.*; import java.awt.*; public class Java_3 implements ActionListener { private JFrame frame; private JButton button; private JButton saveButton; private JTextArea textArea; private JFileChooser dia; private JPanel buttonPanel; public void initGUI() { //*********Found********** frame=new JFrame("file chooser"); button=new JButton("open file"); button.setActionCommand("open"); //*********Found********** button.addActionListener(this); saveButton=new JButton("save file"); saveButton.setActionCommand("save"); //*********Found********** saveButton.addActionListener(this); textArea=new JTextArea("",10,10); buttonPanel=new JPanel(); dia=new JFileChooser(); frame.addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent e) { System.exit(0); } }); buttonPanel.add(button); buttonPanel.add(saveButton); frame.getContentPane().add(buttonPanel,BorderLayout.NORTH); frame.getContentPane().add(textArea,BorderLayout.CENTER); frame.setSize(300,300); //*********Found********** frame.setVisible(true); } //*********Found********** public void actionPerformed(ActionEvent event) { if(event.getActionCommand().equals("open")) { dia.showOpenDialog( frame ); dia.setVisible(true); File file=dia.getSelectedFile(); String fileName=file.getAbsolutePath(); textArea.append("path of selected file: "+fileName+"\r\n"); } else if(event.getActionCommand().equals("save")) { dia.showSaveDialog(frame); dia.setVisible(true); File file=dia.getSelectedFile(); String fileName=file.getAbsolutePath(); textArea.append("path of saved file: "+fileName+"\r\n"); } } public static void main(String args[]) { Java_3 example=new Java_3(); example.initGUI(); } } 3.26
import javax.swing.*; import java.awt.event.*; import java.awt.*; //*********Found********** public class Java_3 extends MouseAdapter implements ActionListener { //*********Found********** private JPopupMenu pop; private JMenu subPop; private JMenuItem color; private JMenuItem exit; private JMenuItem red; private JMenuItem blue; private JTextArea textArea; private JFrame frame; public void initGUI() { pop=new JPopupMenu(); subPop=new JMenu("color"); //*********Found********** red=new JMenuItem("red"); red.addActionListener(this); blue=new JMenuItem("blue"); blue.addActionListener(this); subPop.add(red); subPop.add(blue); exit=new JMenuItem("exit"); exit.addActionListener(this); pop.add(subPop); pop.add(exit); frame=new JFrame("popup frame"); textArea=new JTextArea("",10,10); textArea.addMouseListener(this); //*********Found********** frame.getContentPane().add(textArea); frame.setSize(300,300); frame.setVisible(true); frame.addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent e) { System.exit(0); } }); } public void actionPerformed(ActionEvent event) { if(event.getSource()==red) { //*********Found********** textArea.setForeground(Color.red); textArea.setText("red menu is selected"); } else if(event.getSource()==blue) { textArea.setForeground(Color.blue); textArea.setText("blue menu is selected"); } else if(event.getSource()==exit) { frame.setVisible(false); System.exit(0); } } public void mousePressed(MouseEvent e) { if(e.getModifiers()==e.BUTTON3_MASK) { pop.show(e.getComponent(),e.getX(),e.getY()); } } public static void main(String args[]) { Java_3 example=new Java_3(); example.initGUI(); } } 3.27
import java.awt.Graphics; import javax.swing.JApplet; //*********Found********** public class Java_3 extends JApplet { //*********Found********** public void paint(Graphics g){ int counter = 1; do { //*********Found********** g.drawOval( 110 - counter * 10, 110 - counter * 10, counter * 20, counter * 20 ); //*********Found********** counter++; } while (counter<=10); } } 3.28
//*********Found********** import javax.swing.*; import java.awt.event.*; import java.awt.*; //*********Found********** public class Java_3 extends MouseAdapter implements ActionListener { //*********Found********** private JPopupMenu pop; private JMenu subPop; private JMenuItem color; private JMenuItem exit; private JMenuItem red; private JMenuItem blue; private JTextArea textArea; private JFrame frame; public void initGUI() { pop=new JPopupMenu(); subPop=new JMenu("color"); red=new JMenuItem("red"); red.addActionListener(this); blue=new JMenuItem("blue"); blue.addActionListener(this); subPop.add(red); subPop.add(blue); exit=new JMenuItem("exit"); exit.addActionListener(this); pop.add(subPop); pop.add(exit); //*********Found********** frame=new JFrame("popupframe"); textArea=new JTextArea("",10,10); textArea.addMouseListener(this); frame.getContentPane().add(textArea); frame.setSize(300,300); frame.setVisible(true); frame.addWindowListener(new WindowAdapter() { //*********Found********** public void windowClosing(WindowEvent e) { System.exit(0); } }); } public void actionPerformed(ActionEvent event) { if(event.getSource()==red) { textArea.setForeground(Color.red); textArea.setText("red menu is selected"); } else if(event.getSource()==blue) { textArea.setForeground(Color.blue); textArea.setText("blue menu is selected"); } else if(event.getSource()==exit) { frame.setVisible(false); System.exit(0); } } public void mousePressed(MouseEvent e) { if(e.getModifiers()==e.BUTTON3_MASK) { pop.show(e.getComponent(),e.getX(),e.getY()); } } public static void main(String args[]) { Java_3 example=new Java_3(); example.initGUI(); } } 3.29
import java.awt.*; import java.awt.event.*; import javax.swing.*; import java.io.*; public class Java_3 { public static void main(String[] args) { ExceptTestFrame frame = new ExceptTestFrame(); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setVisible(true); } } class ExceptTestFrame extends JFrame { public ExceptTestFrame() { setTitle("ExceptTest"); Container contentPane = getContentPane(); ExceptTestPanel panel = new ExceptTestPanel(); //*********Found******** contentPane.add(panel); pack(); } } class ExceptTestPanel extends Box { public ExceptTestPanel() { super(BoxLayout.Y_AXIS); group = new ButtonGroup(); addRadioButton("整数被零除", new ActionListener() { //*********Found******** public void actionPerformed(ActionEvent event) { a[1] = 1 / (a.length - a.length); } }); textField = new JTextField(30); add(textField); } //*********Found******** private void addRadioButton(String s, ActionListener listener) { JRadioButton button = new JRadioButton(s, false) { protected void fireActionPerformed(ActionEvent event) { try { textField.setText("No exception"); super.fireActionPerformed(event); } catch (Exception exception) { //*********Found******** textField.setText(exception.toString()); } } }; button.addActionListener(listener); add(button); group.add(button); } private ButtonGroup group; private JTextField textField; private double[] a = new double[10]; }3.30
import java.awt.*; import javax.swing.*; //*********Found********** public class Java_3 extends JApplet{ //*********Found********** public void init(){ Container contentPane = getContentPane(); JLabel label = new JLabel("Java的诞生是对传统计算模式的挑战!", SwingConstants.CENTER); //*********Found********** contentPane.add(label); } } 3.31
import javax.swing.JOptionPane; public class Java_3 { public static void main( String args[] ){ String firstNumber, //存储第1个输入数据 secondNumber, //存储第2个输入数据 //*********Found******** result; //字符串输出 int number1, //用来比较的第1个int型数据 number2; //用来比较的第2个int型数据 //以字符串格式读输入数据 firstNumber = JOptionPane.showInputDialog( "请输入第1个整数:" ); secondNumber = JOptionPane.showInputDialog( "请输入第2个整数:" ); //将字符串转换为int整数 //*********Found******** number1 = Integer.parseInt( firstNumber ); //*********Found******** number2 = Integer.parseInt( secondNumber ); //用空字符串初始化结果变量 result = ""; if ( number1 == number2 ) result = number1 + " == " + number2; if ( number1 != number2 ) result = number1 + " != " + number2; if ( number1 < number2 ) result = result + "\n" + number1 + " < " + number2; if ( number1 > number2 ) result = result + "\n" + number1 + " > " + number2; if ( number1 <= number2 ) result = result + "\n" + number1 + " <= " + number2; if ( number1 >= number2 ) result = result + "\n" + number1 + " >= " + number2; //显示结果 JOptionPane.showMessageDialog( null, result, "比较结果", JOptionPane.INFORMATION_MESSAGE); //*********Found******** System.exit( 0 ); } } 3.32
import java.awt.Graphics; import javax.swing.*; //*********Found********** public class Java_3 extends JApplet{ double sum; //存和的变量 //*********Found********** public void init(){ String firstNumber, //输入第1个字符串格式的数 secondNumber; //输入第2个字符串格式的数 double number1, //加数 number2; //被加数 //读入第1个输入的数 firstNumber = JOptionPane.showInputDialog( "Enter first floating-point value" ); //读入第2个输入的数 secondNumber = JOptionPane.showInputDialog( "Enter second floating-point value" ); //将字符串数据转换成双字长类型 number1 = Double.parseDouble( firstNumber ); number2 = Double.parseDouble( secondNumber ); //数据相加 sum = number1 + number2; } public void paint( Graphics g ) { //用g.drawString给结果 g.drawRect( 15, 10, 270, 20 ); g.drawString( "数相加之和为:" + sum, 25, 25 ); } } //*********Found********** 3.33
import java.awt.*; import javax.swing.*; //*********Found********** public class Java_3 extends JApplet{ JTextArea outputArea; public void init(){ outputArea = new JTextArea(); //*********Found********** Container c = getContentPane(); //*********Found********** c.add( outputArea ); //计算0至10的阶乘 for ( long i = 0; i <= 10; i++ ) outputArea.append( i + "! = " + factorial( i ) + "\n" ); } //阶乘的递归定义 public long factorial( long number ){ if ( number <= 1 ) return 1; else return number * factorial( number - 1 ); } } 3.34
import java.awt.*; import java.awt.event.*; import javax.swing.*; //*********Found********** public class Java_3 extends JApplet implements ActionListener{ JLabel prompt; JTextField input; public void init(){ Container c = getContentPane(); c.setLayout( new FlowLayout() ); //*********Found********** prompt = new JLabel( "输入球半径: " ); input = new JTextField( 10 ); //*********Found********** input.addActionListener(this); c.add( prompt ); c.add( input ); } public void actionPerformed( ActionEvent e ){ double radius = Double.parseDouble( e.getActionCommand() ); showStatus( "体积 " + sphereVolume( radius ) ); } public double sphereVolume( double radius ){ double volume = ( 4.0 / 3.0 ) * Math.PI * Math.pow( radius, 3 ); return volume; } }3.35
import java.lang.*; import java.util.*; public class Java_3{ public static void main(String[ ]args){ int bound=100; int i=0,j=0,counter=0,k=0; int temp=0; boolean first=true; for(i=1;i3.36
import java.lang.*; import java.util.*; public class Java_3{ public static void main(String[ ]args){ int bound=100; int i=0,j=0,counter=0,k=0; int temp=0; boolean first=true; for(i=1;i3.37
import java.awt.*; import java.awt.event.* ; //*********Found******** import javax.swing.*; //*********Found******** public class Java_3 implements ActionListener{ public static void main(String args[ ]){ Java_3 tb = new Java_3(); //*********Found******** JFrame f = new JFrame("Button Test"); f.setSize(200,100); f.setLayout(new FlowLayout(FlowLayout.CENTER)); JButton b = new JButton("Press the Button!"); /JButton //*********Found******** b.addActionListener(tb); //*********Found******** f.add(b); f.addWindowListener(new WindowAdapter(){ public void windowClosing(WindowEvent e){ System.exit(0); } }); f.setVisible(true) ; } //*********Found******** public void actionPerformed(ActionEvent e){ JFrame fr = new JFrame("An Other"); fr.setBackground(Color.green); fr.add(new JLabel("This frame shows when "+"pressing the button in Button Test")); fr.addWindowListener(new WindowAdapter(){ public void windowClosing(WindowEvent e){ System.exit(0); } }); fr.pack(); fr.setVisible(true) ; } }3.38
import java.io.*; import java.util.*; public class Java_3 { //*********Found********** public static void main (String args[]) throws IOException { FileOutputStream unbufStream = new FileOutputStream("test.one"); BufferedOutputStream bufStream = new BufferedOutputStream( //*********Found********** new FileOutputStream ("test.two")); System.out.println(); System.out.println("这是一个测试缓冲流和非缓冲流速度的程序。"); System.out.println(); //*********Found********** int flag = time(unbufStream)-time(bufStream); if(flag > 0) { System.out.println("测试结果:缓冲流的传输速度快于非缓冲流。"); System.out.println(); } else System.out.println("测试结果:缓冲流的传输速度慢于非缓冲流。"); } //*********Found********** static int time(OutputStream out) throws IOException { //*********Found********** Date then = new Date(); for (int i=0; i<1000; i++) { out.write(1); } //*********Found********** out.close (); return (int)((new Date()).getTime() - then.getTime()); } } 3.39
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正式版"); 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); //**********found********** this.setVisible(true); } public void init() { 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); //**********found********** 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); bu1 = new JButton("登录"); bu1.setBounds(250, 200, 65, 20); bu2 = new JButton("多账号"); bu2.setBounds(25, 200, 75, 20); bu3 = new JButton("设置"); bu3.setBounds(140, 200, 65, 20); bu3.addActionListener(new ActionListener() { //**********found********** public void actionPerformed(ActionEvent e) { if (jc1.isSelected()==true) JOptionPane.showConfirmDialog(null,"确定记住密码吗?"); } }); // 所有组件用容器装载 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); } //**********found********** public static void main (String[] args) { Java_3 qq = new Java_3(); } } 3.40
import javax.swing.*; import java.awt.*; import java.awt.event.*; public class Java_3 { public static void main(String[] args) { JFrame frame = new JFrame("Mouse Demo"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.getContentPane().add(new MousePanel()); frame.pack(); frame.setVisible(true); } } //*********Found********** class MousePanel extends JPanel { private JLabel b; public MousePanel() { b = new JLabel(" 起始状态 "); setLayout(new BorderLayout()); add(b, BorderLayout.SOUTH); addMouseMotionListener(new MouseMotion()); setPreferredSize(new Dimension(300, 200)); } //*********Found********** private class MouseMotion extends MouseMotionAdapter { public void mouseMoved(MouseEvent e) { //*********Found********** b.setText(" 鼠标当前位置: "+ e.getX()+ " , " + e.getY()); } } } 3.41
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); void initFrame(){ Container content = getContentPane(); content.setLayout(new BorderLayout()); JPanel calcPanel = new JPanel(); calcPanel.setLayout(new FlowLayout()); calcPanel.add(new JLabel("角度")); calcPanel.add(xval); //***************************Found********************* calcPanel.add(calcBtn); content.add(calcPanel,"North"); //***************************Found********************* 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(xval.getText())/180*3.14; y = Math.sin(x); //***************************Found********************* String str="sin("+ xval.getText() +"*3.14/180)= "+y+'\n'; //***************************Found********************* result.append(str); } } public static void main(String[] args){ new Java_3(); } }3.42
import java.awt.*; import java.awt.event.*; import javax.swing.*; //**************found***************** public class Java_3 extends Frame implements ItemListener{ Color color = Color.blue; CheckboxGroup cg; Checkbox cb1,cb2,cb3; public Java_3(){ cg = new CheckboxGroup(); setSize(300,300); cb1 = new Checkbox("blue",cg,true); cb2 = new Checkbox("red",cg,true); cb3 = new Checkbox("green",cg,true); cg.setSelectedCheckbox(cb1); add(cb1); add(cb2); add(cb3); cb1.addItemListener(this); cb2.addItemListener(this); //**************found***************** cb3.addItemListener(this); } public void itemStateChanged(ItemEvent e){ if(e.getSource()==cb1) color=Color.blue; if(e.getSource()==cb2) color=Color.red; if(e.getSource()==cb3) color=Color.green; repaint(); } class MyEvent1 extends WindowAdapter { public void windowClosing(WindowEvent e){ System.exit(0); } } class MyEvent2 extends ComponentAdapter { public void componentResized(ComponentEvent e){ repaint(); } } //**************found***************** public void paint(Graphics g) { int w = getWidth(),h=getHeight(); int x0 = w/2,y0 = h/2; g.setColor(color); g.drawLine(x0,0,x0,h); g.drawLine(0,y0,w,y0); double pi = 3.1415926,angle,rou; int r,i,x,y; for(r=10;r<200;r+=20){ for(i=0;i<1023;i++){ angle = i * pi/512; rou = r * Math.sin(2*angle); x = (int)Math.round(rou*Math.cos(angle)); y = (int)Math.round(rou*Math.sin(angle)); g.drawOval(x0+x,y0+y,1,1); } } } public static void main(String[] args){ Java_3 f = new Java_3(); MyEvent1 me1 = f.new MyEvent1(); MyEvent2 me2 = f.new MyEvent2(); //**************found***************** f.setTitle("四叶玫瑰线"); f.setLayout(new FlowLayout()); f.addWindowListener(me1); f.addComponentListener(me2); //**************found***************** f.setVisible(true); } }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 ( i3.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){ } } } }