1

How can I fix blank screen on jframe and set values of vgap and hgap from textfield. i am using borderlayout for this.

import java.awt.*;

import javax.swing.*;

public class d1{

public static void main(String[] args) {
    JFrame f1 = new JFrame ("Border Layout") ; 
    f1.setLayout(new BorderLayout());
    f1.setVisible(true);
    f1.setSize(400,400);
    f1.setLocationRelativeTo(null);
    f1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    JTextField t1 = new JTextField();
    t1.setBorder(BorderFactory.createLineBorder(Color.BLACK));
    JTextField t2 = new JTextField();
    t2.setBorder(BorderFactory.createLineBorder(Color.BLACK));

    JPanel p1 = new JPanel ();
    p1.setLayout(new BorderLayout());
    p1.add(new JButton("East"), BorderLayout.EAST);
    p1.add(new JButton("South"), BorderLayout.SOUTH);
    p1.add(new JButton("West"), BorderLayout.WEST);
    p1.add(new JButton("North"), BorderLayout.NORTH);
    p1.add(new JButton("Center"), BorderLayout.CENTER);

    JPanel p2 = new JPanel ();
    p2.setLayout(new BorderLayout());
    JPanel p3 = new JPanel ();
    p3.setLayout(new BorderLayout());
    p3.add(new JLabel("Vgap"), BorderLayout.WEST);
    p3.add(t1, BorderLayout.CENTER);
    JPanel p4 = new JPanel ();
    p4.setLayout(new BorderLayout());
    p4.add(new JLabel("Hgap"), BorderLayout.WEST);
    p4.add(t2, BorderLayout.CENTER);

    JPanel p5 = new JPanel();
    p5.setLayout(new BorderLayout());
    p5.add(new JLabel("Container of BorderLayout"));
    JPanel p6 = new JPanel();
    p6.setLayout(new BorderLayout());
    p6.add(new JLabel("BorderLayout Properties"));

    JPanel p7 = new JPanel ();
    p7.setLayout(new BorderLayout());
    p7.add(p6, BorderLayout.NORTH);
    p7.add(p2, BorderLayout.SOUTH);

    p2.add(p3, BorderLayout.NORTH);
    p2.add(p4, BorderLayout.SOUTH);

    f1.add(p1, BorderLayout.CENTER);
    f1.add(p7, BorderLayout.SOUTH);
    f1.add(p5, BorderLayout.NORTH);

}

}

after this code.

There should be space between north-south and center, north, south and center.

I can not fix blank screen problem when frame is opened.

Joe Taras
  • 15,166
  • 7
  • 42
  • 55
Poké
  • 11
  • 3

2 Answers2

1

Add your panels before f1.setVisible(True) on frame and f1.pack() the frame after it.

You don't need to set a fixed size for your frame. Your added components should take care of it.

To set your components, look at MigLayout. It's easy to use and set components the way you need it.

MVAmorim
  • 105
  • 2
  • 3
  • 11
  • should i do it for textfield? – Poké Oct 26 '15 at 15:14
  • if you are getting a blank screen, how do you know your textfield are not set right? I recommend miglayout for layout your components, its very usefull and easy to work. http://www.miglayout.com/ – MVAmorim Oct 26 '15 at 15:17
  • 1
    anyway it didnt need. i relocate f1.setVisible(True) to last line. i worked. thanks – Poké Oct 26 '15 at 15:18
  • i also need vgap and hgap values. these should be got from textfield. – Poké Oct 26 '15 at 15:20
  • Set your borderLayout to a variable and set setHgap(int) and setVgap(int). – MVAmorim Oct 26 '15 at 15:22
  • i need something different , values of hgap and vgap are inputs which comes from textfield – Poké Oct 26 '15 at 15:24
  • you mean you will dynamically change the gap based on textfield text? – MVAmorim Oct 26 '15 at 15:26
  • I don't know if it works after your set the components position, but try to pass your textField text (after parsing it to int) to your BorderLayout variable by .setHgap(int). – MVAmorim Oct 26 '15 at 15:29
  • excuse me, i dont know how to do it or i can not understand , could you be more specific? – Poké Oct 26 '15 at 15:31
  • Is it your school project? https://docs.oracle.com/javase/tutorial/uiswing/components/ and http://docs.oracle.com/javase/7/docs/api/java/awt/BorderLayout.html – MVAmorim Oct 26 '15 at 15:35
  • kinda, project is related to them. i just need : when someone writes a int value on the 1st textfield , there will be 15 unit space between east , center and center,west. if i write to textfield 10 , there should be 10 unit space between east and center and also other side. same to hgap – Poké Oct 26 '15 at 15:42
0

please check this link you can find it here but first you should add ActionListener into your textField objects so that they can take numbers from inside of themselves. Providing white space in a Swing GUI something like that

    t1 = new JTextField();
    t1.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            String v = t1.getText();
            int numberhGap = Integer.parseInt(v);

        }
    });

    t1.setBorder(BorderFactory.createLineBorder(Color.BLACK));
    t2 = new JTextField();
    t2.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            String h = t2.getText();
            int numberhGap = Integer.parseInt(h);

        }
    });

and declare t1 and t2 outside main method. like this

public class d1{
    static JTextField t1;
    static JTextField t2;
Community
  • 1
  • 1
Earthx9
  • 91
  • 1
  • 1
  • 9