2013-02-21 2 views
0

Итак, я пытаюсь закончить программу, которая создает графический интерфейс, который может преобразовать язык ассемблера в машинный код. Для этой программы требуется 7 классов, но у меня возникают проблемы с тем, что программа Assemble816 запускается в actionlistener в ASMEditor, когда нажата кнопка «Собрать». Я всегда получаю сотню различных ошибок, когда я пробую разные вещи. Довольно сильно застрял.Как вызвать класс Java в ActionListener в другом классе

ASMEditor:

public class ASMEditor extends JPanel { 
     private JTextArea editArea; 
     private JButton assembleButton; 
     private JButton clearButton; 
     private Assemble816 asm; 
     private InstructionMemory iMem; 


    public ASMEditor() { 
     super(new BorderLayout()); 
     setBackground(Color.white); 
     Border blackLine = BorderFactory.createLineBorder(Color.black); 
     setBorder(
      BorderFactory.createTitledBorder(blackLine, "Assembler")); 

     editArea = new JTextArea(); 
     assembleButton = new JButton("Assemble"); 
     assembleButton.setBackground(getBackground()); 
     assembleButton.addActionListener(new ActionListener() { 
      public void actionPerformed(ActionEvent evt) { 
       System.out.println(editArea.getText()); 
       try{ 
        Assemble816 asm = new Assemble816(); 
        //this is all I have 

       } 
       catch(Exception ex) 
       { 
        JOptionPane.showMessageDialog(null, ex.getMessage()); 
       } 

      } 
     }); 
     clearButton = new JButton("Clear"); 
     clearButton.setBackground(getBackground()); 
     clearButton.addActionListener(new ActionListener() { 
      public void actionPerformed(ActionEvent evt) { 
       editArea.setText(""); 
      } 
     }); 


     JPanel buttons = new JPanel(new FlowLayout(FlowLayout.LEFT, 5, 5)); 
     buttons.setBackground(Color.white); 
     buttons.add(assembleButton); 
     buttons.add(clearButton); 
     add(buttons, BorderLayout.NORTH); 

     Border blueLine = BorderFactory.createLineBorder(Color.blue); 
     editArea.setBorder(blueLine); 
     editArea.setBackground(new Color(205, 255, 255)); 
     editArea.setFont(new Font("monospaced", Font.PLAIN, 14)); 
     add(editArea, BorderLayout.CENTER); 
    } 

    } 

и Assemble816:

public class Assemble816 { 
    private InstructionMemory im; 

    private static String[] twoOperand = { 
     "add", "adc", "sub", "xor", "and", "or", "lshift", "ashift" 
    }; 

    private static int lookupTwoOp(String op) { 
     int opcode = 0; 
     for(String o : twoOperand) { 
      if (o.equals(op)) { 
       return opcode; 
      } 
      opcode++; 
     } 
     return -1; 
    } 

    private static String[] oneOperand = { 
     "inc", "dec", "asr", "lsl" 
    }; 

    private static int lookupOneOp(String op) { 
     int opcode = 0; 
     for(String o : oneOperand) { 
      if (o.equals(op)) { 
       return opcode; 
      } 
      opcode++; 
     } 
     return -1; 
    } 

    private static String[] skip = { 
     "skipeq", "skipne", "skipge", "skiplt" 
    }; 

    private static int lookupSkip(String op) { 
     int opcode = 0; 
     for(String o : skip) { 
      if (o.equals(op)) { 
       return opcode; 
      } 
      opcode++; 
     } 
     return -1; 
    } 

    private static String[] wordConstant = { 
     "ldc", "ldd", "std" 
    }; 

    private static int lookupConstant(String op) { 
     int opcode = 0; 
     for(String o : wordConstant) { 
      if (o.equals(op)) { 
       return opcode; 
      } 
      opcode++; 
     } 
     return -1; 
    } 

    public Assemble816(final InstructionMemory im){ 
     this.im = im; 
    } 

    private static void parseTwoArgs(Scanner sc, String [] words ) 
    throws SyntaxError 
    { 
     String rest = sc.nextLine(); 
     String[] ws = rest.split(","); 
     if (ws.length != 2) { 
      throw new SyntaxError("Missing words"); 
     } 
     words[0] = ws[0].trim(); 
     words[1] = ws[1].trim(); 
    } 

    private static int parseRegister(String reg) throws SyntaxError { 
     if (reg.equals("r0")) { 
      return 0; 
     } 
     else if (reg.equals("r1")) { 
      return 1; 
     } 
     else if (reg.equals("r2")) { 
      return 2; 
     } 
     else if (reg.equals("r3")) { 
      return 3; 
     } 
     else { 
      throw new SyntaxError("Not a register: " + reg); 
     } 
    } 

    private static int parseInteger(String i) throws SyntaxError { 
     String ii = i; 
     try { 
      int sign = 1; 
      if (i.charAt(0) == '-') { 
       i = i.substring(1); 
       sign = -1; 
      } 
      int radix = 10; 
      if (i.startsWith("0x")) { 
       radix = 16; 
       i = i.substring(2); 
      } 
      else if (i.charAt(0) == '0') { 
       radix = 8; 
      } 
      return Integer.parseInt(i, radix) * sign; 
     } 
     catch(NumberFormatException ex) { 
      throw new SyntaxError("Not a number: " + ii); 
     } 
    } 

    private static String stripComments(String line) { 
     int split = line.indexOf(';'); 
     if (split == -1) { 
      return line; 
     } 
     else { 
      return line.substring(0, split); 
     } 
    } 

    private void printIM(int address, int length) { 
     int dataPerLine = 0; 
     for (int a = address; a < (address+length); a++) { 
      if (dataPerLine == 0) { 
       System.out.printf("%04x", a&0xffff); 
       dataPerLine = 16; 
      } 
      System.out.printf(" %02x", im.fetch(a) & 0xff); 
      dataPerLine--; 
      if (dataPerLine == 0) { 
       System.out.println(); 
      } 
     } 
     if (dataPerLine != 0) { 
      System.out.println(); 
     } 
    } 

    // added for project, not part of assignment 
    public void assemble(File f) throws IOException, SyntaxError { 
     byte[] buf = new byte[(int) f.length()]; 
     FileInputStream fis = new FileInputStream(f); 
     fis.read(buf); 
     fis.close(); 
     assemble(new String(buf)); 
    } 

    /** 
    * Assemble the file, f. 
    */ 
    public void assemble(String str) throws SyntaxError { 
     int currentPC = 0; 
     int opcode = 0; 
     String[] args = new String[2]; 

     Scanner sc = new Scanner(str); 
     while(sc.hasNextLine()) { 
      Scanner parse = new Scanner(stripComments(sc.nextLine())); 
      if (!parse.hasNext()) continue; // skip empty line 
      String cmd = parse.next(); 
      if (cmd.equals(".org")) { 
       if (!parse.hasNext()) { 
        throw new SyntaxError(".org excepting integer"); 
       } 
       currentPC = parseInteger(parse.next()); 
      } 
      else if (cmd.equals(".dump")) { 
       parseTwoArgs(parse, args); 
       int start = parseInteger(args[0]); 
       int length = parseInteger(args[1]); 
       printIM(start, length); 
      } 
      else if ((opcode=lookupConstant(cmd)) != -1) { 
       parseTwoArgs(parse, args); 
       int reg = parseRegister(args[0]); 
       int k = parseInteger(args[1]); 
       im.set(currentPC, (opcode<<2) | reg); 
       currentPC++; 
       im.set(currentPC, (k >> 8) & 0xff); 
       currentPC++; 
       im.set(currentPC, (k >> 0) & 0xff); 
       currentPC++; 
      } 
      else if ((opcode=lookupTwoOp(cmd)) != -1) { 
       parseTwoArgs(parse, args); 
       int dst = parseRegister(args[0]); 
       int src = parseRegister(args[1]); 
       im.set(currentPC, 0x80 | (opcode<<4) | dst << 2 | src); 
       currentPC++; 
      } 
      else if (cmd.equals("br")) { 
       if (!parse.hasNext()) { 
        throw new SyntaxError("br excepting integer"); 
       } 
       int branch = parseInteger(parse.next()); 
       im.set(currentPC, 0x40 | (branch & 0x3f)); 
       currentPC++; 
      } 
      else if ((opcode=lookupOneOp(cmd)) != -1) { 
       if (!parse.hasNext()) { 
        throw new SyntaxError(cmd + " excepting register"); 
       } 
       int ds = parseRegister(parse.next()); 
       im.set(currentPC, 0x20 | (opcode<<2) | ds); 
       currentPC++; 
      } 
      else if ((opcode=lookupSkip(cmd)) != -1) { 
       if (!parse.hasNext()) { 
        throw new SyntaxError(cmd + " excepting register"); 
       } 
       int ds = parseRegister(parse.next()); 
       im.set(currentPC, 0x30 | (opcode<<2) | ds); 
       currentPC++; 
      } 
      else if (cmd.equals("ld")) { 
       parseTwoArgs(parse, args); 
       int index = parseRegister(args[0]); 
       if (index != 0 && index != 1) { 
        throw new SyntaxError("index register must be r0 or r1"); 
       } 
       int ds = parseRegister(args[1]); 
       im.set(currentPC, 0x10 | (0<<3) | (index<<2) | ds); 
       currentPC++; 
      } 
      else if (cmd.equals("st")) { 
       parseTwoArgs(parse, args); 
       int index = parseRegister(args[0]); 
       if (index != 0 && index != 1) { 
        throw new SyntaxError("index register must be r0 or r1"); 
       } 
       int ds = parseRegister(args[1]); 
       im.set(currentPC, 0x10 | (1<<3) | (index<<2) | ds); 
       currentPC++; 
      } 
      else if (cmd.equals("jl")) { 
       if (!parse.hasNext()) { 
        throw new SyntaxError("jl excepting register"); 
       } 
       int link = parseRegister(parse.next()); 
       im.set(currentPC, 0x0c | link); 
       currentPC++; 
      } 
      else { 
       throw new SyntaxError("unknown instruction: " + cmd); 
      } 
     } 
     sc.close(); 
    } 

    /** 
    * main - accepts the name of the file to assemble on the command line. 
    */ 
    public static void main(String[] args) { 
     if (args.length != 1) { 
      System.out.println("usage: java Assemble816 file"); 
      return; 
     } 
     try { 
      InstructionMemory im = new InstructionMemory(); 
      Assemble816 asm = new Assemble816(im); 
      asm.assemble(new File(args[0])); 
     } 
     catch(IOException ex) { 
      System.out.println("io: " + ex.getMessage()); 
     } 
     catch(SyntaxError ex) { 
      System.out.println("syntax: " + ex.getMessage()); 
     } 
    } 
} 
+1

Так почему же» Вы вызываете asm.assemble (editArea.getText()); ? – MadProgrammer

+0

Я пробовал это, но я все еще получаю ошибку: Исключение в потоке «AWT-EventQueue-0» java.lang.Error: Неразрешенная проблема компиляции: Конструктор Assemble816() не определен. Не знаете, как исправить это. –

+0

Для гибкого вызова GUI в actionPerformed: 'SwingUtilities.invokeLater (новый Runnable() {...});'. –

ответ

2

Assemble816 имеет только один конструктор, который требует один параметр InstructionMemory

assembleButton.addActionListener(new ActionListener() { 
    public void actionPerformed(ActionEvent evt) { 
     System.out.println(editArea.getText()); 
     try{ 
      InstructionMemory im = new InstructionMemory(); 
      Assemble816 asm = new Assemble816(im); 
      asm.assemble(editArea.getText()); 
     } catch(Exception ex) { 
      // I'd probably dump the stack trace here as well, 
      // seen as you're not logging it anywhere 
      JOptionPane.showMessageDialog(null, ex.getMessage()); 
     } 

    } 
}); 
+0

Спасибо! На самом деле работает –

0

Это на 10,00 футов, но не попытаться/поймать блокирующий заявление? Я думаю, что лучший способ - создать поток Runnable и запустить Assemble816 в неблокирующем потоке. В противном случае ASMEditor будет висеть во время работы Assemble816.

Кроме того, только инициализация программы не вызовет основное автоматически .. поэтому поток может быть полезным для вызова вашей сборки методы внутри класса супер редактора ..

Threads

0

Вы сделали два экземпляра из Assemble816 требуется только один.

В main() вы сделали экземпляр с аргументом.

Вы должны передать этот экземпляр в качестве аргумента вашему классу ASMEditor, модифицируя конструктор.

public class ASMEditor extends JPanel { 
    private JTextArea editArea; 
    private JButton assembleButton; 
    private JButton clearButton; 
    private Assemble816 asm; 
    private InstructionMemory iMem; 

    public ASMEditor(Assemble816 a) { 
     super(new BorderLayout()); 
     asm = a; 
     ... 

Нет необходимости дважды конкретизирует, поэтому событийная код становится:

assembleButton.addActionListener(new ActionListener() { 
    public void actionPerformed(ActionEvent evt) { 
     try{ 
     asm.assemble(editArea.getText()); 
     ... 
Смежные вопросы