2013-03-28 33 views
-4

我是java swing的新手。其實我有以下情況:在java swing中需要幫助,創建一個簡單的表格

Case1 : 「the collection was guaranteed.i'm sure it is」 
Case2 : the collection was guaranteed.i'm sure it is 
Case2 : the collection was guaranteed...i'm sure it is 

這些情況下,我想反映使用一個swing window.Which將接受這些情況。

條件:

case1,因爲文本是由「」它不應該做任何事情包圍。

第二種情況,由於數據沒有被「」包圍,「」。應突出顯示(如果「。」是單個)黃色。

第三種情況正弦「。」之前或之後是另一個「。」它應該保持原樣。

有人建議我這可以使用波動來完成,請給我一個關於如何進行的想法。在這裏,我想要一個表格,我們可以粘貼這個文本和一個名爲「清潔」的按鈕,一旦它被擊中,黃色突出顯示應該出現在這段時間。也請給我建議一些很好的參考波動。

我試過的是如下。這是我不知道如何開始,以及如何沒有多少迴旋的經驗,最簡單的方法是使用HTML進行

import java.awt.Color; 
import java.util.Scanner; 
import java.util.logging.Level; 
import java.util.logging.Logger; 
import javax.swing.JOptionPane; 
import javax.swing.event.DocumentListener; 
import javax.swing.text.BadLocationException; 
import javax.swing.text.DefaultHighlighter; 
import javax.swing.text.Highlighter; 

/* 
* To change this template, choose Tools | Templates 
* and open the template in the editor. 
*/ 

/** 

* @author u0138039 
*/ 
public class NewJPanel extends javax.swing.JPanel { 

    /** 
    * Creates new form NewJPanel 
    */ 
    public NewJPanel() { 
     initComponents(); 
     createGUI(); 

    } 

    /** 
    * This method is called from within the constructor to initialize the form. 
    * WARNING: Do NOT modify this code. The content of this method is always 
    * regenerated by the Form Editor. 
    */ 
    @SuppressWarnings("unchecked") 
    // <editor-fold defaultstate="collapsed" desc="Generated Code">       
    private void initComponents() { 

     jFrame1 = new javax.swing.JFrame(); 
     jScrollPane1 = new javax.swing.JScrollPane(); 
     jTextArea1 = new javax.swing.JTextArea(); 
     jButton1 = new javax.swing.JButton(); 
     jScrollPane2 = new javax.swing.JScrollPane(); 
     jTextArea2 = new javax.swing.JTextArea(); 

     javax.swing.GroupLayout jFrame1Layout = new javax.swing.GroupLayout(jFrame1.getContentPane()); 
     jFrame1.getContentPane().setLayout(jFrame1Layout); 
     jFrame1Layout.setHorizontalGroup(
      jFrame1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) 
      .addGap(0, 400, Short.MAX_VALUE) 
     ); 
     jFrame1Layout.setVerticalGroup(
      jFrame1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) 
      .addGap(0, 300, Short.MAX_VALUE) 
     ); 

     jTextArea1.setColumns(20); 
     jTextArea1.setRows(5); 
     jScrollPane1.setViewportView(jTextArea1); 

     jButton1.setText("Clean"); 
     jButton1.addActionListener(new java.awt.event.ActionListener() { 
      public void actionPerformed(java.awt.event.ActionEvent evt) { 
       jButton1ActionPerformed(evt); 
      } 
     }); 

     jTextArea2.setColumns(20); 
     jTextArea2.setRows(5); 
     jScrollPane2.setViewportView(jTextArea2); 

     javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this); 
     this.setLayout(layout); 
     layout.setHorizontalGroup(
      layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) 
      .addComponent(jScrollPane1) 
      .addGroup(layout.createSequentialGroup() 
       .addGap(472, 472, 472) 
       .addComponent(jButton1) 
       .addContainerGap(579, Short.MAX_VALUE)) 
      .addGroup(layout.createSequentialGroup() 
       .addContainerGap() 
       .addComponent(jScrollPane2)) 
     ); 
     layout.setVerticalGroup(
      layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) 
      .addGroup(layout.createSequentialGroup() 
       .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 367, javax.swing.GroupLayout.PREFERRED_SIZE) 
       .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) 
       .addComponent(jButton1) 
       .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) 
       .addComponent(jScrollPane2, javax.swing.GroupLayout.DEFAULT_SIZE, 398, Short.MAX_VALUE) 
       .addContainerGap()) 
     ); 
    }// </editor-fold>       

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {           
     hilit = new DefaultHighlighter(); 
     jTextArea2=jTextArea1.getText(); 
Scanner scan=new Scanner(jTextArea1.getText()); 
    System.out.println("enter some text \n"); 
    String line= scan.nextLine(); 
    if(line.contains("\"")) 
    { 
     System.out.println("Notining to do"); 
     System.exit(0); 
    } 
    int dot1= line.indexOf("."); 
    int dot2=line.lastIndexOf("."); 
    if(dot1==dot2) 
    { 
     jTextArea2.setText(line); 
      try { 
       hilit.addHighlight(dot1,dot1+1, painter); 
      } catch (BadLocationException ex) { 
       Logger.getLogger(NewJPanel.class.getName()).log(Level.SEVERE, null, ex); 
      } 
     jFrame1.setVisible(true); 
    } 
    if(dot1==dot1+dot2 || dot1==dot2-dot1) 
    { 
     JOptionPane.showMessageDialog(jFrame1,"2 dots"); 
    } 
    }           


    // Variables declaration - do not modify      
    private javax.swing.JButton jButton1; 
    private javax.swing.JFrame jFrame1; 
    private javax.swing.JScrollPane jScrollPane1; 
    private javax.swing.JScrollPane jScrollPane2; 
    private javax.swing.JTextArea jTextArea1; 
    private javax.swing.JTextArea jTextArea2; 
    // End of variables declaration     
    private Highlighter hilit=null; 
    private final Highlighter.HighlightPainter painter = new DefaultHighlighter.DefaultHighlightPainter(Color.yellow); 

    private void createGUI() { 
     throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates. 
    } 

} 

感謝

+5

ununderstandable – 2013-03-28 06:59:40

+4

你誤會*擺動*。 – Maroun 2013-03-28 06:59:50

+1

什麼是「波動」?我感到非常傷心和難過,因爲你接受了一項你甚至無法拼寫和理解的技術的家庭作業。 – asgs 2013-03-28 07:00:42

回答

1

沒有完全完成。

使用JTextPane(或JEditorPane)。

textPane.setContentType("text/html"); 

如果代碼設置,確保字符串開頭"<html>"

textPane.setText("<html>「the collection was guaranteed.i'm sure it is」"); 

現在你可以使用HTML標記,着色,如:

"~~~<span style='background-color:#ffff00'>.</span>~~~" 

的文件變化監聽器或按鍵監聽器可以每次重新添加標記。

Document doc = textPane.getDocument(); 
String plainText = doc.getText(0, doc.getLength()); 

對於可以使用的邏輯正則表達式,i.a.與所謂的前瞻。一個單獨的章節。

1

我做了一個簡短的例子。

  • 使用適當的佈局管理按照您的需求
  • 我檢查你的大多數情況下,可能不是所有
  • 我使用了一個Highlighter突出「」

enter image description here

import java.awt.Color; 
    import java.awt.event.ActionEvent; 
    import java.awt.event.ActionListener; 
    import java.util.Scanner; 
    import java.util.logging.Level; 
    import java.util.logging.Logger; 
    import javax.swing.*; 
    import javax.swing.SwingUtilities; 
    import javax.swing.text.BadLocationException; 
    import javax.swing.text.DefaultHighlighter; 
    import javax.swing.text.Highlighter; 




    /** 
    * 
    * @author rohan 
    */ 
    public class TestPeriodHighlight { 
     private JFrame frame; 
     private JPanel panel; 
     private JTextArea jTextArea; 
     private JButton butt; 
     private Highlighter hilit=null; 
     private final Highlighter.HighlightPainter painter = new DefaultHighlighter.DefaultHighlightPainter(Color.yellow); 
    TestPeriodHighlight() throws BadLocationException 
    { 
    createGUI(); 
    process(); 
    } 
    public void process() throws BadLocationException 
    { 
    hilit = new DefaultHighlighter(); 
    jTextArea.setHighlighter(hilit); 
    Scanner scan=new Scanner(System.in); 
     System.out.println("enter some text \n"); 
     String line= scan.nextLine(); 
     if(line.contains("\"")) 
     { 
      System.out.println("Notining to do"); 
      System.exit(0); 
     } 
     int dot1= line.indexOf("."); 
     int dot2=line.lastIndexOf("."); 
     if(dot1==dot2) 
     { 
      jTextArea.setText(line); 
      hilit.addHighlight(dot1,dot1+1, painter); 
      frame.setVisible(true); 
     } 



    } 
    public void createGUI() 
    { 
    butt=new JButton("clear"); 
    frame=new JFrame(); 
    frame.setDefaultCloseOperation(frame.EXIT_ON_CLOSE); 
    panel=new JPanel(); 
    jTextArea=new JTextArea(20,20); 
    panel.add(jTextArea); 
    panel.add(butt); 
    butt.addActionListener(new ActionListener() { 

       public void actionPerformed(ActionEvent ae) 

       { 
       jTextArea.setText(""); 
       } 


       }); 
    frame.add(panel); 
    frame.pack(); 
    frame.setVisible(false); 



    } 

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

       public void run() { 
        try 
        { 
         new TestPeriodHighlight(); 
        } catch (BadLocationException ex) { 
         Logger.getLogger(TestPeriodHighlight.class.getName()).log(Level.SEVERE, null, ex); 
       } 

       } 
       } 
       ); 

     } 

    } 
+0

Hi @JoeyRohan:謝謝你的解決方案。這是行得通的。但在這裏我遇到了一些問題。 1)它只接受1行作爲輸入。我應該做什麼來閱讀整個段落。 2)框架是在輸入文字後創建的,但我想要創建擺動,然後在其中輸入文字。有一次我清楚,它應該突出的東西。真的非常感謝你的上述代碼。 – 2013-03-28 09:08:57

+0

我如何給出多行輸入,並在我創建的框架中輸入 – 2013-03-28 09:15:23

+0

正如我所說的,這只是一個示例。如果要使用GUI創建一個段落,請創建兩個「JTextArea」一個,然後顯示結果.2)第二個是簡單的。我已經完成了'frame.setVisible(false)'使其成爲'frame.setVisible(true)'.3)在JButton butt的'ActionListener'塊中突出顯示部分。 :) – 2013-03-28 09:16:24