Java Swing - 位于JTabbedframe左上角的位置面板

时间:2015-10-24 00:51:10

标签: java swing jpanel gridbaglayout jtabbedpane

由于工作的变化,我不得不从Python TKinter迁移到Java Swing。

我遇到的问题是:

如何在JTabbedFrame的左上角放置包含标签和文本字段等对象的面板。

如果框架本身比面板大,则JPanel默认在页面上居中。

到目前为止,这是我的代码:

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();
            }
        });
    }    
}

结果如下所示

enter image description here

我想要做的是将JPanel的位置移动到左上方,无论框架有多大,例如标签页的位置0,0。取消{{1}的居中位置}。

我正在使用JPanel经理。

1 个答案:

答案 0 :(得分:2)

不要忽视像MigLayout这样的东西,但如果你一直停留在限制第三方库的项目上,那么知道如何操纵默认布局管理器是你可以开发的最重要的技能之一< / p>

你可以简单地添加一个&#34;填充物&#34;组件放入您的布局,设置它的weightxweighty属性以填充可用空间

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);
        }

    }

}

通过更改

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

gbc.gridwidth = GridBagConstraints.REMAINDER;

我也可以影响title的位置

Title position

但您也可以使用anchor属性来实现它。你使用哪个将满足你的需求

有关详细信息,请查看How to Use GridBagLayout