How to add a JButton to a JFrame?










0















I tried to add a button to the JFrame, but it won't appear for some reason. How do I make it appear?



import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.JPanel;
import javax.swing.SwingConstants;
import javax.swing.border.Border;
import javax.swing.BorderFactory;
import java.awt.Color;
import java.awt.Container;
import java.awt.Rectangle;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.*;

public class GraficoconArreglo extends javax.swing.JFrame {
JPanel pan = (JPanel) this.getContentPane();
JLabel lab = new JLabel[6];
JTextField text = new JTextField[6];
Border border = BorderFactory.createLineBorder(Color.pink,1);
JButton b = new JButton("Calculate");

public GraficoconArreglo()
initComponents();
pan.setLayout(null);
pan.setBackground(Color.GRAY);
for(int i=0; i<lab.length ;i++)
lab[i] = new JLabel();
text[i] = new JTextField();

lab[i].setBounds(new Rectangle(15,(i+1)*40, 60, 25));
lab[i].setText("Data " + (i+1));
lab[i].setBorder(border);
text[i].setBounds(new Rectangle(100,(i+1)*40, 60, 25));

pan.add(lab[i],null);
pan.add(text[i],null);

setSize(200,330);
setTitle("Arrays in forums.");

add(b);
b.addActionListener((ActionListener) this);











share|improve this question



















  • 2





    Never use a null layout. Never try to call setBounds directly. It'll never work. Your button is probably not appearing because it is outside of the bounds.

    – markspace
    Nov 15 '18 at 18:11











  • 1) For better help sooner, post a Minimal, Complete, and Verifiable example or Short, Self Contained, Correct Example. 2) Provide ASCII art or a simple drawing of the intended layout of the GUI at minimum size, and if resizable, with more width and height - to show how the extra space should be used. 3) Java GUIs have to work on different OS', screen size, screen resolution etc. using different PLAFs in different locales. As such, they are not conducive to pixel perfect layout. Instead use layout managers, or combinations of them ..

    – Andrew Thompson
    Nov 16 '18 at 0:39











  • .. along with layout padding and borders for white space.

    – Andrew Thompson
    Nov 16 '18 at 0:39















0















I tried to add a button to the JFrame, but it won't appear for some reason. How do I make it appear?



import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.JPanel;
import javax.swing.SwingConstants;
import javax.swing.border.Border;
import javax.swing.BorderFactory;
import java.awt.Color;
import java.awt.Container;
import java.awt.Rectangle;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.*;

public class GraficoconArreglo extends javax.swing.JFrame {
JPanel pan = (JPanel) this.getContentPane();
JLabel lab = new JLabel[6];
JTextField text = new JTextField[6];
Border border = BorderFactory.createLineBorder(Color.pink,1);
JButton b = new JButton("Calculate");

public GraficoconArreglo()
initComponents();
pan.setLayout(null);
pan.setBackground(Color.GRAY);
for(int i=0; i<lab.length ;i++)
lab[i] = new JLabel();
text[i] = new JTextField();

lab[i].setBounds(new Rectangle(15,(i+1)*40, 60, 25));
lab[i].setText("Data " + (i+1));
lab[i].setBorder(border);
text[i].setBounds(new Rectangle(100,(i+1)*40, 60, 25));

pan.add(lab[i],null);
pan.add(text[i],null);

setSize(200,330);
setTitle("Arrays in forums.");

add(b);
b.addActionListener((ActionListener) this);











share|improve this question



















  • 2





    Never use a null layout. Never try to call setBounds directly. It'll never work. Your button is probably not appearing because it is outside of the bounds.

    – markspace
    Nov 15 '18 at 18:11











  • 1) For better help sooner, post a Minimal, Complete, and Verifiable example or Short, Self Contained, Correct Example. 2) Provide ASCII art or a simple drawing of the intended layout of the GUI at minimum size, and if resizable, with more width and height - to show how the extra space should be used. 3) Java GUIs have to work on different OS', screen size, screen resolution etc. using different PLAFs in different locales. As such, they are not conducive to pixel perfect layout. Instead use layout managers, or combinations of them ..

    – Andrew Thompson
    Nov 16 '18 at 0:39











  • .. along with layout padding and borders for white space.

    – Andrew Thompson
    Nov 16 '18 at 0:39













0












0








0


1






I tried to add a button to the JFrame, but it won't appear for some reason. How do I make it appear?



import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.JPanel;
import javax.swing.SwingConstants;
import javax.swing.border.Border;
import javax.swing.BorderFactory;
import java.awt.Color;
import java.awt.Container;
import java.awt.Rectangle;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.*;

public class GraficoconArreglo extends javax.swing.JFrame {
JPanel pan = (JPanel) this.getContentPane();
JLabel lab = new JLabel[6];
JTextField text = new JTextField[6];
Border border = BorderFactory.createLineBorder(Color.pink,1);
JButton b = new JButton("Calculate");

public GraficoconArreglo()
initComponents();
pan.setLayout(null);
pan.setBackground(Color.GRAY);
for(int i=0; i<lab.length ;i++)
lab[i] = new JLabel();
text[i] = new JTextField();

lab[i].setBounds(new Rectangle(15,(i+1)*40, 60, 25));
lab[i].setText("Data " + (i+1));
lab[i].setBorder(border);
text[i].setBounds(new Rectangle(100,(i+1)*40, 60, 25));

pan.add(lab[i],null);
pan.add(text[i],null);

setSize(200,330);
setTitle("Arrays in forums.");

add(b);
b.addActionListener((ActionListener) this);











share|improve this question
















I tried to add a button to the JFrame, but it won't appear for some reason. How do I make it appear?



import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.JPanel;
import javax.swing.SwingConstants;
import javax.swing.border.Border;
import javax.swing.BorderFactory;
import java.awt.Color;
import java.awt.Container;
import java.awt.Rectangle;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.*;

public class GraficoconArreglo extends javax.swing.JFrame {
JPanel pan = (JPanel) this.getContentPane();
JLabel lab = new JLabel[6];
JTextField text = new JTextField[6];
Border border = BorderFactory.createLineBorder(Color.pink,1);
JButton b = new JButton("Calculate");

public GraficoconArreglo()
initComponents();
pan.setLayout(null);
pan.setBackground(Color.GRAY);
for(int i=0; i<lab.length ;i++)
lab[i] = new JLabel();
text[i] = new JTextField();

lab[i].setBounds(new Rectangle(15,(i+1)*40, 60, 25));
lab[i].setText("Data " + (i+1));
lab[i].setBorder(border);
text[i].setBounds(new Rectangle(100,(i+1)*40, 60, 25));

pan.add(lab[i],null);
pan.add(text[i],null);

setSize(200,330);
setTitle("Arrays in forums.");

add(b);
b.addActionListener((ActionListener) this);








java swing jframe jbutton layout-manager






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 19 '18 at 16:22







DigiLei

















asked Nov 15 '18 at 18:09









DigiLeiDigiLei

23




23







  • 2





    Never use a null layout. Never try to call setBounds directly. It'll never work. Your button is probably not appearing because it is outside of the bounds.

    – markspace
    Nov 15 '18 at 18:11











  • 1) For better help sooner, post a Minimal, Complete, and Verifiable example or Short, Self Contained, Correct Example. 2) Provide ASCII art or a simple drawing of the intended layout of the GUI at minimum size, and if resizable, with more width and height - to show how the extra space should be used. 3) Java GUIs have to work on different OS', screen size, screen resolution etc. using different PLAFs in different locales. As such, they are not conducive to pixel perfect layout. Instead use layout managers, or combinations of them ..

    – Andrew Thompson
    Nov 16 '18 at 0:39











  • .. along with layout padding and borders for white space.

    – Andrew Thompson
    Nov 16 '18 at 0:39












  • 2





    Never use a null layout. Never try to call setBounds directly. It'll never work. Your button is probably not appearing because it is outside of the bounds.

    – markspace
    Nov 15 '18 at 18:11











  • 1) For better help sooner, post a Minimal, Complete, and Verifiable example or Short, Self Contained, Correct Example. 2) Provide ASCII art or a simple drawing of the intended layout of the GUI at minimum size, and if resizable, with more width and height - to show how the extra space should be used. 3) Java GUIs have to work on different OS', screen size, screen resolution etc. using different PLAFs in different locales. As such, they are not conducive to pixel perfect layout. Instead use layout managers, or combinations of them ..

    – Andrew Thompson
    Nov 16 '18 at 0:39











  • .. along with layout padding and borders for white space.

    – Andrew Thompson
    Nov 16 '18 at 0:39







2




2





Never use a null layout. Never try to call setBounds directly. It'll never work. Your button is probably not appearing because it is outside of the bounds.

– markspace
Nov 15 '18 at 18:11





Never use a null layout. Never try to call setBounds directly. It'll never work. Your button is probably not appearing because it is outside of the bounds.

– markspace
Nov 15 '18 at 18:11













1) For better help sooner, post a Minimal, Complete, and Verifiable example or Short, Self Contained, Correct Example. 2) Provide ASCII art or a simple drawing of the intended layout of the GUI at minimum size, and if resizable, with more width and height - to show how the extra space should be used. 3) Java GUIs have to work on different OS', screen size, screen resolution etc. using different PLAFs in different locales. As such, they are not conducive to pixel perfect layout. Instead use layout managers, or combinations of them ..

– Andrew Thompson
Nov 16 '18 at 0:39





1) For better help sooner, post a Minimal, Complete, and Verifiable example or Short, Self Contained, Correct Example. 2) Provide ASCII art or a simple drawing of the intended layout of the GUI at minimum size, and if resizable, with more width and height - to show how the extra space should be used. 3) Java GUIs have to work on different OS', screen size, screen resolution etc. using different PLAFs in different locales. As such, they are not conducive to pixel perfect layout. Instead use layout managers, or combinations of them ..

– Andrew Thompson
Nov 16 '18 at 0:39













.. along with layout padding and borders for white space.

– Andrew Thompson
Nov 16 '18 at 0:39





.. along with layout padding and borders for white space.

– Andrew Thompson
Nov 16 '18 at 0:39












2 Answers
2






active

oldest

votes


















1














You are creating only one button and adding it to 6 different places. Therefore, you only would see it on the last place you added.






share|improve this answer






























    -2














    You should add the button to the contentPane, not the JFrame. A working code for this, provided from SwingDesigner from Eclipse Marketplace would be:



    public class Window extends JFrame 

    private JPanel contentPane;

    /**
    * Launch the application.
    */
    public static void main(String args)
    EventQueue.invokeLater(new Runnable()
    public void run()
    try
    Window frame = new Window();
    frame.setVisible(true);
    catch (Exception e)
    e.printStackTrace();


    );


    /**
    * Create the frame.
    */
    public Window()
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setBounds(100, 100, 450, 300);
    contentPane = new JPanel();
    contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
    contentPane.setLayout(null);
    setContentPane(contentPane);

    JButton btnNewButton = new JButton("New button");
    btnNewButton.setBounds(170, 110, 89, 23);
    contentPane.add(btnNewButton);








    share|improve this answer


















    • 1





      (1-) Don't use a null layout. Swing was designed to be used with layout managers. You should add the button to the contentPane, not the JFrame. - you can add the button to the frame. The add() method of the frame will forward the component to the content pane of the frame. There is no need to specifically set the content pane to a panel, since the default content pane already is a JPanel.

      – camickr
      Nov 15 '18 at 18:58










    Your Answer






    StackExchange.ifUsing("editor", function ()
    StackExchange.using("externalEditor", function ()
    StackExchange.using("snippets", function ()
    StackExchange.snippets.init();
    );
    );
    , "code-snippets");

    StackExchange.ready(function()
    var channelOptions =
    tags: "".split(" "),
    id: "1"
    ;
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function()
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled)
    StackExchange.using("snippets", function()
    createEditor();
    );

    else
    createEditor();

    );

    function createEditor()
    StackExchange.prepareEditor(
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader:
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    ,
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    );



    );













    draft saved

    draft discarded


















    StackExchange.ready(
    function ()
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53325520%2fhow-to-add-a-jbutton-to-a-jframe%23new-answer', 'question_page');

    );

    Post as a guest















    Required, but never shown

























    2 Answers
    2






    active

    oldest

    votes








    2 Answers
    2






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    1














    You are creating only one button and adding it to 6 different places. Therefore, you only would see it on the last place you added.






    share|improve this answer



























      1














      You are creating only one button and adding it to 6 different places. Therefore, you only would see it on the last place you added.






      share|improve this answer

























        1












        1








        1







        You are creating only one button and adding it to 6 different places. Therefore, you only would see it on the last place you added.






        share|improve this answer













        You are creating only one button and adding it to 6 different places. Therefore, you only would see it on the last place you added.







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 15 '18 at 18:21









        jordiburgosjordiburgos

        3,15432754




        3,15432754























            -2














            You should add the button to the contentPane, not the JFrame. A working code for this, provided from SwingDesigner from Eclipse Marketplace would be:



            public class Window extends JFrame 

            private JPanel contentPane;

            /**
            * Launch the application.
            */
            public static void main(String args)
            EventQueue.invokeLater(new Runnable()
            public void run()
            try
            Window frame = new Window();
            frame.setVisible(true);
            catch (Exception e)
            e.printStackTrace();


            );


            /**
            * Create the frame.
            */
            public Window()
            setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            setBounds(100, 100, 450, 300);
            contentPane = new JPanel();
            contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
            contentPane.setLayout(null);
            setContentPane(contentPane);

            JButton btnNewButton = new JButton("New button");
            btnNewButton.setBounds(170, 110, 89, 23);
            contentPane.add(btnNewButton);








            share|improve this answer


















            • 1





              (1-) Don't use a null layout. Swing was designed to be used with layout managers. You should add the button to the contentPane, not the JFrame. - you can add the button to the frame. The add() method of the frame will forward the component to the content pane of the frame. There is no need to specifically set the content pane to a panel, since the default content pane already is a JPanel.

              – camickr
              Nov 15 '18 at 18:58















            -2














            You should add the button to the contentPane, not the JFrame. A working code for this, provided from SwingDesigner from Eclipse Marketplace would be:



            public class Window extends JFrame 

            private JPanel contentPane;

            /**
            * Launch the application.
            */
            public static void main(String args)
            EventQueue.invokeLater(new Runnable()
            public void run()
            try
            Window frame = new Window();
            frame.setVisible(true);
            catch (Exception e)
            e.printStackTrace();


            );


            /**
            * Create the frame.
            */
            public Window()
            setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            setBounds(100, 100, 450, 300);
            contentPane = new JPanel();
            contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
            contentPane.setLayout(null);
            setContentPane(contentPane);

            JButton btnNewButton = new JButton("New button");
            btnNewButton.setBounds(170, 110, 89, 23);
            contentPane.add(btnNewButton);








            share|improve this answer


















            • 1





              (1-) Don't use a null layout. Swing was designed to be used with layout managers. You should add the button to the contentPane, not the JFrame. - you can add the button to the frame. The add() method of the frame will forward the component to the content pane of the frame. There is no need to specifically set the content pane to a panel, since the default content pane already is a JPanel.

              – camickr
              Nov 15 '18 at 18:58













            -2












            -2








            -2







            You should add the button to the contentPane, not the JFrame. A working code for this, provided from SwingDesigner from Eclipse Marketplace would be:



            public class Window extends JFrame 

            private JPanel contentPane;

            /**
            * Launch the application.
            */
            public static void main(String args)
            EventQueue.invokeLater(new Runnable()
            public void run()
            try
            Window frame = new Window();
            frame.setVisible(true);
            catch (Exception e)
            e.printStackTrace();


            );


            /**
            * Create the frame.
            */
            public Window()
            setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            setBounds(100, 100, 450, 300);
            contentPane = new JPanel();
            contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
            contentPane.setLayout(null);
            setContentPane(contentPane);

            JButton btnNewButton = new JButton("New button");
            btnNewButton.setBounds(170, 110, 89, 23);
            contentPane.add(btnNewButton);








            share|improve this answer













            You should add the button to the contentPane, not the JFrame. A working code for this, provided from SwingDesigner from Eclipse Marketplace would be:



            public class Window extends JFrame 

            private JPanel contentPane;

            /**
            * Launch the application.
            */
            public static void main(String args)
            EventQueue.invokeLater(new Runnable()
            public void run()
            try
            Window frame = new Window();
            frame.setVisible(true);
            catch (Exception e)
            e.printStackTrace();


            );


            /**
            * Create the frame.
            */
            public Window()
            setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            setBounds(100, 100, 450, 300);
            contentPane = new JPanel();
            contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
            contentPane.setLayout(null);
            setContentPane(contentPane);

            JButton btnNewButton = new JButton("New button");
            btnNewButton.setBounds(170, 110, 89, 23);
            contentPane.add(btnNewButton);









            share|improve this answer












            share|improve this answer



            share|improve this answer










            answered Nov 15 '18 at 18:30









            exileexile

            12




            12







            • 1





              (1-) Don't use a null layout. Swing was designed to be used with layout managers. You should add the button to the contentPane, not the JFrame. - you can add the button to the frame. The add() method of the frame will forward the component to the content pane of the frame. There is no need to specifically set the content pane to a panel, since the default content pane already is a JPanel.

              – camickr
              Nov 15 '18 at 18:58












            • 1





              (1-) Don't use a null layout. Swing was designed to be used with layout managers. You should add the button to the contentPane, not the JFrame. - you can add the button to the frame. The add() method of the frame will forward the component to the content pane of the frame. There is no need to specifically set the content pane to a panel, since the default content pane already is a JPanel.

              – camickr
              Nov 15 '18 at 18:58







            1




            1





            (1-) Don't use a null layout. Swing was designed to be used with layout managers. You should add the button to the contentPane, not the JFrame. - you can add the button to the frame. The add() method of the frame will forward the component to the content pane of the frame. There is no need to specifically set the content pane to a panel, since the default content pane already is a JPanel.

            – camickr
            Nov 15 '18 at 18:58





            (1-) Don't use a null layout. Swing was designed to be used with layout managers. You should add the button to the contentPane, not the JFrame. - you can add the button to the frame. The add() method of the frame will forward the component to the content pane of the frame. There is no need to specifically set the content pane to a panel, since the default content pane already is a JPanel.

            – camickr
            Nov 15 '18 at 18:58

















            draft saved

            draft discarded
















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid


            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.

            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53325520%2fhow-to-add-a-jbutton-to-a-jframe%23new-answer', 'question_page');

            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            這個網誌中的熱門文章

            How to read a connectionString WITH PROVIDER in .NET Core?

            Node.js Script on GitHub Pages or Amazon S3

            Museum of Modern and Contemporary Art of Trento and Rovereto