【发布时间】:2016-02-29 23:51:30
【问题描述】:
所以我正在使用外部和内部类制作示例 GUI。我做了两个内部类。首先是“第一个面板”的父类,第二个是我创建了一个子类,它是“第二个面板”,我使用 GridBagLayout 添加了 JButton。我的问题是它不会移动到我想要的位置。我分配了我的 gridx = 2 和 gridy = 1。但它不会移动。任何帮助将不胜感激!
public class Login extends JFrame{
mainPanel mainpanel = new mainPanel(); // I create a class object for mainPanel so I can set as ContentPane.
//Constructor
public Login(){
setSize(500,400);
setTitle("Login Sample");
setVisible(true);
setLocationRelativeTo(null);
getContentPane().add(mainpanel);
//Window Listener
addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e){
System.exit(0);
}//window Closing
});
}
class mainPanel extends JPanel { //InnerClass
firstPanel firstpanel = new firstPanel();
//Constructor
public mainPanel(){
setPreferredSize(new Dimension (500,400));
setLayout(new BorderLayout());
setBorder(BorderFactory.createLineBorder(Color.green, 3));
add(firstpanel);
}
class firstPanel extends JPanel{
//Create Button
JButton loginButton = new JButton("Login");
//Constraints
GridBagConstraints loginConstraints = new GridBagConstraints();
public firstPanel(){
setLayout(new GridBagLayout());
loginConstraints.gridx = 1;
loginConstraints.gridy = 2;
add(loginButton,loginConstraints);
}
}
【问题讨论】:
-
您还没有指定您希望按钮在什么位置,或者它现在在什么位置。我的猜测是,如果您在 BorderLayout 的中间只有一个对象,它就会在中间居中。
-
我已经使用 FIRST_LINE_START 指定了位置,但它没有移动到左上角。我错过了什么?谢谢。
-
@MiaLegaspi 每个发布的问题您只能接受一个解决方案。您可能会接受对您最有帮助的解决方案。
-
@user3437460 哦,我明白了。谢谢你的提示我会保留这个:)
标签: java swing user-interface