[英]JTextField not appearing in grid layout
I'm using a grid layout for one of my programs and when i try to add JTextFields to the grid It doesn't show at all. 我正在为其中一个程序使用网格布局,当我尝试将JTextFields添加到网格时,它根本不显示。 If i try adding JButtons instead of JTextFields in the same method it works perfectly.
如果我尝试以相同的方法添加JButtons而不是JTextFields,则效果很好。
package suDUKO;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.Toolkit;
import javax.swing.JTextField;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.WindowConstants;
public class Gui_Class {
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
Dimension blocksize;
private final int screenW=(int) (screenSize.getWidth()/2);
private final int screenH=(int) (screenSize.getHeight()/2);
JFrame frame;
JPanel panel;
public Gui_Class() {
frame=new JFrame("Suduko");
frame.setBounds((int) screenW-500,screenH-500,500,500);
frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
frame.setVisible(true);
panel=new JPanel();
frame.add(panel);
panel.setVisible(true);
panel.setLayout(new GridLayout(9, 9));
JTextField [][] table = new JTextField[9][9];
for(int m = 0; m < 9; m++) {
for(int n = 0; n < 9; n++) {
table[m][n]=new JTextField(m+" "+n);
table[m][n].setVisible(true);
panel.add(table[m][n]);
}
}
}
}
There are a number of problems with this code. 此代码有很多问题。 The main ones that you asked about, can be fixed by adding all the components, then calling
pack()
before finally calling setVisible(true);
您需要解决的主要问题可以通过添加所有组件,然后在最终调用
setVisible(true);
之前调用pack()
setVisible(true);
. 。
If the code as seen above does those things, the GUI won't be 500 x 500 in size, and will not be centered. 如果上述代码完成了这些任务,则GUI的大小将不会达到500 x 500,并且不会居中。 Each has it's own best approach.
每个人都有其自己的最佳方法。
Firstly, it seems you want the content pane to be square (500 x 500), and that won't happen if the frame is 500 x 500, because it has a title bar and possibly borders to render. 首先,您似乎希望内容窗格为正方形(500 x 500),如果框架为500 x 500,则不会发生这种情况,因为它具有标题栏和可能的边框。 Then centering a GUI on screen is as simple as
frame.setLocationRelativeTo(null)
. 然后将GUI居中在屏幕上就像
frame.setLocationRelativeTo(null)
一样简单。
You've already marked this correct, but was just preparing an example of what is written above, so here it is! 您已经标记为正确,但是只是准备上面所写内容的示例,所以这里就是!
import java.awt.*;
import javax.swing.*;
public class Gui_Class {
JFrame frame;
JPanel panel;
public Gui_Class() {
frame = new JFrame("Suduko");
frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
panel = new JPanel(new GridLayout(9, 9));
frame.add(panel);
//panel.setVisible(true); //unnecessary
JTextField[][] table = new JTextField[9][9];
for (int m = 0; m < 9; m++) {
for (int n = 0; n < 9; n++) {
table[m][n] = new SquareTextField(m + " " + n);
panel.add(table[m][n]);
}
}
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
public static void main(String[] args) {
Runnable r = () -> {
new Gui_Class();
};
SwingUtilities.invokeLater(r);
}
}
class SquareTextField extends JTextField {
int size = 30;
SquareTextField(String s) {
super(s);
setFont(getFont().deriveFont((float)size));
int sz = size/6;
setMargin(new Insets(sz, sz, sz, sz));
}
@Override
public Dimension getPreferredSize() {
Dimension d = super.getPreferredSize();
int w = d.width;
int h = d.height;
int max = w>h ? w : h;
return new Dimension(max, max);
}
}
You just need to add: 您只需要添加:
frame.pack()
after the loops. 循环后。
Add a frame.pack()
at the end of the constructor. 在构造函数的末尾添加一个
frame.pack()
。 That will make the layout manager to, well, layout its components. 这样,布局管理器就可以布局其组件。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.