简体   繁体   中英

Java Button Placement using BorderLayout

This code displays nothing, I have exhausted many avenues but it does not display anything on the GUI (I have a main class that calls this as well already). Please help. I am trying to put the two JButtons horizontally at the bottom of the page and the JTextField and JLabel at the center of the screen.

package test;

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

public class Gui extends JFrame {
    private JLabel label;
    private JButton clear;
    private JButton copy;
    private JTextField textfield;

    public Gui(){

        super("test");

        clear = new JButton("Clear");
        copy = new JButton("Copy");
        label = new JLabel("");
        textfield = new JTextField("enter text here");

        JPanel bottom = new JPanel(new BorderLayout());

        JPanel subBottom = new JPanel();
        subBottom.add(copy);
        subBottom.add(clear);


        JPanel centre = new JPanel (new BorderLayout());

        JPanel subCentre = new JPanel();
        subCentre.add(label);
        subCentre.add(textfield);





        bottom.add(subBottom, BorderLayout.PAGE_END);
        centre.add(subCentre, BorderLayout.CENTER);

        }


    }

Your code is a bit over-complicated. You only need two panels, centre , and buttons . There are two reasons your UI is not showing up:

  1. You never added the panels to the frame
  2. You never set visible to true(Achieve this by using setVisible(true) ), unless you did this in the class you ran it in.

One simple way to achieve your desired UI is like so(I added a main method to show the window):

import javax.swing.*;
import java.awt.*;
public class test extends JFrame {
    public Test() {
        super("test");
        JPanel buttons = new JPanel();
        JPanel centre = new JPanel();
        add(buttons, BorderLayout.SOUTH); //these lines add the 
        add(centre, BorderLayout.CENTER); //panels to the frame
        JButton clear = new JButton("Clear");                     // No need
        JButton copy = new JButton("Copy");                       // to declare
        JLabel label = new JLabel("Label");                       // these
        JTextField textfield = new JTextField("enter text here"); // privately
        buttons.add(copy); 
        buttons.add(clear);
        centre.add(label);
        centre.add(textfield);
        pack();
        setLocationRelativeTo(null);
        setVisible(true);
        setDefaultCloseOperation(EXIT_ON_CLOSE);
    } //end constructor

    //added main method to run the UI
    public static void main(String[] args) {
        new Experiments();
    } //end main
} //end class

And it shows the window:

窗口

I got closer but its not pretty code, the JFrame is 500x500 so this works based on that... any better suggestions than what I have?

package lab6;

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

public class Gui extends JFrame {
    private JLabel label;
    private JButton clear;
    private JButton copy;
    private JTextField textfield;

    public Gui(){

        super("test");

        clear = new JButton("Clear");
        copy = new JButton("Copy");
        label = new JLabel("label");
        textfield = new JTextField("enter text here");

        JPanel masterPanel = new JPanel(new BorderLayout());

        JPanel top = new JPanel();
        top.setPreferredSize(new Dimension(100, 200));

        JPanel bottom = new JPanel();

        JPanel subBottom = new JPanel();
        subBottom.add(copy);
        subBottom.add(clear);


        JPanel centre = new JPanel ();

        JPanel subCentre = new JPanel();
        subCentre.add(label);
        subCentre.add(textfield);


        bottom.add(subBottom);
        centre.add(subCentre);

        masterPanel.add(bottom, BorderLayout.PAGE_END);
        masterPanel.add(top, BorderLayout.PAGE_START);
        masterPanel.add(centre, BorderLayout.CENTER);


        add(masterPanel);
    }


}

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