-4

私はJavaスイングが初めてです。実際に私は以下のケースを持っています:

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

これらのケースは、スイングウィンドウを使用して反映したい.これらのケースを受け入れる.

条件 :

ではcase1、テキストが "" で囲まれているため、何もする必要はありません。

2 番目のケースでは、データが "" で囲まれていないため、"." (「.」が単一の場合)黄色で強調表示する必要があります。

3 番目のケースでは、"." をサインします。別の「.」が前後にある そのままにしておくべきです。

誰かがスイングを使用してこれを行うことができると私に提案しました。続行する方法について教えてください。ここでは、このテキストと「Clean」という名前のボタンを貼り付けることができるフォームが必要です。クリックすると、ピリオドに黄色のハイライトが表示されます。また、スイングに関する良い参考文献を教えてください。

私が試したことは以下の通りです。開始方法と続行方法を知らなかったので、これは完全には行われていません

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.
    }

}

ありがとう

4

2 に答える 2

1

私は短い例を作りました。

  • 必要に応じて適切な LayoutManager を使用してください
  • 私はあなたのほとんどのケースをチェックしましたが、すべてではないかもしれません
  • Highlighter「.」を強調表示するためにa を使用しました。

ここに画像の説明を入力

    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);
                 }

                }
                }
                );

        }

    }
于 2013-03-28T08:58:11.517 に答える
1

Swing の経験があまりないので、最も簡単な方法は HTML を使用することです。

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());

ロジックには正規表現を使用できます。つまり、いわゆる先読みを使用できます。別の章。

于 2013-03-28T08:29:57.923 に答える