From 80ea9ae5f07cd758193ecfcc96f1aadada1d054b Mon Sep 17 00:00:00 2001 From: Arthur <78031901+Arthur7770@users.noreply.github.com> Date: Wed, 17 Feb 2021 10:39:10 +0100 Subject: [PATCH] Adding Listeners. --- src/lsystem/Main.java | 5 +- src/lsystem/screen/Tab.java | 92 ++++++++++++++----- .../screen/listener/ClearListener.java | 24 +++++ .../screen/listener/FieldListener.java | 44 +++++++++ 4 files changed, 138 insertions(+), 27 deletions(-) create mode 100644 src/lsystem/screen/listener/ClearListener.java create mode 100644 src/lsystem/screen/listener/FieldListener.java diff --git a/src/lsystem/Main.java b/src/lsystem/Main.java index b0b56d1..1618331 100644 --- a/src/lsystem/Main.java +++ b/src/lsystem/Main.java @@ -14,9 +14,8 @@ public class Main { public static void main(String[] args) { MainFrame frame = new MainFrame(); - frame.pack(); frame.setVisible(true); - + /* SwingGLCanvas canvas = new SwingGLCanvas(); final Scanner scanner = new Scanner(System.in); @@ -42,7 +41,7 @@ public class Main { Rewrite rewriter = new Rewrite(axiom, parser.parseRules(), nbIterations); final String word = rewriter.rewrite(); System.out.println(word); - scanner.close(); + scanner.close();*/ } diff --git a/src/lsystem/screen/Tab.java b/src/lsystem/screen/Tab.java index 1fae1a9..5e9ec16 100644 --- a/src/lsystem/screen/Tab.java +++ b/src/lsystem/screen/Tab.java @@ -1,47 +1,91 @@ package lsystem.screen; -import lsystem.screen.listener.AxiomFieldListener; +import lsystem.screen.listener.ClearListener; +import lsystem.screen.listener.FieldListener; import javax.swing.*; import java.awt.*; import javax.swing.JTabbedPane; public class Tab { + MainFrame frame; + int nbTabs; + int nbRules; public Tab(MainFrame frame,int nbTabs,int nbRules,JTabbedPane tabs) { + this.frame = frame; + this.nbRules = nbRules; + this.nbTabs = nbTabs; + JPanel tab = new JPanel(); - JTextArea axiomList = new JTextArea(); - axiomList.setText("Axiome : \n"); - 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"); + JTextArea axiomList = textArea("Axiome : \n",nbTabs); + JTextArea rulesList = textArea("Règles : \n",nbTabs+10); - 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(); - axiomeField.addKeyListener(new AxiomFieldListener(frame,nbTabs)); - axiomeField.setPreferredSize(new Dimension(20,20)); + axiomeField.addKeyListener(new FieldListener(frame,nbTabs,(byte)0)); + axiomeField.setPreferredSize(new Dimension(120,20)); frame.addToTextFieldList(axiomeField,nbTabs); 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); } + 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; + } } diff --git a/src/lsystem/screen/listener/ClearListener.java b/src/lsystem/screen/listener/ClearListener.java new file mode 100644 index 0000000..e575b5f --- /dev/null +++ b/src/lsystem/screen/listener/ClearListener.java @@ -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(""); + } +} diff --git a/src/lsystem/screen/listener/FieldListener.java b/src/lsystem/screen/listener/FieldListener.java new file mode 100644 index 0000000..e5e032d --- /dev/null +++ b/src/lsystem/screen/listener/FieldListener.java @@ -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) { + + } +}