import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Vector;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import javax.swing.BorderFactory;
import javax.swing.DefaultListModel;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.JTextArea;
import javax.swing.JScrollPane;
import javax.swing.border.*;
import java.awt.Toolkit;
public class RegExpRun extends JFrame {
private static final long serialVersionUID = 6819222900970457455L;
private JPanel mainPanel = new JPanel();
private JButton runButton = new JButton();
private JLabel label = new JLabel();
private JTextField field = new JTextField();
private JTextArea area = new JTextArea();
private DefaultListModel rightModel = new DefaultListModel();
private JList rightList = new JList(rightModel);
private JScrollPane scroll = null;
public RegExpRun(String title) {
Toolkit kit = Toolkit.getDefaultToolkit();
Dimension screenSize = kit.getScreenSize();
int screenHeight = screenSize.height;
int screenWidth = screenSize.width;
//置于屏幕中央
setSize(screenWidth/2, screenHeight/2);
setLocation(screenWidth/4, screenHeight/4);
setTitle("Overcome Regular Expression");
setPreferredSize(new Dimension(600, 400));
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
initComponent();
addData();
pack();
setVisible(true);
}
private static void createAndShowGUI() {
new RegExpRun("GridBagLayoutFrameDemo");
}
private void initComponent() {
label.setText("Regex");
runButton.setText("実行");
area.setBorder(BorderFactory.createTitledBorder("Source"));
scroll = new JScrollPane(area);
scroll.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
scroll.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED);
rightList.setPreferredSize(new Dimension(150, 150));
rightList.setBorder(BorderFactory.createTitledBorder("Matches"));
// Border redline = BorderFactory.createLineBorder(Color.RED);
TitledBorder title = BorderFactory.createTitledBorder("ようこそ");
title.setTitleJustification(TitledBorder.TOP);
mainPanel.setBorder(BorderFactory.createTitledBorder(title));
mainPanel.setLayout(new GridBagLayout());
GridBagConstraints c = new GridBagConstraints();
c.gridx = 0;
c.gridy = 0;
c.gridwidth = 1;
c.gridheight = 1;
c.fill = GridBagConstraints.HORIZONTAL;
c.weightx = 0;
c.weighty = 0;
mainPanel.add(label, c);
c.gridx++;
c.weightx = 3;
mainPanel.add(field, c);
c.gridx++;
c.weightx = 0;
c.gridwidth = 1;
c.gridheight = 1;
mainPanel.add(runButton, c);
c.gridx = 0;
c.gridy = 1;
c.weightx = 1;
c.weighty = 1;
c.gridwidth = 2;
c.gridheight = 2;
c.fill = GridBagConstraints.BOTH;
mainPanel.add(scroll, c);
c.gridx = 3;
c.gridy = 1;
c.gridwidth = 1;
c.gridheight = 2;
c.weightx = 1;
c.weighty = 1;
c.fill = GridBagConstraints.BOTH;
mainPanel.add(rightList, c);
this.getContentPane().add(mainPanel);
}
private void addData() {
runButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
runRegularExpression();
}
});
}
private void runRegularExpression()
{
Vector<String> v = ReExp.getResult(field.getText(),
area.getText());
((DefaultListModel)rightList.getModel()).clear();
for (int i = 0; i < v.size(); i++)
{
((DefaultListModel)rightList.getModel()).addElement(v.elementAt(i));
}
}
public static void main(String[] args) {
javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGUI();
}
});
}
}
class ReExp
{
public ReExp()
{
}
static public Vector<String> getResult(String reg, String text)
{
Pattern p = Pattern.compile(reg);
if (p == null)
{
JOptionPane.showMessageDialog(null, "Error! Error!");
return null;
}
Matcher m = p.matcher(text);
Vector<String> v = new Vector<String>();
while (m.find())
{
v.add(m.group());
}
return v;
}
}
Regular Expressiton のテストツッル。
説明:
ボタン実行をクリックする後で、Sourceの内容のプロセスする結果はMatchesには表示します。
chick the 実行 button, the processed result of Source border shows in Matches border.
单击 実行 按钮后,source的内容显示在Matches中。
标签:java,import,private,Regular,swing,new,Expression,javax From: https://blog.51cto.com/u_15929756/5988622