Προς το περιεχόμενο

Compare String and act


Chemical

Προτεινόμενες αναρτήσεις

Δημοσ.

Γεια σας μια ερώτηση εχω,

έχω φτιάξει 2 text fields kai ενα textarea

θέλω να τοποθετώ 2 λεξεις στα 2 text fields να τα συγκρίνω και να εμφανίζει στο textarea όποια γράμματα δεν είναι κοινά μεταξύ τους.

Σημειώστε ότι μέχρι τωρα το πρόγραμμα μπορεί να κάνει copy ta 2 text fields kai na ta paste sto text area.

Ευχαριστώ.

Δημοσ.

Ένας τρόπος:

 

>
String s1 = textField1.getText();
String s2 = textField2.getText();
for (int i=0; i<s1.length(); i++){
   String s = s1.charAt(i)+"";
   if (s2.indexOf(s)==-1)
       textArea.append(s);
}

Δημοσ.
Δεν ξέρω τι θες ακριβώς αλλά πρόσεξε αν υπάρχουν ίδια γράμματα στην ίδια λέξη τι θα γίνει ?

ΟΚ got it!

Τώρα παίζει το εξής problem,to myJButton2 που αντιστοιχει στο κομματι κωδικα που μου έδωσε ο Dikemoy,δεν καταφέρνω να το τοποθετήσω κάτω από το allo koumpi to "myJButton" που αντιστοιχει στο CopyApplication&PasteApplication,θα παρατηρήσετε οτι εφαρμόζω τον ίδιο τροπο με αυτον της διάταξης των myJTextFields,2& myJTextArea και δεν δουλεύει...

...where is the problem man...?

σας δίνω τον κώδικα:

>import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

public class MyWindow extends JFrame {//ok
   /**
    * Constructor for objects of class MyWindow
    */
   public MyWindow() {//ok
       // Set window title in titlebar
       super("This is my window");//go to your class you belong and fetch "This is my Window"
       
       // Set window size and position
       Toolkit myToolkit = getToolkit();//ok default fash
       Dimension screenSize = myToolkit.getScreenSize();//ok default fash
       setBounds(screenSize.width/4, screenSize.height/4, //ok default fash
           screenSize.width/2, screenSize.height/2);//ok default fash

       // Set the default close operation
       setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//ok
       
       // Initialize window elements
       initElements();//initializing Elements of window
   }
   
   private void initElements() {//ok
       // Create the MenuBar
       menuBar = new JMenuBar();//ok
       
       // Create MenuBar menus
       fileMenu = new JMenu("File");//ok
       editMenu = new JMenu("Edit");//ok
       
       // Create the MenuItem objects
       exitItem = new JMenuItem("Exit     Alt + F4");//ok
       exitItem.addActionListener(new ExitApplication());//ok
       copyItem = new JMenuItem("Copy     Ctrl + C");//ok
       copyItem.addActionListener(new CopyApplication());//ok
       pasteItem = new JMenuItem("Paste     Ctrl + V");//ok
       pasteItem.addActionListener(new PasteApplication());//ok
       // Add the MenuItem objects to the Menu object
       fileMenu.add(exitItem);  //add the exit item to the fileMenu
       editMenu.add(copyItem);//add the copy item to the "edit" submenu ok?
       editMenu.add(pasteItem);//add the pasteitem to the "edit "submenu ok?
      // Add the menus to the MenuBar
       menuBar.add(fileMenu);  //adding the "File" submenu to the menubar ok?
       menuBar.add(editMenu);  //adding the "Edit" submenu to the nenubar ok?
       
       // Add the MenuBar to the JFrame
       setJMenuBar(menuBar);  //have already created the menouBar and pass as "parameter" to JMenuBar via command "set" ok?
       
       // Set the layout of the contentPane
       
       
       borderLayout = new BorderLayout();//doing it as always ok!
       
       
       getContentPane().setLayout(borderLayout);
       
       // Create the JButton object
       myJButton = new MyButton("Copy/Paste");//ok
       myJButton.addActionListener(new CopyApplication());
       myJButton.addActionListener(new PasteApplication());
       Dimension myJButtonDimension = new Dimension(180, 20);//ok
       myJButton.setPreferredSize(myJButtonDimension);//ok pernv megethos button to orisma poy exv orisei alraedy
       
       //create the MyButton2 object
       myJButton2 = new MyButton("String and act");
       myJButton2.addActionListener(new CompareApplication());
       Dimension myJButton2Dimension = new Dimension(180, 20);
       myJButton2.setPreferredSize(myJButton2Dimension);
           
               // Create the JTextField object
       
       myJTextField = new JTextField("First half");//ok
       Dimension myJTextFieldDimension = new Dimension(280, 20);//ok
       myJTextField.setPreferredSize(myJTextFieldDimension);//ok orisma megethoys 
       myJTextField2 = new JTextField("Second half");//ok same as above
       Dimension myJTextField2Dimension = new Dimension(280, 20);//ok creating  3rd textfield
       myJTextField2.setPreferredSize(myJTextField2Dimension);//ok edw oi diastaseis toy 3rd textfield tha paroyn orisma afto poy tha dilwsw parakatw..
       // Create the JTextArea object
       myJTextArea = new JTextArea("My Text Area");
       Dimension myJTextAreaDimension = new Dimension(280, 120);
       myJTextArea.setPreferredSize(myJTextAreaDimension);
       
       // Create the JPanel objects
       left = new JPanel();//ok
       right = new JPanel();//ok
       
       // Add myJTextField and myJTextArea to the left JPanel
       left.add(myJTextField);
       left.add(myJTextField2);
       left.add(myJTextArea);
       // Add myJButton to the right JPanel
       right.add(myJButton);
       right.add(myJButton2);
               
       // Add both JPanels to the contentPane using the BorderLayout features
       getContentPane().add(left, BorderLayout.CENTER);
       getContentPane().add(right, BorderLayout.EAST);
               
       // Show the window on screen
       setVisible(true);//ok
   }
   
   public static void main(String[] args) {
       newWindow = new MyWindow();
   }
   
   class MyButton extends JButton implements ActionListener {  //ulopoiei h class MyButton to interface ActionListener kai klhronomei thn JButton
       public MyButton(String buttonText) {  //the word "copy" will be inserted via the parameters of String buttonText
           super(buttonText);  //super : goto class you belong a.k.a JButton and fetch the desired text
           addActionListener(this);  //JButton therefore use of command add - use of "this" : uses this constructor
       }
  abstract  class MyButton2 extends JButton implements ActionListener {
       public MyButton2(String buttonText){
           super(buttonText);
           addActionListener(this);
       }
   }
       public void actionPerformed(ActionEvent e) {
       
   }
}
  class ExitApplication implements ActionListener {  //the class must perform (all) the methods of the interface it implements
       public void actionPerformed(ActionEvent e) {  // e entered by user in textfield
           newWindow.dispose();  //dispose of the current window 
           System.exit(0);  //telos rohs program - exit status:0 (or 1 smtimes)-> identifier for the OS - 0: "all ok"
       }
   }
  class CopyApplication implements ActionListener {  //the class must perform (all) the methods of the interface it implements
       public void actionPerformed(ActionEvent e) {  // e entered by user in textfield
           temp = myJTextArea.getText() + "\n" + myJTextField.getText() + " " +myJTextField2.getText() +"\n" ; //(edo einai olo to juice!!!)
       
       }
   }
  
  class PasteApplication implements ActionListener {  //the class must perform (all) the methods of the interface it implements
       public void actionPerformed(ActionEvent e) {  // e entered by user in textfield
           myJTextArea.setText(temp); //(edo einai olo to juice!!!)
       
       }
   } 
  class CompareApplication implements ActionListener{
      public void actionPerformed(ActionEvent e) {
          String s1 = myJTextField.getText();
          String s2 = myJTextField2.getText();
            for (int i=0; i<s1.length(); i++){
              String s = s1.charAt(i)+"";
            if (s2.indexOf(s)==-1)
              myJTextArea.append(s);
}
          
        
          
      }
  } 
   private static JFrame newWindow;
   // Declare Window elements
   private JMenuBar menuBar;
   private JMenu fileMenu, editMenu;
   private JMenuItem exitItem,copyItem,pasteItem;
   
   
   
   private BorderLayout borderLayout;
   
   private JPanel left, right;
 
   private MyButton  myJButton,myJButton2;
   private JTextField myJTextField,myJTextField2;
   private JTextArea myJTextArea;
   String temp; 
   
   String s1;
   String s2;
   int a,b,c;
}

  • 2 εβδομάδες αργότερα...
Δημοσ.

οκ done with it

Χρησιμοποίησα Box και ολα οκ.

>Box myBox = Box.createVerticalBox();
       
       
// Add myJTextField and myJTextArea to the left JPanel
       myBox.add(myJTextField);
       myBox.add(myJTextField2);
       myBox.add(myJTextArea);
       left.add(myBox);
       
// Add myJButton to the right JPanel
       myBox.add(myJButton);
       myBox.add(myJButton2);
       right.add(myBox);

Αρχειοθετημένο

Αυτό το θέμα έχει αρχειοθετηθεί και είναι κλειστό για περαιτέρω απαντήσεις.

  • Δημιουργία νέου...