为什么我的JButton中只有1个显示? [英] Why is only 1 of my JButtons showing?
问题描述
我不明白为什么只有登录现有帐户"是唯一显示的按钮.我只需要显示2个按钮,即使我将其可见性设置为true并移动了它,也不会显示创建帐户",因此它与登录现有帐户"不重叠.
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".
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();
}
}
推荐答案
许多问题:
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);
JFrame的内容窗格的默认布局管理器是BorderLayout,因此不需要此代码.
The default layout manager for the content pane of a JFrame is a BorderLayout, so this code is unnecessary.
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.
此外,JPanel的默认布局管理器是FlowLayout.因此,您不能只是一个BorderLayout约束而期望它起作用.
Also, the default layout manager for a JPanel is the FlowLayout. So you can't just a BorderLayout constraint and expect it to work.
loginPanel.setVisible(true);
registerPanel.setVisible(true);
默认情况下,所有Swing组件(JFrame,JDialog等除外)都是可见的,因此不需要上面的代码.
All Swing component (except JFrame, JDialog etc) are visible by default, so the above code is unnecessary.
mainWindow.add(login);
mainWindow.add(register);
mainWindow.add(loginPanel);
mainWindow.add(registerPanel);
如前所述,框架的默认布局是BorderLayout.如果未指定约束,则组件将转到"CENTER".但是一次只能在中心显示一个组件.
As mentioned earlier the default layout for the frame is a BorderLayout. If you don't specify a constraint, then the component goes to the "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.
我建议您阅读 Layout Manager上的Swing教程中的这一节. 的工作示例,向您提供使用每个布局管理器的基础知识.
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.
这篇关于为什么我的JButton中只有1个显示?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!