Having a strange problem. I'm learning Java Swing right now, and have a basic frame with a set size. 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. 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. 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.
有一个奇怪的问题。我现在正在学习Java Swing,并且有一个具有设置大小的基本框架。我添加了按钮,然后是单选按钮,一切都很好。我添加了一个JTextField,所有的东西都在这个框架上空白,直到我手动调整它的大小,然后一切都出现了。我尝试过用网格和网格约束来解决问题,似乎没有什么帮助,但是我还没有很好的处理这些问题。我相信所有的东西都是正确的,但是盒子是空白的,当我改变大小的时候,哪怕是一个像素的方向,一切都会好起来的。
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.
基本上它需要看起来像两个按钮在上面,两个按钮在这些按钮下面,两个在这些按钮下面,最后是在单选按钮下的TextField。
I would appreciate any help.
我很感激你的帮助。
Thanks!
谢谢!
1 个解决方案
#1
2
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. So really the code should be:
不要设置框架的大小。让Swing确定每个组件的大小和框架的大小。所以代码应该是:
f.add(p);
f.pack();
f.setVisible(true);
Don't set the preferredSize() of your buttons. 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. If you resize the frame components will flow to another line. Maybe you need to use a BoxLayout
.
另外,您需要更改布局管理器。现在您正在使用一个流布局,而布局只显示正确工作。如果您调整框架组件的大小,则会流向另一行。也许你需要使用一个方框布局。
Read the Swing tutorial on Using Layout Manager for more information.
阅读Swing教程,使用布局管理器获取更多信息。
#1
2
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. So really the code should be:
不要设置框架的大小。让Swing确定每个组件的大小和框架的大小。所以代码应该是:
f.add(p);
f.pack();
f.setVisible(true);
Don't set the preferredSize() of your buttons. 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. If you resize the frame components will flow to another line. Maybe you need to use a BoxLayout
.
另外,您需要更改布局管理器。现在您正在使用一个流布局,而布局只显示正确工作。如果您调整框架组件的大小,则会流向另一行。也许你需要使用一个方框布局。
Read the Swing tutorial on Using Layout Manager for more information.
阅读Swing教程,使用布局管理器获取更多信息。