Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/337.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java Swing-JTabbedframe左上角的位置面板_Java_Swing_Jpanel_Gridbaglayout_Jtabbedpane - Fatal编程技术网

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

Java Swing-JTabbedframe左上角的位置面板,java,swing,jpanel,gridbaglayout,jtabbedpane,Java,Swing,Jpanel,Gridbaglayout,Jtabbedpane,由于工作的变化,我不得不从Python TKinter迁移到Java Swing 我的问题是: 如何在JTabbedFrame的左上角定位包含标签和文本字段等对象的面板 如果框架本身大于面板,则默认情况下,JPanel在页面上居中 以下是我目前的代码: public class GUI { //Constructor GUI(){ //Craete a new frame //borderlayout JFrame jfrm =

由于工作的变化,我不得不从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();
            }
        });
    }    
}
结果如下所示

我要做的是将
JPanel
的位置移到左上角,不管框架有多大,例如选项卡式页面的位置0,0。覆盖
JPanel
的居中


我使用的是
GridBaglayout
管理器。

并不是为了贬低MigLayout之类的功能,但是如果你被困在一个限制第三方库的项目中,那么知道如何操作默认布局管理器是你可以培养的最重要的技能之一

您可以简单地在布局中添加一个“填充”组件,将其设置为
weightx
weighty
属性以填充可用空间

通过改变

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

我还可以影响
标题的位置

但您也可以使用
锚定
属性来实现它。你所使用的将取决于你的需要


查看更多详细信息

GridBagLayout有点差劲。我建议改用MigLayout。它可以做GBL能做的一切,还有更多,有没有关于MIGLayout的好教程,你可以提供一个链接,只需查看主页上的快速入门指南和备忘单:你可以通过设置最后一个组件的weightx和weighty属性,使用GridBagLayout来实现这一点
gbc.gridwidth = 2; //GridBagConstraints.REMAINDER;
gbc.gridwidth = GridBagConstraints.REMAINDER;