在JFrame里 for循环添加控件,如何每次都能让JFrame调整刚好的大小
publicmyProduct(StringStr,inty)其中y是输入的控件数量。如何能让JFrame根据y值调整自身到刚好的大小呢??代码片段:setAlwaysO...
public myProduct(String Str,int y)
其中y是输入的控件数量。如何能让JFrame根据y值调整自身到刚好的大小呢??
代码片段:
setAlwaysOnTop(true);
addWindowFocusListener(new myWindowsFouceListener());
setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
setTitle(Str);
JPanel[] panel=new JPanel[y];
productTF=new JTextField[y];
JLabel[] label=new JLabel[y];
for(int i=0;i<y;i++){
panel[i]=new JPanel();
label[i]=new JLabel(String.valueOf(i+1)+".");
productTF[i]=new JTextField("",10);
productTF[i].setDocument(new LimitedDocument(40));// 文本框允许长度为40
if(i==y-1){
panel[i].setFocusTraversalKeys(KeyboardFocusManager.FORWARD_TRAVERSAL_KEYS, Collections.EMPTY_SET);
panel[i].setFocusTraversalKeys(KeyboardFocusManager.BACKWARD_TRAVERSAL_KEYS, Collections.EMPTY_SET);
productTF[i].addKeyListener(new myKeyListener());
}
panel[i].add(label[i]);
panel[i].add(productTF[i]);
getContentPane().add(panel[i]);
}
setResizable(false);
setVisible(true); 展开
其中y是输入的控件数量。如何能让JFrame根据y值调整自身到刚好的大小呢??
代码片段:
setAlwaysOnTop(true);
addWindowFocusListener(new myWindowsFouceListener());
setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
setTitle(Str);
JPanel[] panel=new JPanel[y];
productTF=new JTextField[y];
JLabel[] label=new JLabel[y];
for(int i=0;i<y;i++){
panel[i]=new JPanel();
label[i]=new JLabel(String.valueOf(i+1)+".");
productTF[i]=new JTextField("",10);
productTF[i].setDocument(new LimitedDocument(40));// 文本框允许长度为40
if(i==y-1){
panel[i].setFocusTraversalKeys(KeyboardFocusManager.FORWARD_TRAVERSAL_KEYS, Collections.EMPTY_SET);
panel[i].setFocusTraversalKeys(KeyboardFocusManager.BACKWARD_TRAVERSAL_KEYS, Collections.EMPTY_SET);
productTF[i].addKeyListener(new myKeyListener());
}
panel[i].add(label[i]);
panel[i].add(productTF[i]);
getContentPane().add(panel[i]);
}
setResizable(false);
setVisible(true); 展开
2个回答
展开全部
// Center the window
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
Dimension frameSize = frame.getSize();
if (frameSize.height > screenSize.height) {
frameSize.height = screenSize.height;
}
if (frameSize.width > screenSize.width) {
frameSize.width = screenSize.width;
}
frame.setLocation((screenSize.width - frameSize.width) / 2,
(screenSize.height - frameSize.height) / 2);
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
Dimension frameSize = frame.getSize();
if (frameSize.height > screenSize.height) {
frameSize.height = screenSize.height;
}
if (frameSize.width > screenSize.width) {
frameSize.width = screenSize.width;
}
frame.setLocation((screenSize.width - frameSize.width) / 2,
(screenSize.height - frameSize.height) / 2);
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询