Προς το περιεχόμενο

Βοήθεια στην Java


24_hours_party

Προτεινόμενες αναρτήσεις

Δημοσ.

Γεια χαρά, φτιάχνουμε για την πτυχιακή μας ενά προγραμματάκι σε java με το οποίο ο χρήστης θα μπορεί να κρατάει σημειώσεις σε ένα ηλεκτρονικό βιβλίο html. Προσπαθώ να φτιάξω την "highlight" λειτουργία αλλά αντιμετωπίζω ένα πρόβλημα. Όταν φορτώνω το αρχείο δεν μπορώ να κάνω highlight ενώ αν του δίνω το κείμενο μέσα από το πρόγραμμα αυτό λειτουργεί κανονικά.

 

Αυτή είναι η κλάση, βέβαια απλά το δοκιμάζω να δω πως λειτουργεί όχι ότι θα τα αφήσω όλα σε αυτή την κλάση.

 

>
import java.io.*;

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.text.*;

import java.awt.*;
import java.awt.event.*;
import java.util.HashMap;

import javax.swing.*;
import javax.swing.text.*;
import javax.swing.event.*;
import javax.swing.undo.*;




public class ViewClass extends JFrame {

 private JFrame frame = new JFrame(); // main window
 private JPanel panel = new JPanel(new BorderLayout()); //panels to aid layout
 private JPanel panel2 = new JPanel(new GridLayout(1, 1));

 private JMenuBar menuBar=new JMenuBar();
 JMenu file = new JMenu("File");
 JMenu help = new JMenu("Help");

 JMenuItem open=new JMenuItem("Open");
 JMenuItem save=new JMenuItem("Save");
 JMenuItem exit=new JMenuItem("Exit");
 JMenuItem manual=new JMenuItem("Manual");
 JMenuItem about=new JMenuItem("About");

 JButton buttonH=new JButton();
 JButton buttonB=new JButton();
 JButton buttonT=new JButton();

 private JToolBar toolBar = new JToolBar();
 private JScrollPane scrollPane = new JScrollPane();
 private Model callingModel;
 private Controller theController;





 private static JEditorPane createEditorPane() {
   JEditorPane editorPane = new JEditorPane();
   editorPane.setEditable(false);
   java.net.URL helpURL = ViewClass.class.getResource("simple.html");
   System.out.println(helpURL);
   if (helpURL != null) {
       try {
           editorPane.setPage(helpURL);
       } catch (IOException e) {
           System.err.println("Attempted to read a bad URL: " + helpURL);
       }
   } else {
       System.err.println("Couldn't find file: mexico_city.html");
   }
   //editorPane.setCaretPosition(0);
   //editorPane.setMargin(new Insets(5,5,5,5)); //Sets the space around our text
//StyledDocument styledDoc = .getStyledDocument();
   return editorPane;
}


public ViewClass(Model m) {
    callingModel =m; //retrieve link to model
    theController = new Controller(callingModel); // create controller


    //set up visual features of the window
    frame.setTitle("Take a note");
    frame.setJMenuBar(menuBar);
    menuBar.add(file);
    menuBar.add(help);
    file.add(open);
    file.add(save);
    file.addSeparator();
    file.add(exit);
    help.add(manual);
    help.add(about);
    file.setMnemonic(KeyEvent.VK_F);
    help.setMnemonic(KeyEvent.VK_H);
    //frame.getContentPane().setLayout(new BorderLayout());

    toolBar.add(buttonH);
    buttonH.setIcon(new ImageIcon("H.gif"));
    buttonH.setToolTipText("Highlight");
    buttonH.setActionCommand("Highlight");
    

    toolBar.add(buttonB);
    buttonB.setIcon(new ImageIcon("B.gif"));
    buttonB.setToolTipText("Bookmark");
    buttonB.setActionCommand("Bookmark");

    toolBar.add(buttonT);
    buttonT.setIcon(new ImageIcon("T.gif"));
    buttonT.setToolTipText("Take a note");
    buttonT.setActionCommand("Noting");

    JEditorPane editorPane = createEditorPane();
    //JEditorPane e = new JEditorPane("text/html", "hfhk shhdf kdsh kshfdsh fh fkshfhs fh fhf dhf hfhf hfd hfdf ");
    scrollPane = new JScrollPane(editorPane);
    
    MyHighlightPainter h = new MyHighlightPainter(Color.yellow);
 Highlighter hilite = editorPane.getHighlighter();
 System.out.println(editorPane.getContentType());
 try
 {
 	System.out.println("I am in");
 	hilite.addHighlight(1,6,h);
 }
 catch(BadLocationException ex)
 {
 	System.out.println(ex.getMessage());
 }


   frame.getContentPane().add(panel);
   frame.getContentPane().add(panel2);
   panel.add(toolBar, BorderLayout.PAGE_START);
   panel.add(scrollPane, BorderLayout.CENTER);

  
  frame.getContentPane().add(panel, BorderLayout.CENTER);
  
  
  exit.addActionListener(theController);
  about.addActionListener(theController);
  buttonH.addActionListener(theController);
  buttonB.addActionListener(theController);
  buttonT.addActionListener(theController);
 

    //ensure the following statement is always last
    frame.pack();
    frame.setSize(1274,970);
    frame.setVisible(true);//and nobody sees it until we do this
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

  }
  
  static class MyHighlightPainter extends DefaultHighlighter.DefaultHighlightPainter
{
public MyHighlightPainter(Color r)
{
super(r);
}
}


}

 

Αυτό δεν λειτουργεί. Αλλά το παρακάτω λειτουργεί.

 

>
import java.io.*;

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.text.*;

import java.awt.*;
import java.awt.event.*;
import java.util.HashMap;

import javax.swing.*;
import javax.swing.text.*;
import javax.swing.event.*;
import javax.swing.undo.*;




public class ViewClass extends JFrame {

 private JFrame frame = new JFrame(); // main window
 private JPanel panel = new JPanel(new BorderLayout()); //panels to aid layout
 private JPanel panel2 = new JPanel(new GridLayout(1, 1));

 private JMenuBar menuBar=new JMenuBar();
 JMenu file = new JMenu("File");
 JMenu help = new JMenu("Help");

 JMenuItem open=new JMenuItem("Open");
 JMenuItem save=new JMenuItem("Save");
 JMenuItem exit=new JMenuItem("Exit");
 JMenuItem manual=new JMenuItem("Manual");
 JMenuItem about=new JMenuItem("About");

 JButton buttonH=new JButton();
 JButton buttonB=new JButton();
 JButton buttonT=new JButton();

 private JToolBar toolBar = new JToolBar();
 private JScrollPane scrollPane = new JScrollPane();
 private Model callingModel;
 private Controller theController;





 private static JEditorPane createEditorPane() {
   JEditorPane editorPane = new JEditorPane();
   editorPane.setEditable(false);
   java.net.URL helpURL = ViewClass.class.getResource("simple.html");
   System.out.println(helpURL);
   if (helpURL != null) {
       try {
           editorPane.setPage(helpURL);
       } catch (IOException e) {
           System.err.println("Attempted to read a bad URL: " + helpURL);
       }
   } else {
       System.err.println("Couldn't find file: mexico_city.html");
   }
   //editorPane.setCaretPosition(0);
   //editorPane.setMargin(new Insets(5,5,5,5)); //Sets the space around our text
//StyledDocument styledDoc = .getStyledDocument();
   return editorPane;
}


public ViewClass(Model m) {
    callingModel =m; //retrieve link to model
    theController = new Controller(callingModel); // create controller


    //set up visual features of the window
    frame.setTitle("Take a note");
    frame.setJMenuBar(menuBar);
    menuBar.add(file);
    menuBar.add(help);
    file.add(open);
    file.add(save);
    file.addSeparator();
    file.add(exit);
    help.add(manual);
    help.add(about);
    file.setMnemonic(KeyEvent.VK_F);
    help.setMnemonic(KeyEvent.VK_H);
    //frame.getContentPane().setLayout(new BorderLayout());

    toolBar.add(buttonH);
    buttonH.setIcon(new ImageIcon("H.gif"));
    buttonH.setToolTipText("Highlight");
    buttonH.setActionCommand("Highlight");
    

    toolBar.add(buttonB);
    buttonB.setIcon(new ImageIcon("B.gif"));
    buttonB.setToolTipText("Bookmark");
    buttonB.setActionCommand("Bookmark");

    toolBar.add(buttonT);
    buttonT.setIcon(new ImageIcon("T.gif"));
    buttonT.setToolTipText("Take a note");
    buttonT.setActionCommand("Noting");

    //JEditorPane editorPane = createEditorPane();
    JEditorPane e = new JEditorPane("text/html", "hfhk shhdf kdsh kshfdsh fh fkshfhs fh fhf dhf hfhf hfd hfdf ");
    scrollPane = new JScrollPane(e);
    
    MyHighlightPainter h = new MyHighlightPainter(Color.yellow);
 Highlighter hilite = e.getHighlighter();
 System.out.println(e.getContentType());
 try
 {
 	System.out.println("I am in");
 	hilite.addHighlight(1,6,h);
 }
 catch(BadLocationException ex)
 {
 	System.out.println(ex.getMessage());
 }


   frame.getContentPane().add(panel);
   frame.getContentPane().add(panel2);
   panel.add(toolBar, BorderLayout.PAGE_START);
   panel.add(scrollPane, BorderLayout.CENTER);

  
  frame.getContentPane().add(panel, BorderLayout.CENTER);
  
  
  exit.addActionListener(theController);
  about.addActionListener(theController);
  buttonH.addActionListener(theController);
  buttonB.addActionListener(theController);
  buttonT.addActionListener(theController);
 

    //ensure the following statement is always last
    frame.pack();
    frame.setSize(1274,970);
    frame.setVisible(true);//and nobody sees it until we do this
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

  }
  
  static class MyHighlightPainter extends DefaultHighlighter.DefaultHighlightPainter
{
public MyHighlightPainter(Color r)
{
super(r);
}
}


}

 

Μπορεί να μου πει κάποιος που οφείλεται αυτό το πρόβλημα?

Αρχειοθετημένο

Αυτό το θέμα έχει αρχειοθετηθεί και είναι κλειστό για περαιτέρω απαντήσεις.

  • Δημιουργία νέου...