Класс Frame
package myLaba;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class Frame1 extends JFrame {
JPanel contentPane;
BorderLayout borderLayout1 = new BorderLayout();
JTextField jTextField1 = new JTextField();
JPanel jPanel1 = new JPanel();
JButton jButton1 = new JButton();
JPanel jPanel2 = new JPanel();
JLabel jLabel1 = new JLabel();
//Construct the frame
public Frame1() {
enableEvents(AWTEvent.WINDOW_EVENT_MASK);
try {
jbInit();
}
catch(Exception e) {
e.printStackTrace();
}
}
//Component initialization
private void jbInit() throws Exception {
contentPane = (JPanel) this.getContentPane();
contentPane.setLayout(borderLayout1);
this.setSize(new Dimension(400, 300));
this.setTitle("Преобразователь дробных чисел");
jButton1.setText("Преобразовать!");
jButton1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(ActionEvent e) {
jButton1_actionPerformed(e);
}
});
jLabel1.setText("Реультат преобразования");
jTextField1.setText("");
contentPane.add(jTextField1, BorderLayout.NORTH);
contentPane.add(jPanel1, BorderLayout.SOUTH);
jPanel1.add(jButton1, null);
contentPane.add(jPanel2, BorderLayout.CENTER);
jPanel2.add(jLabel1, null);
}
//Overridden so we can exit when window is closed
protected void processWindowEvent(WindowEvent e) {
super.processWindowEvent(e);
if (e.getID() == WindowEvent.WINDOW_CLOSING) {
System.exit(0);
}
}
void jButton1_actionPerformed(ActionEvent e) {
try{
Tape tape = new Tape(jTextField1.getText());
Automat automat = new Automat(tape);
automat.process();
jLabel1.setText("Входная строка успешно разпознана number == " + automat.getNumber());
jLabel1.setText("xyxp");
}catch(LexError le){
jLabel1.setText("*** ОШИБКА *** " + le.getMessage());
}
}
}
Класс myApplication
package myLaba;
import java.awt.*;
public class myApplication {
//Construct the application
public myApplication() {
Frame1 frame = new Frame1();
//Validate frames that have preset sizes
frame.validate();
//Center the window
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
Dimension frameSize = frame.getSize();
if (frameSize.height > screenSize.height) {
frameSize.height = screenSize.height;
}
if (frameSize.width > screenSize.width) {
frameSize.width = screenSize.width;
}
frame.setLocation((screenSize.width - frameSize.width) / 2, (screenSize.height - frameSize.height) / 2);
frame.setVisible(true);
}
//Main method
public static void main(String[] args) {
new myApplication();
}
}
Класс Action
package myLaba;
public interface Action {
public boolean process (int symbol);
public State getNextState();
}
Класс State
package myLaba;
import java.util.*;
public class State {
private String name;
private ArrayList actions;
private boolean success = false;
public State(String name) {
actions = new ArrayList();
this.name = name.toUpperCase();
}
public State(String name, boolean success) {
actions = new ArrayList();
this.name = name.toUpperCase();
this.success = success;
}
public void addAction(Action action){
actions.add(action);
}
public State process (int c) throws LexError {
for (int i = 0; i < actions.size(); i++){
Action a = (Action)actions.get(i);
if (a.process(c)){
return a.getNextState();
}
}
throw new LexError ("В состоянии " + name + " обнаружен "
+ (c == -1 ? "конец строки" : "недопустимый символ " + (char)c));
}
public String getName(){
return name;
}
public boolean isSuccess(){
return success;
}
}