简体   繁体   中英

Can I add more than on button to a java JFrame?

I am facing a problem, I want to add more than a button to a JFrame, but it only takes the last one and puts it into the frame, a sample of my code is below:

String isName = "";

JFrame frame = new JFrame(isName);
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);

String childAmb = "PDA276";

for (int j=0; j<3; j++){

    if (childAmb.matches("Phone\\w\\w\\w"))
        fancyButtonCreator(childAmb, new ImageIcon ("src/phone.gif"), frame);

    else if (childAmb.matches("PDA\\w\\w\\w"))
        fancyButtonCreator(childAmb, new ImageIcon ("src/pda.gif"), frame);

    else if (childAmb.matches("PC\\w\\w\\w"))
        fancyButtonCreator(childAmb, new ImageIcon ("src/pc.gif"), frame);
    }

   frame.setVisible(true);
   frame.setBounds(100, 200, 200, 200);

Thank you.


If you don't have a layout-manager, only one, the last component added will show up.

frame.setLayout (new FlowLayout ());
frame.add (new JButton ("foo"));
frame.add (new JButton ("bar"));

Read the section from the Swing tutorial on Using Layout Managers for examples.

You can start with the FlowLayout.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM