Home java ActionListener when working with java swing

ActionListener when working with java swing

Author

Date

Category

It is necessary that when pressing the button I changed the color of TextField’a … But it is necessary that the condition is realized: if the color is brown background, then it changes to red.
No errors, no warnings give me me, but when compiling the program, nothing works anything.

jbutton roombutton1 = new jbutton ("Hotel Room No. 1");
JTEXTFIELD ROOMFIELD1 = NEW JTEXTFIELD ();
Roomfield1.SetBackGround (New Color (153, 51, 0));
Roomfield1.SetForeGround (New Color (255, 102, 0));
// Buttons and ActionListener
RoomButton1.AddActiveListener (New ActionListener () {
  @Override
  Public Void ActionPerForMed (ActionEvent E) {
    If (RoomButton1.getBackground () == New Color (153, 51, 0)) {
      JOPTIONPANE.SHOWMESSAdialog (NULL, "The number is successfully booked!");
      Roomfield1.SetBackGround (New Color (204, 0, 0));
    }
  }
});

Answer 1, Authority 100%

Try to compare once created objects, and not every time new:

Public Class SO_1017232 {
Static Color C1 = NEW COLOR (69, 153, 60);
Public Static Void Main (String [] Args) {
  Jframe f = new jframe ();
  F.Setlayout (New FlowLayout ());
  JButton RoomButton1 = New Jbutton ("Hotel Room No. 1");
  JTEXTFIELD ROOMFIELD1 = NEW JTEXTFIELD ();
  Roomfield1.SetPreferredSize (New Dimension (100,100));
  F.Add (Roomfield1);
  F.Add (RoomButton1);
  Roomfield1.SetBackGround (C1);
  Roomfield1.SetForeGround (New Color (255, 102, 0));
  RoomButton1.AddActiveListener (New ActionListener () {
    @Override
    Public Void ActionPerForMed (ActionEvent E) {
      if (Roomfield1.getBackground () == C1) {
        Roomfield1.SetBackGround (New Color (204, 0, 0));
        JOPTIONPANE.SHOWMESSAdialog (NULL, "The number is successfully booked!");
      }}
  });
  F.Setvisible (TRUE);
}
}

Objects created via New, but with default identical elements are not equal. After all, it can be in the village of 2 Ivanov Ivan Ivanovich, but at the same time different ….

You compare specimens by reference, they are different, because Each time you create a new object.

Programmers, Start Your Engines!

Why spend time searching for the correct question and then entering your answer when you can find it in a second? That's what CompuTicket is all about! Here you'll find thousands of questions and answers from hundreds of computer languages.

Recent questions