Java - JButton + JTextField - źle wyświetla treści

0

Witam, uczę sią Javy i mam problem, bawię się kodami i to co mi nie wychodzi:
Powinno działać tak że jak wduszę Button 1 To wyświetla się Wiadomość w pierwszym polu, a jak Button 2 to w drugim polu a działa tak że jak wdusze Button 1 to pojawia sie wiadomość w polu pierwszym i drugim i odwrotnie. Proszę o pomoc.
Kod programu:

package buttoneventdemo;

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
 
@SuppressWarnings("serial")
public class ButtonEventDemo extends JFrame
{
    private JButton
        b1 = new JButton("Button 1"),
        b2 = new JButton("Button 2");
   
    private JTextField txt = new JTextField(12);

   
    class ButtonListener implements ActionListener
    {
        public void actionPerformed(ActionEvent e)
        {   
            txt.setText(e.getActionCommand()); 
           
            txt1.setText(e.getActionCommand());
        }
    }
   
    private ButtonListener btnlist1 = new ButtonListener();
    private ButtonListener btnlist = new ButtonListener();
    public ButtonEventDemo()
    {
        super("Button Event Demo");
       
        b1.addActionListener(btnlist1);
        b2.addActionListener(btnlist);

       
       
        b1.setActionCommand("Hello!");
        b2.setActionCommand("Goodbye!");
       
       
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setSize(250, 250);
        setVisible(true);
        setLayout(new FlowLayout());
       

        add(b2);
        add(txt);
        add(b1);
        add(txt1);
    }
   
   
   
   
    public static void main(String[] args)
    {
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                new ButtonEventDemo();
            }
        });
    }
} 

Prosze o pomoc najlepiej w postaci poprawionego tekstu. pozdrawiam

0

Przechwytywanie zdarzeń jest opisane np. tutaj http://javastart.pl/grafika_awt_swing/zdarzenia-przyciski/

Można to zrobić np. w ten sposób:

public void actionPerformed(ActionEvent e) {
    Object source = e.getSource();
			 
    if(source == b1) {
        txt.setText(e.getActionCommand());
    }
    if(source == b2) {
        txt1.setText(e.getActionCommand());
    }
0

Na szybko:

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

public class ButtonsEventFrame extends JFrame {

    private JTextField txt1;
    private JTextField txt2;

    public ButtonsEventFrame() {
        setupFrame();
        initializeComponents();
    }

    private void initializeComponents() {
        JButton b1 = new JButton("Button 1");
        JButton b2 = new JButton("Button 2");

        txt1 = new JTextField(12);
        txt2 = new JTextField(12);

        b1.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                clearTextBoxes();
                txt1.setText("Hello!");
            }
        });

        b2.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                clearTextBoxes();
                txt2.setText("Goodbye!");
            }
        });

        add(b1);
        add(txt1);
        add(b2);
        add(txt2);
    }

    private void clearTextBoxes() {
        txt1.setText("");
        txt2.setText("");
    }

    private void setupFrame() {
        setTitle("Button Event Demo");
        setSize(250, 250);
        setLayout(new FlowLayout());
    }
}

class ButtonsEventDemo {
    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                ButtonsEventFrame frame = new ButtonsEventFrame();
                frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
                frame.setVisible(true);
            }
        });
    }
}

0

@up@ zapomniałeś validata i packa

0

Wielkie Dzięki Panowie :)

1 użytkowników online, w tym zalogowanych: 0, gości: 1