Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: java

  1. #1
    Junior Member
    Join Date
    May 2011
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Post java

    I am new to this forum and need help. Please can someone help me fix this problem?//URL LINK
    import java.awt.*;
    import javax.swing.*;
    import java.awt.event.*;
    import java.net.*;
    import java.io.*;
    import java.util.*;
    import java.util.regex.*;
     
    public class BLink extends JFrame {
     
    	private JLabel TURL,LNA,LStatus,LRC;
        private JButton Exit,Check;
        private JTextField Input;
        private JScrollPane Jsp,Jsp1,Jsp2;
        private JTextArea List1,List2,List3;
     
        public BLink()
        {
        	super( "BLink" );
     
            Container panel = getContentPane();
            panel.setLayout(null);
     
            TURL = new JLabel("URL: ");
            TURL.setBounds(10,10,40,25);
            panel.add(TURL);
     
            Input = new JTextField();
            Input.setBounds(45,10,165,25);
            panel.add(Input);
     
            Check = new JButton("Start");
            Check.setBounds(230,10,80,25);
            panel.add(Check);
     
            LNA = new JLabel("Network Address");
            LNA.setBounds(15,45,100,25);
            panel.add(LNA);
     
            LStatus = new JLabel("Status:");
            LStatus.setBounds(390,45,100,25);
            panel.add(LStatus);
     
            LRC = new JLabel("Response Code:");
            LRC.setBounds(520,45,100,25);
            panel.add(LRC);
     
            List1 = new JTextArea();
            Jsp = new JScrollPane(List1);
            Jsp.getHorizontalScrollBar();
            Jsp.getVerticalScrollBar();
        	Jsp.setBounds(10,65,350,300);
        	List1.setEditable(false);
        	panel.add(Jsp);
     
        	List2 = new JTextArea();
            Jsp1 = new JScrollPane(List2);
            Jsp1.getHorizontalScrollBar();
            Jsp1.getVerticalScrollBar();
        	Jsp1.setBounds(360,65,150,300);
        	List2.setEditable(false);
        	panel.add(Jsp1);
     
        	List3 = new JTextArea();
            Jsp2 = new JScrollPane(List3);
            Jsp2.getHorizontalScrollBar();
            Jsp2.getVerticalScrollBar();
        	Jsp2.setBounds(510,65,100,300);
        	List3.setEditable(false);
        	panel.add(Jsp2);
     
        	Exit = new JButton("Exit");
            Exit.setBounds(535,370,70,25);
            panel.add(Exit);
     
            ButtonHandler bhandler = new ButtonHandler();
          	Exit.addActionListener(bhandler);
          	Check.addActionListener(bhandler);
     
          	setResizable(false);
            setSize(650,450);
            setVisible(true);
    	}
     
        public static void main( String args[] ){
            BLink application = new BLink();
            application.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE );
        }
     
        private class ButtonHandler implements ActionListener{
    	    public void actionPerformed(ActionEvent event){
    			if(event.getSource() == Exit)
    			{
    				if (JOptionPane.showConfirmDialog(null, "Are You Sure Want To Exit","Confirm", JOptionPane.YES_NO_OPTION)== 0)
    		   		{
    			   		System.exit(0);
    			   	}
    			}
     
    			if(event.getSource() == Check)
    			{
    				List1.setText("");
    				List2.setText("");
    				List3.setText("");
    					String CurrentLine="";
            			String TotalString="";
            			InputStream urlStream;
            		try
            		{
    	        		try
            			{
    	        		String urlString = Input.getText();
            			URL url = new URL(urlString);
            			HttpURLConnection connection = (HttpURLConnection)url.openConnection();
            			connection.connect();
                		urlStream = connection.getInputStream();
                		BufferedReader reader = new BufferedReader(new InputStreamReader(urlStream,"gbk"));
                		while ((CurrentLine = reader.readLine()) != null)
                		{
                			TotalString += CurrentLine+" ";
                		}
            			}
                		catch (MalformedURLException ex)
            			{
    	        				String urlString = "http://" + Input.getText();
            					URL url = new URL(urlString);
            					HttpURLConnection connection = (HttpURLConnection)url.openConnection();
            					connection.connect();
                				urlStream = connection.getInputStream();
                				BufferedReader reader = new BufferedReader(new InputStreamReader(urlStream,"gbk"));
                				while ((CurrentLine = reader.readLine()) != null)
                				{
                					TotalString += CurrentLine+" ";
                				}
            			}
            			catch(UnknownHostException e)
            			{
    	        			JOptionPane.showMessageDialog(null, "Unknown Host");
            			}
                		String content =  TotalString;
                		String sc = "SC.txt";
                		File scfile = new File(sc);
     
                		if(!scfile.exists())
                		{
    	            		List1.append("File does not exist");
    	            		System.exit(0);
                		}
                		else
                		{
    	            		BufferedWriter out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(scfile),"UTF8"));
    	            		out.write(content);
    	            		out.close();
                		}
     
                		String page = loadPage("SC.txt");
            			Pattern pattern = Pattern.compile("<a(?:(?!href).)++href\\s*+=\\s*+[\"'](https?://[^/]+)", Pattern.CASE_INSENSITIVE);
            			Matcher matcher = pattern.matcher(page);
            			while (matcher.find()) {
     
    	        			try
    	        			{
    	        			String urlString2 = matcher.group(1);
                			URL url2 = new URL(urlString2);
                			HttpURLConnection connection2 = (HttpURLConnection)url2.openConnection();
                			connection2.connect();
                			int response = connection2.getResponseCode();
     
                			String msg = connection2.getResponseMessage();
     
    	          	  			List1.append(urlString2 + "\n");
                  				List2.append(msg + "\n");
                  				List3.append(response + "\n");
     
              	  			InputStream is = connection2.getInputStream();
                  			byte[] buffer = new byte [256];
                  			while (is.read (buffer) != -1) {}
                  			is.close();
    						}
            				catch (MalformedURLException ex){}
            				catch (IOException e){}
     
               		 	}
              		}
            		catch (IOException e)
            		{
                		e.printStackTrace();
            		}
    			}
    		}
    	}
     
    	private static String loadPage(String name) {
            StringBuffer output = new StringBuffer();
            try {
                FileReader file = new FileReader(name);
                BufferedReader buff = new BufferedReader(file);
                boolean eof = false;
                while (!eof) {
                    String line = buff.readLine();
                    if (line == null)
                        eof = true;
                    else
                        output.append(line + "\n");
                }
                buff.close();
            } catch (IOException e) {
                System.out.println("Error -- " + e.toString());
            }
            return output.toString();
        }
    }
     
     
    2. import java.awt.*;
    import java.awt.Graphics;
    import java.awt.event.*;
    import javax.swing.*;
    import javax.swing.colorchooser.*;
    import javax.swing.event.*;
     
    public class Editor extends JApplet{
     
     private SwingUtilities Utility;
     private JLabel Title;
     private JTextArea EnterArea;
     private JScrollPane SPEnterArea;
     private JComboBox FontSize1,FontSize2,FontSize3;
     private JButton Clear, TextColourButton, BackgroundColourButton;
     private JMenuBar menubar;
     private JMenu menu1, menu2;
     private JMenuItem menu1item1, menu1item2, menu2item1;
     private String Fo[] = {"Times New Roman","Arial", "Kristen ITC"};
     private String FStyle[] = {"PLAIN","BOLD","ITALIC"};
     private String Size[] = {"10","12","14","16","18","20"}; 
     private String Fon = "Times New Roman";
     private int FontStyle = Font.PLAIN;
     private int Fonts = 14;
     
     
     public void init()
     {
         try
         {
             Utility.invokeAndWait(new Runnable() {
                 public void run() {
                     layouts();
                 }
             });
         } 
         catch (Exception e)
         {
             System.err.println("createGUI didn't successfully complete");
         }
     
     }//End of Init()
     
     public void layouts()
     {
     
      Container appletpanel = getContentPane();
            appletpanel.setLayout(null);
     
            menubar = new JMenuBar();
     
            menu1 = new JMenu();
            menu2 = new JMenu();
     
            menu1item1 = new JMenuItem();
            menu1item2 = new JMenuItem();
     
            menu2item1 = new JMenuItem();
     
            menu1.setText("File");              
            menu1item1.setText("Open");
            menu1item1.addActionListener(new ActionListener()
            {
                public void actionPerformed(ActionEvent evt)
                {
                    menu1item1ActionPerformed(evt);
                }
            }
            );
            menu1.add(menu1item1);
     
            menu1item2.setText("Save");
            menu1item2.addActionListener(new ActionListener()
            {
                public void actionPerformed(ActionEvent evt)
                {
                    menu1item2ActionPerformed(evt);
                }
            }
            );
            menu1.add(menu1item2);
     
            menu2.setText("Help");              
            menu2item1.setText("About");
            menu2item1.addActionListener(new ActionListener()
            {
                public void actionPerformed(ActionEvent evt)
                {
                    menu2item1ActionPerformed(evt);
                }
            }
            );
            menu2.add(menu2item1);
     
            menubar.add(menu1);
            menubar.add(menu2);
            menubar.setBounds(1,1,640,20);
           appletpanel.add(menubar);
     
            Title = new JLabel("Text Editor");
            Title.setBounds(1,20,100,25);
            Title.setFont(new Font("Times New Roman", Font.BOLD, 20));
            appletpanel.add(Title);
     
            FontSize1 = new JComboBox(Fo); 
            FontSize1.setBounds(1,55,140,20);         
            FontSize1.setMaximumRowCount(3);
            FontSize1.addItemListener(new ItemListener()
            {
             public void itemStateChanged(ItemEvent event)
             {
              if(FontSize1.getSelectedIndex() == 0)
              { 
               Fon = "Times New Roman";
               EnterArea.setFont(new Font(Fon, FontStyle , Fonts));
           }
           else if(FontSize1.getSelectedIndex() == 1)
              { 
               Fon = "Arial";
               EnterArea.setFont(new Font(Fon, FontStyle , Fonts));
           }
           else if(FontSize1.getSelectedIndex() == 2)
              { 
               Fon = "Kristen ITC";
               EnterArea.setFont(new Font(Fon, FontStyle , Fonts));
           }
     
           }
       }
       )
            appletpanel.add(FontSize1); 
     
     
            FontSize2 = new JComboBox(FStyle); 
            FontSize2.setBounds(150,55,100,20);         
            FontSize2.setMaximumRowCount(3);
            FontSize2.addItemListener(new ItemListener()
            {
             public void itemStateChanged(ItemEvent event)
             {
              if(FontSize2.getSelectedIndex() == 0)
              { 
               FontStyle = Font.PLAIN;
               EnterArea.setFont(new Font(Fon, FontStyle , Fonts));
           }
              else if(FontSize2.getSelectedIndex() == 1)
              { 
               FontStyle = Font.BOLD;
               EnterArea.setFont(new Font(Fon, FontStyle , Fonts));
           }
           else if(FontSize2.getSelectedIndex() == 2)
              { 
               FontStyle = Font.ITALIC;
               EnterArea.setFont(new Font(Fon, FontStyle , Fonts));
           }
     
     
           }
       }
       );
            appletpanel.add(FontSize2); 
     
     
            FontSize3 = new JComboBox(Size); 
            FontSize3.setBounds(260,55,100,20);         
            FontSize3.setMaximumRowCount(6);
            FontSize3.addItemListener(new ItemListener()
            {
             public void itemStateChanged(ItemEvent event)
             {
              if(FontSize3.getSelectedIndex() == 0)
              { 
               Fonts = 10;
               EnterArea.setFont(new Font(Fon, FontStyle , Fonts));
           }
           else if(FontSize3.getSelectedIndex() == 1)
              { 
               Fonts = 12;
               EnterArea.setFont(new Font(Fon, FontStyle , Fonts));
           }
           else if(FontSize3.getSelectedIndex() == 2)
              { 
               Fonts = 14;
               EnterArea.setFont(new Font(Fon, FontStyle , Fonts));
           }
           else if(FontSize3.getSelectedIndex() == 3)
              { 
               Fonts = 16;
               EnterArea.setFont(new Font(Fon, FontStyle , Fonts));
           }
           else if(FontSize3.getSelectedIndex() == 4)
              { 
               Fonts = 18;
               EnterArea.setFont(new Font(Fon, FontStyle , Fonts));
           }
           else if(FontSize3.getSelectedIndex() == 5)
              { 
               Fonts = 20;
               EnterArea.setFont(new Font(Fon, FontStyle , Fonts));
           }
           }
       }
       );
            appletpanel.add(FontSize3); 
     
     
      EnterArea = new JTextArea();
      SPEnterArea = new JScrollPane(EnterArea);
      SPEnterArea.getHorizontalScrollBar();
            SPEnterArea.getVerticalScrollBar();     
      SPEnterArea.setBounds(1,75,450,380);
      EnterArea.setFont(new Font(Fon, FontStyle , Fonts));
      EnterArea.setLineWrap(true);
      EnterArea.setWrapStyleWord(true);
      appletpanel.add(SPEnterArea);
     
      Clear = new JButton("Clear");
      Clear.setBounds(1,475,70,25);
            appletpanel.add(Clear);
     
            TextColourButton = new JButton("Font Color");
            TextColourButton.setBounds(460,75,100,20);
            appletpanel.add(TextColourButton);
     
            BackgroundColourButton = new JButton("Background Color");
            BackgroundColourButton.setBounds(460,115,100,20);
            appletpanel.add(BackgroundColourButton);
     
            ButtonHandler bhandler = new ButtonHandler();
           Clear.addActionListener(bhandler);
           TextColourButton.addActionListener(bhandler);
           BackgroundColourButton.addActionListener(bhandler);
     
      setSize(640,520);
            setVisible(true);
     
     
     }
     
     
        private void menu1item1ActionPerformed(ActionEvent event) 
        {
         }
     
        private void menu1item2ActionPerformed(ActionEvent event) 
        {
         }
     
        private void menu2item1ActionPerformed(ActionEvent event) 
        {
         JOptionPane.showMessageDialog(null, " Text Editor\nUEL computer school\n”);
     
        }
     
    }
    Last edited by KevinWorkman; May 19th, 2011 at 12:37 PM. Reason: added highlight tags


  2. #2
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,357
    My Mood
    Hungover
    Thanks
    144
    Thanked 626 Times in 537 Posts

    Default Re: java

    That's not really how this works.

    First off, that's way too much code for anybody to wade through. Boil your problem down to an SSCCE, and we'll go from there.
    Secondly, you haven't even told us what the problem is! Do you have a question? Where are you stuck?
    And finally, don't forget the highlight tags next time.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!