Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/370.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 按钮的Jtabbedpane布局不工作_Java_Swing_Layout Manager - Fatal编程技术网

Java 按钮的Jtabbedpane布局不工作

Java 按钮的Jtabbedpane布局不工作,java,swing,layout-manager,Java,Swing,Layout Manager,下面使用tabbedpane的swing界面工作正常,直到我为按钮设置了布局,也就是说,当我将内容窗格loginpage设置为null(loginpage.setlayout(null))时,按钮从窗格中消失,但当我用textfield或textarea替换按钮时,按钮工作正常 package atmg; import java.awt.CardLayout; import java.awt.event.ActionEvent; import java.awt.event.ActionList

下面使用tabbedpane的swing界面工作正常,直到我为按钮设置了布局,也就是说,当我将内容窗格loginpage设置为null(loginpage.setlayout(null))时,按钮从窗格中消失,但当我用textfield或textarea替换按钮时,按钮工作正常

package atmg;

import java.awt.CardLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTabbedPane;

public class newgui extends JFrame {

    /**
     * 
     */
    private static final long serialVersionUID = 1L;
    public JPanel clickfn= new JPanel(),gui= new JPanel(),trasgui= new JPanel(),contentpanel = new JPanel();
    public static JTabbedPane Tabs = new JTabbedPane();
    public JButton loginpage, filloginfo,createlogin ;
    public JLabel label1 ,label2;
    private CardLayout cardlayout = new CardLayout();
    //static JPanel panel1 = new JPanel();
    newguilogin nw;

    public static void main(String[] args) {
        newgui tf = new newgui();
        tf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        tf.setSize(700,700);
        tf.setVisible(true);
        tf.setLocation(400,20);
    }

    public newgui(int a)
    {
        System.out.println(a);
    }
    public newgui() {
        super("ATM");
        Initialize();
    }

    public void Initialize()
    {

        nw= new newguilogin();
        nw.setgui(this);
        loginpage = new JButton("Go to loginpage");
        filloginfo = new JButton("go to fill log info");
        createlogin = new JButton("create a new user");
        // TODO Auto-generated constructor stub



        actionListener a1 = new actionListener(); 

        loginpage.addActionListener(a1);
        filloginfo.addActionListener(a1);
        createlogin.addActionListener(a1);


        clickfn.add(loginpage);
        clickfn.setSize(20,20);
        clickfn.setLocation(50,50);
        clickfn.add(filloginfo);


        contentpanel.setLayout(cardlayout);


        contentpanel.add(Tabs, "tab");
        Tabs.add(clickfn,"panel1");
        //Tabs.add(trasgui,"panel3");

        this.setContentPane(contentpanel);

        cardlayout.show(contentpanel, "tab");
    }
    public class actionListener implements ActionListener
    {


        @Override
        public void actionPerformed(ActionEvent e) {
            // TODO Auto-generated method stub
            JButton src = (JButton)e.getSource();
            if(src.equals(loginpage))
                //threader = new Threading();

                //Tabs.addTab("panel2", gui);
                nw = new newguilogin();
                nw.initialize();



        }
    }
}
当我将内容窗格的loginpage设置为null(loginpage.setlayout(null))时,按钮消失

Swing布局遵循的一个好的经验法则是:永远不要使用
null
布局


虽然空布局和
setBounds()
可能会像创建复杂GUI的最简单和最好的方式一样吸引新手,但您创建的GUI越多,在使用它们时遇到的困难就越严重。当GUI调整大小时,它们不会调整您的组件的大小,它们是一个需要增强或维护的皇家女巫,当它们放置在滚动窗格中时会完全失败,当在所有平台或屏幕分辨率与原始分辨率不同的情况下查看时,它们看起来非常糟糕


还要了解,您可以嵌套JPanel,每个都使用自己的简单布局,从而使用简单布局管理器创建复杂的GUI

不要使用空布局。Swing设计用于布局管理器。但我的大多数设计都使用了窗格的null布局,我会尝试使用网格或任何其他布局管理器,而null布局和
setBounds()
可能会像创建复杂GUI的最简单和最好的方法一样吸引新手,您创建的Swing GUI越多,在使用它们时遇到的困难就越严重。当GUI调整大小时,它们不会调整您的组件的大小,它们是一个需要增强或维护的皇家女巫,当放置在滚动窗格中时,它们会完全失败,在所有平台上查看时,它们看起来非常笨拙,或者屏幕分辨率与原来的不同。还要了解,您可以嵌套JPanel,每个都使用自己的简单布局,将布局管理器更改为Gridbaglayout,并且工作正常,如果我们能给我举个例子,我是否能够将scrollpane添加到选项卡窗格中