简体   繁体   English

Swing…添加JTextField使我的JPanel消失

[英]Swing…Adding JTextField blanks out my JPanel

Having a strange problem. 有一个奇怪的问题。 I'm learning Java Swing right now, and have a basic frame with a set size. 我现在正在学习Java Swing,并且具有一个固定大小的基本框架。 I added buttons, and then radio buttons, and everything was fine. 我添加了按钮,然后添加了单选按钮,一切都很好。 I add a JTextField and everything goes blank on the Frame until I manually resize it and then everything appears. 我添加了一个JTextField,并且在Frame上一切变为空白,直到我手动调整它的大小,然后一切都出现了。 I've tried messing around with Grids and GridBagConstraints and nothing seems to be helping, but I don't have a very good handle on working with those just yet. 我已经尝试过使用Grids和GridBagConstraints,似乎没有任何帮助,但是我现在还没有很好的处理方法。 Everything is sized correctly I believe, but the box opens up blank and then when I change the size even by a single pixel in any direction, everything shows up fine. 我相信所有尺寸的大小都正确,但是该框会打开空白,然后当我在任何方向上甚至将单个像素更改尺寸时,一切都可以正常显示。

Here is my code so far: 到目前为止,这是我的代码:

import javax.swing.*;

import java.awt.*;

public class Test {

    private JFrame f; //frame
    private JPanel p; //window
    private JButton b1; //button
    private JButton b2; //button
    private JButton b3; //button
    private JButton b4; //button
    private JRadioButton rad1;
    private JRadioButton rad2;
    private String radBut1 = "Checking";
    private String radBut2 = "Savings";
    private JTextField textField;


public Test(){

    gui();

}

public void gui(){      

    f = new JFrame("ATM Machine");
    f.setVisible(true);
    f.setSize(350,200);
    f.setLocationRelativeTo(null);
    //f.setResizable(false);
    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    p = new JPanel();

    b1 = new JButton("Withdraw From");
    b2 = new JButton("Deposit To");
    b3 = new JButton("Transfer To");
    b4 = new JButton("Balance Of");

    b1.setPreferredSize(new Dimension(150,25));
    b2.setPreferredSize(new Dimension(150,25));
    b3.setPreferredSize(new Dimension(150,25));
    b4.setPreferredSize(new Dimension(150,25));

    p.add(b1);
    p.add(b2);
    p.add(b3);
    p.add(b4);

    rad1 = new JRadioButton(radBut1);
    rad2 = new JRadioButton(radBut2);






    ButtonGroup radioGroup = new ButtonGroup();
    radioGroup.add(rad1);
    radioGroup.add(rad2);

    p.add(rad1);
    p.add(rad2);

    textField = new JTextField(25);

    p.add(textField);


    f.add(p);




}

public static void main(String[] args) {

    new Test();


}

}

So basically it needs to look like 2 buttons on top, 2 buttons underneath those buttons, 2 radio buttons under those, and then finally the TextField under the radio buttons. 因此,基本上,它需要看起来像是顶部的2个按钮,这些按钮下方的2个按钮,这些按钮下方的2个单选按钮,最后是单选按钮下方的TextField。

I would appreciate any help. 我将不胜感激任何帮助。

Thanks! 谢谢!

Components should be added to the frame BEFORE making the frame visible. 在使框架可见之前,应将组件添加到框架。

f = new JFrame("ATM Machine");
//f.setVisible(true);
...
f.add(p);
f.setVisible(true);

and have a basic frame with a set size. 并具有设定尺寸的基本框架。

Don't set the size of the frame. 不要设置框架的大小。 Let Swing determine the size of each component and the size of the frame. 让Swing确定每个组件的大小和框架的大小。 So really the code should be: 所以实际上代码应该是:

f.add(p);
f.pack();
f.setVisible(true);

Don't set the preferredSize() of your buttons. 不要设置按钮的preferredSize()。 If you want all the buttons to be the same size, then use a GridLayout on a separate panel: 如果希望所有按钮的大小都相同,请在单独的面板上使用GridLayout:

JPanel buttons = new JPanel( new GridLayout(0, 4) );
buttons.add(b1);
buttons.add(b2);
...

Then you add the buttons panel to the frame. 然后,将按钮面板添加到框架。

Also, you need to change the layout manager. 另外,您需要更改布局管理器。 Right now you are using a FlowLayout and the layout only appears to work correctly. 现在,您正在使用FlowLayout,而布局似乎只能正常工作。 If you resize the frame components will flow to another line. 如果您调整框架大小,组件将流到另一行。 Maybe you need to use a BoxLayout . 也许您需要使用BoxLayout

Read the Swing tutorial on Using Layout Manager for more information. 阅读有关使用布局管理器的Swing教程以获取更多信息。

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

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