1

I was trying the simple Swing application. I have added 6 Buttons vertically with 4-5 line gaps in panel but not all are visible when I execute the program. Bottom buttons are not showing. I have added the scroll pane functionality but it's not happening.

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.Rectangle;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import javax.swing.JToolBar;

public class Frame6 extends JFrame {
    private BorderLayout layoutMain = new BorderLayout();
    private JPanel panelCenter = new JPanel();
// private JScrollPane  panel1 = new JScrollPane(panel);
    private JMenuBar menuBar = new JMenuBar();
    private JMenu menuFile = new JMenu();
    private JMenuItem menuFileExit = new JMenuItem();
    private JToolBar toolBar = new JToolBar();
    private JButton buttonOpen = new JButton();
    private JButton buttonClose = new JButton();
    private JButton buttonHelp = new JButton();
    private ImageIcon imageOpen = new ImageIcon(Frame6.class.getResource("openfile.gif"));
    private ImageIcon imageClose = new ImageIcon(Frame6.class.getResource("closefile.gif"));
    private ImageIcon imageHelp = new ImageIcon(Frame6.class.getResource("help.gif"));
    private JButton jButton1 = new JButton();
    private JButton jButton2 = new JButton();
    private JButton jButton3 = new JButton();
    private JButton jButton4 = new JButton();
    private JButton jButton5 = new JButton();
    private JButton jButton6 = new JButton();

    public Frame6() {
        try {
            jbInit();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private void jbInit() throws Exception {
        this.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
        this.setVisible(true);
        this.setJMenuBar( menuBar );
        this.getContentPane().setLayout( layoutMain );
        panelCenter.setLayout( null );
        this.setSize(new Dimension(401, 1100));
        menuFile.setText( "File" );
        menuFileExit.setText( "Exit" );
        menuFileExit.addActionListener( new ActionListener() { public void actionPerformed( ActionEvent ae ) { fileExit_ActionPerformed( ae ); } } );
        buttonOpen.setToolTipText( "Open File" );
        buttonOpen.setIcon( imageOpen );
        buttonClose.setToolTipText( "Close File" );
        buttonClose.setIcon( imageClose );
        buttonHelp.setToolTipText( "About" );
        buttonHelp.setIcon( imageHelp );
        jButton1.setText("jButton1");
        jButton1.setBounds(new Rectangle(45, 75, 160, 20));
        jButton2.setText("jButton2");
        jButton2.setBounds(new Rectangle(40, 200, 185, 20));
        jButton3.setText("jButton3");
        jButton3.setBounds(new Rectangle(45, 390, 210, 20));
        jButton4.setText("jButton4");
        jButton4.setBounds(new Rectangle(35, 590, 175, 20));
        jButton5.setText("jButton5");
        jButton5.setBounds(new Rectangle(30, 755, 200, 20));
        jButton6.setText("jButton6");
        jButton6.setBounds(new Rectangle(55, 945, 190, 20));
        menuFile.add( menuFileExit );
        menuBar.add( menuFile );
        toolBar.add( buttonOpen );
        toolBar.add( buttonClose );
        toolBar.add( buttonHelp );
        this.getContentPane().add( toolBar, BorderLayout.NORTH );
        panelCenter.add(jButton6, null);
        panelCenter.add(jButton5, null);
        panelCenter.add(jButton4, null);
        panelCenter.add(jButton3, null);
        panelCenter.add(jButton2, null);
        panelCenter.add(jButton1, null);
        this.getContentPane().add( panelCenter, BorderLayout.CENTER );
    }

public static void main(String[] args) {
        new Frame6();
    }
    void fileExit_ActionPerformed(ActionEvent e) {
        System.exit(0);
    }
}
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
  • 4
    `panelCenter.setLayout( null )` Java GUIs might have to work on a number of platforms, on different screen resolutions & using different PLAFs. As such they are not conducive to exact placement of components. To organize the components for a robust GUI, instead [use layout managers](http://docs.oracle.com/javase/tutorial/uiswing/layout/index.html), or [combinations of them](http://stackoverflow.com/a/5630271/418556), along with layout padding & borders for [white space](http://stackoverflow.com/q/17874717/418556). – Andrew Thompson Jul 11 '14 at 10:54
  • `jButton1` through 6 are visible when I run your code. – David Yee Jul 11 '14 at 10:58
  • i submit to @DavidYee s comment. If i run your code i can see every button. – Rubinum Jul 11 '14 at 11:43
  • Study the GridBagLayout. It gives you all the flexibility you need. You can place component where you want and it works well when changing the size of the window. Read [this tutorial](http://docs.oracle.com/javase/tutorial/uiswing/layout/gridbag.html) – Alejandro Vera Jul 11 '14 at 18:09

0 Answers0