简体   繁体   English

为什么只有1个JButton显示?

[英]Why is only 1 of my JButtons showing?

I don't understand why only "Log in to existing account" is the only button to show. 我不明白为什么只有“登录现有帐户”是显示的唯一按钮。 All I want is 2 buttons to show and "Create account" isn't showing even though I set its visibility to true and moved it so it isn't overlapping with "Login to existing account". 我只需要显示2个按钮,即使我将其可见性设置为true并移动了它,也不会显示“创建帐户”,因此它与“登录现有帐户”没有重叠。

public class HotelBookingSystem extends JFrame
{
Container con;
public HotelBookingSystem()
{
    super("Booking System");
    JFrame mainWindow = new JFrame("Booking");
    mainWindow.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    mainWindow.setSize(350,400);
    mainWindow.setVisible(true);

    con = getContentPane();
    BorderLayout myLayout = new BorderLayout();
    con.setLayout(myLayout);

    JButton login = new JButton ("Login to existing account");
    JButton register = new JButton ("Create Account");
    JPanel loginPanel = new JPanel();
    JPanel registerPanel = new JPanel();

    loginPanel.add(login, BorderLayout.NORTH);
    registerPanel.add(register, BorderLayout.SOUTH);
    login.setSize(350, 100);
    register.setSize(350, 100);

    loginPanel.setVisible(true);
    registerPanel.setVisible(true);

    mainWindow.add(login);
    mainWindow.add(register);
    mainWindow.add(loginPanel);
    mainWindow.add(registerPanel);

    login.addActionListener(new ActionListener()
        {
            public void actionPerformed(ActionEvent e)
            {
              mainWindow.setVisible(false);     
            }
        });

    register.addActionListener(new ActionListener()
        {
            public void actionPerformed(ActionEvent e)
            {
              mainWindow.setVisible(false);     
            }
        });
}

public static void main(String[] args) 
{
    HotelBookingSystem dataBaseAccess = new HotelBookingSystem();
}

} }

Have you tried using a different layout for the panels containing the JButtons? 您是否尝试过对包含JButton的面板使用其他布局?

Since you have two different panels, one button is on the very top of loginPanel, and the other button is on the very bottom of registerPanel. 由于您有两个不同的面板,因此一个按钮位于loginPanel的最顶部,另一个按钮位于registerPanel的最底部。

Instead, try putting them in the same panel, were one is in the NORTH and the other is in the CENTER. 相反,请尝试将它们放置在同一面板中,一个位于北方,另一个位于CENTER。

Many problems: 很多问题:

mainWindow.setVisible(true);

The frame should be made visible AFTER all the components are added to the frame, so this should be the last statement in the constructor. 在将所有组件添加到框架之后,应该使该框架可见,因此这应该是构造函数中的最后一条语句。

con = getContentPane();
BorderLayout myLayout = new BorderLayout();
con.setLayout(myLayout);

The default layout manager for the content pane of a JFrame is a BorderLayout, so this code is unnecessary. JFrame内容窗格的默认布局管理器是BorderLayout,因此不需要此代码。

login.setSize(350, 100);
register.setSize(350, 100);

Don't try to set the size of a component. 不要尝试设置组件的大小。 It is the job of the layout manager to set the size and location of each component. 布局管理器的工作是设置每个组件的大小和位置。

JPanel loginPanel = new JPanel();
JPanel registerPanel = new JPanel();

loginPanel.add(login, BorderLayout.NORTH);
registerPanel.add(register, BorderLayout.SOUTH);

Why are you creating two panels? 为什么要创建两个面板? You can just add the buttons directly to the frame. 您可以直接将按钮添加到框架中。

Also, the default layout manager for a JPanel is the FlowLayout. 另外,JPanel的默认布局管理器是FlowLayout。 So you can't just a BorderLayout constraint and expect it to work. 因此,您不能只是一个BorderLayout约束而期望它起作用。

loginPanel.setVisible(true);
registerPanel.setVisible(true);

All Swing component (except JFrame, JDialog etc) are visible by default, so the above code is unnecessary. 默认情况下,所有Swing组件(JFrame,JDialog等除外)都是可见的,因此不需要上面的代码。

mainWindow.add(login);
mainWindow.add(register);
mainWindow.add(loginPanel);
mainWindow.add(registerPanel);

As mentioned earlier the default layout for the frame is a BorderLayout. 如前所述,框架的默认布局是BorderLayout。 If you don't specify a constraint, then the component goes to the "CENTER". 如果未指定约束,则组件将转到“ CENTER”。 But only a single component can be displayed in the center at one time. 但是一次只能在中心显示一个组件。

Fix all the other problems and then try something like: 解决所有其他问题,然后尝试执行以下操作:

mainWindow.add(login, BorderLayout.NORTH);
mainWindow.add(register, BorderLayout.SOUTH);
mainWindow.add(loginPanel, BorderLayout.WEST);
mainWindow.add(registerPanel, BorderLayout.EAST);

to see the difference. 看看区别。 Adjust the constraints as required. 根据需要调整约束。

I suggest you read the section from the Swing tutorial on Layout Manager for working examples to give you the basics of using each of the layout managers. 我建议您阅读Layout Manager上Swing教程中的部分,以获取工作示例,以为您提供使用每种布局管理器的基础知识。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM