Adding Listeners.

This commit is contained in:
Arthur 2021-02-17 10:39:10 +01:00
parent 61e26dc007
commit 80ea9ae5f0
4 changed files with 138 additions and 27 deletions

View File

@ -14,9 +14,8 @@ public class Main {
public static void main(String[] args) { public static void main(String[] args) {
MainFrame frame = new MainFrame(); MainFrame frame = new MainFrame();
frame.pack();
frame.setVisible(true); frame.setVisible(true);
/*
SwingGLCanvas canvas = new SwingGLCanvas(); SwingGLCanvas canvas = new SwingGLCanvas();
final Scanner scanner = new Scanner(System.in); final Scanner scanner = new Scanner(System.in);
@ -42,7 +41,7 @@ public class Main {
Rewrite rewriter = new Rewrite(axiom, parser.parseRules(), nbIterations); Rewrite rewriter = new Rewrite(axiom, parser.parseRules(), nbIterations);
final String word = rewriter.rewrite(); final String word = rewriter.rewrite();
System.out.println(word); System.out.println(word);
scanner.close(); scanner.close();*/
} }

View File

@ -1,47 +1,91 @@
package lsystem.screen; package lsystem.screen;
import lsystem.screen.listener.AxiomFieldListener; import lsystem.screen.listener.ClearListener;
import lsystem.screen.listener.FieldListener;
import javax.swing.*; import javax.swing.*;
import java.awt.*; import java.awt.*;
import javax.swing.JTabbedPane; import javax.swing.JTabbedPane;
public class Tab { public class Tab {
MainFrame frame;
int nbTabs;
int nbRules;
public Tab(MainFrame frame,int nbTabs,int nbRules,JTabbedPane tabs) { public Tab(MainFrame frame,int nbTabs,int nbRules,JTabbedPane tabs) {
this.frame = frame;
this.nbRules = nbRules;
this.nbTabs = nbTabs;
JPanel tab = new JPanel(); JPanel tab = new JPanel();
JTextArea axiomList = new JTextArea(); JTextArea axiomList = textArea("Axiome : \n",nbTabs);
axiomList.setText("Axiome : \n"); JTextArea rulesList = textArea("Règles : \n",nbTabs+10);
axiomList.setEditable(false);
frame.addToTextAreaList(axiomList,nbTabs);
JTextArea rulesList = new JTextArea();
rulesList.setText("Règles : \n");
rulesList.setEditable(false);
frame.addToTextAreaList(rulesList,nbTabs+10);
JButton closeButton = new JButton("x");
JLabel axiome = new JLabel("Axiome");
JLabel rules = new JLabel("Règle "+ nbRules);
JLabel axiome = new JLabel("Axiome :");
JLabel rules = new JLabel("Règle "+ nbRules+" :");
JTextField axiomeField = new JTextField(); JTextField axiomeField = new JTextField();
axiomeField.addKeyListener(new AxiomFieldListener(frame,nbTabs)); axiomeField.addKeyListener(new FieldListener(frame,nbTabs,(byte)0));
axiomeField.setPreferredSize(new Dimension(20,20)); axiomeField.setPreferredSize(new Dimension(120,20));
frame.addToTextFieldList(axiomeField,nbTabs); frame.addToTextFieldList(axiomeField,nbTabs);
JTextField rulesField = new JTextField(); JTextField rulesField = new JTextField();
rulesField.setPreferredSize(new Dimension(20,20)); rulesField.addKeyListener(new FieldListener(frame,nbTabs+10,(byte)1));
rulesField.setPreferredSize(new Dimension(120,20));
frame.addToTextFieldList(rulesField,nbTabs+10);
JButton submitButton = new JButton("Générer");
JButton clearButton = new JButton("Clear");
clearButton.addActionListener(new ClearListener(frame,nbTabs));
JPanel southComponents = subPanel(clearButton,submitButton,null);
GridBagConstraints gc = new GridBagConstraints();
gc.gridx = 0;
gc.gridy = 0;
gc.weightx = 0;
gc.weighty = 0.5;
JPanel eastComponents = subPanel(axiomList,rulesList,gc);
JPanel centerComponents = subPanel(axiomeField,rulesField,gc);
JPanel westComponents = subPanel(axiome,rules,gc);
JPanel aboveComponents = new JPanel();
aboveComponents.add(westComponents);
aboveComponents.add(centerComponents);
aboveComponents.add(eastComponents);
aboveComponents.setLayout(new GridLayout(1,3));
tab.add(aboveComponents);
tab.add(southComponents);
tab.setLayout(new BoxLayout(tab,1));
tab.add(axiome);
tab.add(axiomeField);
tab.add(axiomList);
tab.add(rules);
tab.add(rulesField);
tab.add(rulesList);
tab.setLayout(new GridLayout(2,3));
tabs.addTab("Génération"+String.valueOf(nbTabs), tab); tabs.addTab("Génération"+String.valueOf(nbTabs), tab);
} }
public JTextArea textArea(String texte,int nb){
JTextArea res = new JTextArea();
res.setText(texte);
res.setEditable(false);
res.setPreferredSize(new Dimension(120,100));
frame.addToTextAreaList(res,nb);
return res;
}
public JPanel subPanel(Component a, Component b,GridBagConstraints gc){
JPanel res = new JPanel();
if(gc == null){
res.add(a);
res.add(b);
return res;
}
res.setLayout(new GridBagLayout());
res.add(a,gc);
gc.gridy = 1;
res.add(b,gc);
gc.gridy = 0;
return res;
}
} }

View File

@ -0,0 +1,24 @@
package lsystem.screen.listener;
import lsystem.screen.MainFrame;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class ClearListener implements ActionListener {
MainFrame frame;
int nbTabs;
public ClearListener(MainFrame frame, int nbTabs){
this.frame =frame;
this.nbTabs = nbTabs;
}
@Override
public void actionPerformed(ActionEvent e) {
frame.textAreaList.get(nbTabs).setText("Axiome : \n");
frame.textAreaList.get(nbTabs+10).setText("Règles : \n");
frame.textFieldList.get(nbTabs).setText("");
frame.textFieldList.get(nbTabs+10).setText("");
}
}

View File

@ -0,0 +1,44 @@
package lsystem.screen.listener;
import lsystem.screen.MainFrame;
import javax.swing.*;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.lang.reflect.Field;
public class FieldListener implements KeyListener {
MainFrame frame;
int index;
byte i;
public FieldListener(MainFrame frame, int index, byte i){
this.frame = frame;
this.index = index;
this.i = i;
}
@Override
public void keyTyped(KeyEvent ke) {
if(ke.getKeyCode() != KeyEvent.VK_ENTER)
frame.changeList(String.valueOf(ke.getKeyChar()),i, (JTextArea) frame.textAreaList.get(index));
else{
String str = ";\r\n";
frame.changeList(str,i, (JTextArea) frame.textAreaList.get(index));
frame.textFieldList.get(index).setText(null);
}
}
@Override
public void keyPressed(KeyEvent ke) {
if(ke.getKeyCode() == KeyEvent.VK_ENTER)
frame.textFieldList.get(index).setText(null);
}
@Override
public void keyReleased(KeyEvent e) {
}
}