0

Please take a look at my code it is working fine the way I want but the only issue is that I want to add another button opposite my current button and I am not able to do so can any body please help me.

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

public class Example2 extends JFrame {

public Example2() {
        initUI();
    }

public final void initUI() {
        JPanel panel = new JPanel();
        getContentPane().add(panel);
        panel.setLayout(null);
        panel.setToolTipText("A Panel container");

        JButton button = new JButton("Even");
        button.setBounds(100, 60, 100, 30);
        button.setToolTipText("A button component");

        JButton button2 = new JButton("Odd");
        button2.setBounds(100, 60, 100, 30);
        button2.setToolTipText("A button component");

        //Add action listener to button
                button.addActionListener(new ActionListener () {
                    public void actionPerformed(ActionEvent  e)
                    {
                        //Execute when button is pressed
                        System .out.println("You clicked the button");
                        int sum=0;
                                for(int i=1;i<=100;i++){
                                    if(i%2==0){
                                        System.out.println(i);
                                        sum+=i;
                                    }
                                }
        System.out.println("Sum of even numbers: "+sum);
                    }
        });

        panel.add(button);
        panel.add(button2);

        setTitle("Tooltip");
        setSize(500, 400);
        setLocationRelativeTo(null);
        setDefaultCloseOperation(EXIT_ON_CLOSE);

    }
    public static void main(String[] args) {
                Example2 ex = new Example2();
                ex.setVisible(true);
    }
}
MD Sayem Ahmed
  • 28,628
  • 27
  • 111
  • 178
KennEthel
  • 39
  • 2
  • 4
  • possible duplicate of [Display another button and print even numbers when button pressed](http://stackoverflow.com/questions/9961713/display-another-button-and-print-even-numbers-when-button-pressed) – Robin Apr 01 '12 at 08:46

3 Answers3

7
panel.setLayout(null);

That is where it starts to go wrong.

  1. Use layouts. See Laying Out Components Within a Container & Effective Layout Management: Short Course for more details.
  2. Use:
    • The appropriate layouts.
    • Possibly nested inside one another.
    • With appropriate layout padding and component border/insets for white space.

As an aside.

    ...
    button.setBounds(100, 60, 100, 30);
    button.setToolTipText("A button component");

    JButton button2 = new JButton("Odd");
    button2.setBounds(100, 60, 100, 30);
    ...

Did you notice how the bounds of the two buttons were identical? What do you think happens when you put two components of the same size in the same place?

Community
  • 1
  • 1
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
  • I m not able to do it can you help – KennEthel Apr 01 '12 at 07:08
  • 1
    @user1054393 For useful links see my answer. – COD3BOY Apr 01 '12 at 07:19
  • 2 separate 'one line fixes' were provided by fkr, but also go through the links in the answers of @Sanjay & me. – Andrew Thompson Apr 01 '12 at 07:42
  • 1
    How ironic. This is exactly what was recommended in [his other question with the same snippet](http://stackoverflow.com/questions/9961713/display-another-button-and-print-even-numbers-when-button-pressed) – Robin Apr 01 '12 at 08:47
  • @Robin (sigh) You can lead a horse to water, but you can't make it drink. – Andrew Thompson Apr 01 '12 at 08:52
  • @AndrewThompson correct. +1 for your remark about the bounds. I skipped the rest of the code when I saw the `null` layout and started writing my answer – Robin Apr 01 '12 at 08:53
  • @Robin *"I skipped the rest of the code when I saw the `null` layout"* I got bored, and came **back** to it. Actually I was interested to see how it looked on-screen to guess how it was *supposed* to be laid out, but gave up when I realized there were two buttons in the exact same place! – Andrew Thompson Apr 01 '12 at 08:58
  • You got bored when you saw two buttons? – KennEthel Apr 04 '12 at 16:15
3

You have to change panel.setLayout(null) to layout you need. For example:

    panel.setLayout(new BoxLayout(panel, BoxLayout.X_AXIS));

or

    panel.setLayout(new java.awt.FlowLayout(java.awt.FlowLayout.CENTER));
fikr4n
  • 3,250
  • 1
  • 25
  • 46
3

Andrew Thompson +1 ,

Here are some usefull links :

  1. A Visual Guide to Layout Managers
  2. Using Layout Managers
  3. Adding space between components
Community
  • 1
  • 1
COD3BOY
  • 11,964
  • 1
  • 38
  • 56
  • That last one was a good answer, but missed one technique for adding white space, layout padding. E.G. `new GridLayout(0,3,10,10); // Use 10px padding` Because I can't up-vote the other answer (again) I'll just up-vote this one. BTW - thanks for the comment re links, I knew there was an edit open that I should have put through. ;) – Andrew Thompson Apr 01 '12 at 07:34