通过Netbeans创建GUI

时间:2013-07-26 16:07:09

标签: java user-interface netbeans

我已经通过Netbeans Java创建了一个GUI,但每当我最大化GUI窗口时,文本框就会变得不对齐。我使用Netbeans拖放功能来创建GUI。我想知道为什么每当我最大化GUI时文本框都会不对齐

2 个答案:

答案 0 :(得分:1)

这是布局的问题。

阅读使用布局:http://docs.oracle.com/javase/tutorial/uiswing/layout/visual.html

如果您打算使用Netbean的GUI构建器,请运行教程。它将帮助您更好地了解构建器的使用方式。 https://netbeans.org/kb/docs/java/quickstart-gui.html

答案 1 :(得分:1)

我理解你的问题,因为我已经遇到了同样的问题,我的建议是你可以使用netbeans的拖放,但在使用它之前先制作一个面板,然后在该面板中删除你所有的东西,然后使用JFrame组件重新调整大小的功能只需将面板放在您想要放置的位置,例如,如果您想要对齐它。

//if your class is extending JFrame
public static int getWIDTH(){
    return WIDTH;
}
public static int getHEIGHT() {
    return HEIGHT;
}
private void formComponentResized(java.awt.event.ComponentEvent evt) {
    // this is for setting panel in the middle of the JFrame horizontally
    int a = getWIDTH();
    int b = Panel.getWidth();
    a = a/2;
    b = b/2;
    int centerForX = b - a;
    // This is for setting the panel in the middle vertically
    int x = getHEIGHT();
    int y = Panel.getHeight();
    x = x/2;
    y = y/2;
    int centerForY = x - y;
    // Making a 'Point' object and then setting location of the Panel.
    Point p = new Point(centerForX , centerForY);
    Panel.setLocation(p);
}

一些要点:
1)getWIDTH和getHEIGHT方法将由netbeans在所有方法之外写入,但在类中getwidth和getheight并按ctrl + space并按Enter键将分别生成getWIDTH和getHEIGHT方法。
2)formbeonentResized方法将由netbeans生成,只需转到设计选项卡,然后在导航器中,大部分在左下角右键单击JFrame,然后转到事件然后组件然后是componentResized。
3)如果您有任何疑问,请不要犹豫,因为我对此主题有详细的了解。

donot forget this step otherwise whole help will be of no use

相关问题