简体   繁体   中英

Java Swing - position panel in top left of a JTabbedframe

I am having to migrate from Python TKinter to Java Swing due to a change of jobs.

The problem I have is:

How do I position a panel containing objects such as labels and text fields in the top left hand corner of JTabbedFrame .

If the frame itself is bigger than the panel , the JPanel is centering by default on the page.

Here is my code so far:

public class GUI {

    //Constructor
    GUI(){
        //Craete a new frame
        //borderlayout
        JFrame jfrm = new JFrame("Tabbed Demo");            
        //Intialize size
        jfrm.setSize(500, 250);         
        //Terminate program 
        jfrm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);            
        //Create Tabbed Frame
        JTabbedPane jtp = new JTabbedPane(JTabbedPane.TOP);         
        //Create a panel to place on tabbed page
        JPanel jpnl = new JPanel();
        jpnl.setLayout(new GridBagLayout());
        GridBagConstraints gbc = new GridBagConstraints();
        gbc.fill=GridBagConstraints.HORIZONTAL;
        gbc.insets = new Insets(5,5,5,5);           
        jpnl.setOpaque(true);
        jpnl.setComponentOrientation(ComponentOrientation.LEFT_TO_RIGHT);
        //create a 3 labels horizontally            
        gbc.gridx=1;
        gbc.gridy=0;
        JLabel title= new JLabel(" Tracking System   ");
        title.setFont(new Font("Serif", Font.PLAIN, 40));
        jpnl.add(title,gbc);
        gbc.gridx=0;
        gbc.gridy=1;    
        JLabel subtitle= new JLabel("First");
        subtitle.setFont(new Font("Serif", Font.PLAIN, 18));
        jpnl.add(subtitle,gbc);
        gbc.gridx=1;
        gbc.gridy=1;
        JTextArea firstText = new JTextArea("Type Here!");
        firstText.setFont(new Font("Serif", Font.PLAIN, 18));
        jpnl.add(firstText,gbc);            
        gbc.gridx=0;
        gbc.gridy=2;            
        JLabel subtitle2= new JLabel("Last");
        jpnl.add(subtitle2,gbc);
        subtitle2.setFont(new Font("Serif", Font.PLAIN, 18));           
        //add to Tab
        jtp.addTab("Demo", jpnl);           
        //make visible
        jfrm.getContentPane().add(jtp);         
        jfrm.setVisible(true);
    }

    public static void main(String[] args) {
        // launch app

        //create the frame on the event dispaytching thread
        SwingUtilities.invokeLater(new Runnable(){
            public void run(){
                new GUI();
            }
        });
    }    
}

The result is shown below

在此处输入图片说明

What I want to do is move the location of the JPanel to the top left no matter how big the frame is, eg location 0,0 of the tabbed page.Override the centring of the JPanel .

I am using the GridBaglayout manager.

Not to discount the power of something like MigLayout, but if you're stuck on a project which limits third party libraries, then knowing how to manipulate the default layout managers is one of the most important skills you can develop

You could simply add a "filler" component into you layout, setting it's weightx and weighty properties to fill the available space

GridBagLayout的

import java.awt.EventQueue;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

public class TestLayout {

    public static void main(String[] args) {
        new TestLayout();
    }

    public TestLayout() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                    ex.printStackTrace();
                }

                JFrame frame = new JFrame("Testing");
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.add(new TestPane());
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }

    public class TestPane extends JPanel {

        public TestPane() {
            setLayout(new GridBagLayout());
            GridBagConstraints gbc = new GridBagConstraints();
            gbc.gridx = 0;
            gbc.gridy = 0;
            gbc.gridwidth = 2; //GridBagConstraints.REMAINDER;
            gbc.insets = new Insets(4, 4, 4, 4);

            JLabel title = new JLabel("Tracking System");
            title.setFont(title.getFont().deriveFont(40f));
            add(title, gbc);

            gbc.gridy++;
            gbc.gridwidth = 1;
            gbc.anchor = GridBagConstraints.WEST;
            add(new JLabel("First: "), gbc);
            gbc.gridy++;
            add(new JLabel("First: "), gbc);

            gbc.gridx++;
            gbc.gridy = 1;
            gbc.gridwidth = 1;
            gbc.anchor = GridBagConstraints.WEST;
            gbc.fill = GridBagConstraints.HORIZONTAL;
            add(new JTextField(10), gbc);
            gbc.gridy++;
            add(new JTextField(10), gbc);

            gbc.gridx++;
            gbc.gridy++;
            gbc.weightx = 1;
            gbc.weighty = 1;
            // Blank/filler component
            add(new JLabel(), gbc);
        }

    }

}

By changing

gbc.gridwidth = 2; //GridBagConstraints.REMAINDER;

to

gbc.gridwidth = GridBagConstraints.REMAINDER;

I can effect the position of the title as well

头衔

But you can also effect it by using the anchor property. Which you use will come down to your needs

Have a look at How to Use GridBagLayout for more details

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