0

Guys i'm new to awt library. I'm try to build a simple GUI using java awt. I want to build something react with button thing.

enter codepackage chapter19;
import java.awt.*;
 import java.awt.event.*;
class Gui05 extends Frame implements ActionListener,ItemListener{
private static final ItemEvent ItemEvent = null;
TextField[] tfs;// south
Button[] bts; //North
TextArea ta;
Checkbox[] cbs;// east3
Button bt;

Gui05(){
    super("Compound Layout");

    create();
    allocate();
    button(null);


}
private void allocate(){

    this.setLayout(new BorderLayout(5,5));
    Panel cp = new Panel();
    cp.setLayout(null);
    ta.setSize(400,300);
    ta.setLocation(20,20);

    this.add("Center",ta);

    for(Button b: bts){
    // panel 이란 컨테이너를 사용해야 한다 
    Panel np = new Panel();
    np.setLayout(new GridLayout(1,4,5,5));
    for(Button t: bts){
        np.add(t);


    }
    this.add("North",np);

    Panel sp = new Panel();
    sp.setLayout(new GridLayout(1,2,5,5));
    sp.add(tfs[0]);
    sp.add(tfs[1]);
    this.add("South",sp);



    Panel ep = new Panel();
    ep.setLayout(new GridLayout(2,1,5,5));
    Panel ex = new Panel();
    ex.setLayout(new GridLayout(3,1,5,5));
    //ex.add(bt);
    //
    for(Checkbox t: cbs){
        ep.add(t);


    }
    ep.add(ex);
    ep.add(bt);
    this.add("East",ep);
    this.setSize(500, 430);
    this.setResizable(false);

    }







}




private void create(){
    tfs = new TextField[]{new TextField(),new TextField()};
    bts = new Button[]{new Button("NEW"), new Button("OPEN")

    , new Button("CLOSE"), new Button("EXIT")};



    ta = new TextArea();
    cbs = new Checkbox[]{new Checkbox("red"),new Checkbox("yello"),new      Checkbox("green")};
    bt = new Button("enter");
    bt.addActionListener(this);

    addWindowListener(
            new WindowAdapter()
            {
                public void windowClosing(WindowEvent e){

                    System.exit(0);
                }
            }
            );


    this.setSize(1000, 800);
}

public void button(ActionEvent ie){


    String arg = bt.getActionCommand();
    if(arg == "enter"){
        System.out.println("checking..");
        Checkbox tempcbs = new Checkbox("red");
        //System.out.println("arg2..."+arg2);
        if(tempcbs == cbs[0]){
            System.out.println("red is printing...");
            setBackground(Color.red);

        }


    }

}




//public void stateChange(ItemEvent ie){
    //String arg = bt.getActionCommand();
    //System.out.println("arg..."+arg);



//}
@Override
public void itemStateChanged(ItemEvent e) {
    // TODO Auto-generated method stub

}
@Override
public void actionPerformed(ActionEvent e) {
    // TODO Auto-generated method stub

}


      }

       public class GuiEx05 {
            public static void main(String[] args) {
            new Gui05().setVisible(true);



      }
   }
here

and this part

enter code here public void button(ActionEvent ie){


    String arg = bt.getActionCommand();
    if(arg == "enter"){
        System.out.println("checking..");
        Checkbox tempcbs = new Checkbox("red");
        //System.out.println("arg2..."+arg2);
        if(tempcbs == cbs[0]){
            System.out.println("red is printing...");
            setBackground(Color.red);

        }

its printing "checking.." even though i did not click the enter button and I want to check checkBox's text is match with 'red','yello' string. how to do this?

  • Avoid using `null` layouts, pixel perfect layouts are an illusion within modern ui design. There are too many factors which affect the individual size of components, none of which you can control. Swing was designed to work with layout managers at the core, discarding these will lead to no end of issues and problems that you will spend more and more time trying to rectify – MadProgrammer May 03 '16 at 10:48
  • I would strongly recommend you start with the Swing library or JavaFX and waste your time with the AWT API, but that's just me – MadProgrammer May 03 '16 at 10:49

0 Answers0