0

First of all, I don't know how to put this straight and correctly, so bear with my lack of valuable information. Anyhow, I've just started learning Java GUI today and it seems pretty amusing and somewhat simple(if it comes to the basics such as creating a window with objects), yet there are obstacles. What I'm trying to do here is make a simple GUI that containts 2 label, 2 text fields and a button. The 2 lables are X and Y, with respective fields to enter a desired text. The button is used to "concatinate" the entries and add them to a different label then show it in a seperate panel. My code is:

public class Fenetre extends JFrame
{
    private JPanel p;
    private JPanel jpanel;

    public JLabel etiq1, etiq2, etiqr;
    public JTextField text1;
    public JTextField text2; 
    public JButton button1;


    public Fenetre()
    {
        super();

        setLayout(new GridLayout());
        this.setSize(300,300);
        this.setVisible(true);
        this.setContentPane(this.getContentPane());
        this.setTitle("X+Y résultat");

        jpanel=(JPanel)this.getContentPane();
        text1=new JTextField();
        text2=new JTextField();
        etiq1=new JLabel("X");
        etiq2=new JLabel("Y");

        jpanel.add(etiq1);
        jpanel.add(etiq2);
        jpanel.add(text1);
        jpanel.add(text2);
        button1=new JButton("X+Y");
        this.add(button1);
        this.pack();
        jpanel.addMouseListener(new MouseAdapter()
        {
            public void mousePressed(MouseEvent e)
            {
                etiqr.setText(text1.getText()+text2.getText());
            }
        });
    }
}

What I've made so far is the window that contains the objects and a mouseListener to concatinate the two texts into a single one. My question is how do I use the mouseListener to show the concatinated text in a different panel?(Supposdly in the same frame?).

Amine
  • 1,396
  • 4
  • 15
  • 32
  • You would use a third JPanel that uses a [CardLayout](https://docs.oracle.com/javase/tutorial/uiswing/layout/card.html). Then add both your JPanels to this JPanel using appropriate constants (see the [CardLayout Tutorial](https://docs.oracle.com/javase/tutorial/uiswing/layout/card.html) for more on this), and then use the CardLayout object to swap views. – Hovercraft Full Of Eels Nov 08 '16 at 23:13
  • @HovercraftFullOfEels is there any way to stick with 2 panels??? – Amine Nov 08 '16 at 23:16
  • You may be confused in what I stated above. You're only displaying **one** JPanel at a time. The CardLayout-using JPanel works behind the scenes but allows for smooth swapping between your two JPanels. That "third" JPanel is never seen. Please *read the tutorial* as it's all laid out for you. – Hovercraft Full Of Eels Nov 08 '16 at 23:24
  • Side note: you might want to add an ActionListener to that JButton if you want it to do something when pushed. – Hovercraft Full Of Eels Nov 08 '16 at 23:25
  • 1
    @HovercraftFullOfEels *"you might want to add an ActionListener to that JButton if you want it to do something when pushed."* Or provide action by keyboard, or an accelerator for the action.. – Andrew Thompson Nov 08 '16 at 23:27

0 Answers0