2012-09-18 47 views
1

我想使用擺動組件在框架中打開文本文件,最好使用高亮設施。我在第一幀申請文本獲取文本文件的名稱,並希望在第二frame.My代碼來打開文本文件使用擺動組件在框架中打開文本文件


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

public class FirstGUI extends JFrame { 

private JLabel label; 
private JTextField textfield; 
private JButton button; 

public FirstGUI() { 
    setLayout(new FlowLayout()); 

    label = new JLabel("Enter the file path:"); 
    add(label); 

    textfield = new JTextField(); 
    add(textfield); 

    button = new JButton("Open"); 
    add(button); 

    AnyClass ob = new AnyClass(); 
    button.addActionListener(ob); 
} 

public class AnyClass implements ActionListener { 
    public void actionPerformed(ActionEvent obj) { 
     //SecondGUI s =new SecondGUI(); 
     //s.setVisible(true); 
    } 
} 

public static void main(String[] args) { 

    FirstGUI obj= new FirstGUI(); 
    obj.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
    obj.setSize(600,600); 
    obj.setLocation(100,100); 
    obj.setVisible(true); 
} 
} 

我應該使用什麼Swing組件在我的第二幀中打開一個文本文件呢?如果可能,請提供代碼大綱!

回答

2

最簡單的選擇將是一個JTextArea

另一個更好的選擇是JEditorPane

你可以看看this文本組件教程,以更好地理解它們並選擇你所需要的最好的。

1

JTextArea的文本

的FileInputStream myFIS;

objectInputStream myOIS(myFIS);

Data = myOIS.read();

text.setText(Data);

這應該給你一些想法去哪裏。不要忘記將文件輸入流設置爲文件位置,以便知道要打開的文件。然後ObjectInputStream將獲取數據並將信息保存到名爲Data的字段中。然後將textArea設置爲使用數據作爲信息來「設置」要顯示的textArea。

注意:ObjectInputStream不是唯一可用的輸入流。您將不得不使用與您的文件相關的輸入流。

3

上mKorbel和丹斯答案擴展:

那麼你可以使用JTextArea像這樣:

enter image description here

import java.awt.BorderLayout; 
import java.awt.Color; 
import java.lang.reflect.InvocationTargetException; 
import javax.swing.JFrame; 
import javax.swing.JScrollPane; 
import javax.swing.JTextArea; 
import javax.swing.SwingUtilities; 
import javax.swing.text.*; 

public class LineHighlightPainter { 

    String revisedText = "Hello, World! "; 
    String token = "Hello"; 

    public static void main(String args[]) { 
     try { 
      SwingUtilities.invokeAndWait(new Runnable() { 

       @Override 
       public void run() { 
        new LineHighlightPainter().createAndShowGUI(); 
       } 
      }); 
     } catch (InterruptedException | InvocationTargetException ex) { 
      ex.printStackTrace(); 
     } 
    } 

    public void createAndShowGUI() { 
     JFrame frame = new JFrame("LineHighlightPainter demo"); 
     frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 

     JTextArea area = new JTextArea(9, 45); 
     area.setLineWrap(true); 
     area.setWrapStyleWord(true); 
     area.setText(revisedText); 

     // Highlighting part of the text in the instance of JTextArea 
     // based on token. 
     highlight(area, token); 

     frame.getContentPane().add(new JScrollPane(area), BorderLayout.CENTER); 
     frame.pack(); 
     frame.setVisible(true); 
    } 

    // Creates highlights around all occurrences of pattern in textComp 
    public void highlight(JTextComponent textComp, String pattern) { 
     // First remove all old highlights 
     removeHighlights(textComp); 

     try { 
      Highlighter hilite = textComp.getHighlighter(); 
      Document doc = textComp.getDocument(); 
      String text = doc.getText(0, doc.getLength()); 

      int pos = 0; 
      // Search for pattern 
      while ((pos = text.indexOf(pattern, pos)) >= 0) { 
       // Create highlighter using private painter and apply around pattern 
       hilite.addHighlight(pos, pos + pattern.length(), myHighlightPainter); 
       pos += pattern.length(); 
      } 

     } catch (BadLocationException e) { 
      e.printStackTrace(); 
     } 
    } 

    // Removes only our private highlights 
    public void removeHighlights(JTextComponent textComp) { 
     Highlighter hilite = textComp.getHighlighter(); 
     Highlighter.Highlight[] hilites = hilite.getHighlights(); 

     for (int i = 0; i < hilites.length; i++) { 
      if (hilites[i].getPainter() instanceof MyHighlightPainter) { 
       hilite.removeHighlight(hilites[i]); 
      } 
     } 
    } 
    // An instance of the private subclass of the default highlight painter 
    Highlighter.HighlightPainter myHighlightPainter = new MyHighlightPainter(Color.red); 

    // A private subclass of the default highlight painter 
    class MyHighlightPainter 
      extends DefaultHighlighter.DefaultHighlightPainter { 

     public MyHighlightPainter(Color color) { 
      super(color); 
     } 
    } 
} 

或可選擇地使用JTextPane和文字可以高亮顯示:

1)在文檔級別更改任意文本部分的任何樣式屬性,是這樣的:

SimpleAttributeSet sas = new SimpleAttributeSet(); 
StyleConstants.setForeground(sas, Color.YELLOW); 
doc.setCharacterAttributes(start, length, sas, false); 

2)通過對textPane水平熒光筆亮點:

DefaultHighlighter.DefaultHighlightPainter highlightPainter = new DefaultHighlighter.DefaultHighlightPainter(Color.YELLOW); 
textPane.getHighlighter().addHighlight(startPos, endPos,highlightPainter); 

參考文獻: