我是 java swings 的新手。其实我有以下情况:
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
, 由于文本被 "" 包围,它不应该做任何事情。
第二种情况,由于数据没有被“”包围,所以“.” 应以黄色突出显示(如果“.”为单个)。
在第三种情况下,正弦“。” 在另一个“.”之前或之后 它应该保持原样。
有人建议我可以使用秋千来完成,请给我一个关于如何进行的想法。在这里,我想要一个表单,我们可以在其中粘贴此文本和一个名为“清洁”的按钮,一旦它被点击,黄色突出显示应该出现在句点上。还请向我推荐一些关于摇摆的好参考。
我尝试过的如下。这还没有完全完成,因为我不知道如何启动它以及如何进行
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.
}
}
谢谢