黑马程序员技术交流社区

标题: java.awt.Window 下的pack()方法到底有啥用呢?为啥这样不起效果 [打印本页]

作者: Kelvinhu    时间: 2014-4-12 15:39
标题: java.awt.Window 下的pack()方法到底有啥用呢?为啥这样不起效果
先上代码:


  1. import java.awt.*;
  2. import javax.swing.*;


  3. public class SimpleFrameTest {
  4.         public static void main(String args[]) {
  5.                 EventQueue.invokeLater(new Runnable() {
  6.                         public void run() {
  7.                                 JFrame frame = new JFrame("How to use pack?");
  8.                                 frame.add(new NotHelloWorld());
  9.                                 frame.pack();
  10.                                 frame.setVisible(true);
  11.                                 //frame.setSize(300,300);   //正常尺寸显示的是这样,我把它给注释掉。
  12.                         }
  13.                 });
  14.         }
  15. }


  16. class NotHelloWorld extends JComponent{

  17.         public void paintComponent(Graphics g){
  18.                 g.drawString("NotHelloWorld",75,100);
  19.         };
  20. }

复制代码
pack方法: 调整此窗口的大小,以适合其子组件的首选大小和布局。如果该窗口和/或其所有者还不可显示,则在计算首选大小之前都将变得可显示。在计算首选大小之后,将会验证该窗口。

那么 既然这样为什么会这样呢。。




不是说调整此窗口大小以适应布局吗?难道理解哪里的不对吗

作者: Kelvinhu    时间: 2014-4-12 15:42
难道drawString方法添加进去的不能被调整?
作者: Kelvinhu    时间: 2014-4-12 16:54
:Q别沉了。。。up下。。
作者: Kelvinhu    时间: 2014-4-12 17:33
:L再up。。。
作者: leon_hm    时间: 2014-4-12 19:23
Jframe里面得是图片才有效果,看下面这个demo
  1. package test;

  2. import java.awt.*;
  3. import javax.swing.*;


  4. public class SimpleFrameTest {
  5.         public static void main(String args[]) {
  6.                 EventQueue.invokeLater(new Runnable() {
  7.                         public void run() {
  8.                                 JFrame frame = new JFrame();
  9.                                 JLabel label = new JLabel(new ElliptIcon(380, 260, Color.red));
  10.                                 label.setLayout(new GridLayout(2, 2));
  11.                                 frame.setContentPane(label);

  12.                                 for (int i = 0; i < 4; i++) {
  13.                                     label.add(new JLabel(new ElliptIcon(100, 60, Color.blue)));
  14.                                 }

  15.                                 frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  16.                                 frame.pack();
  17.                                 frame.setVisible(true);
  18.                                 
  19.                                 
  20.                         }
  21.                 });
  22.         }
  23.         
  24.         private static class ElliptIcon implements Icon {

  25.             private int w, h;
  26.             private Color color;

  27.             public ElliptIcon(int w, int h, Color color) {
  28.                 this.w = w;
  29.                 this.h = h;
  30.                 this.color = color;
  31.             }

  32.             @Override
  33.             public void paintIcon(Component c, Graphics g, int x, int y) {
  34.                 g.setColor(color);
  35.                 g.fillOval(x, y, w, h);
  36.             }

  37.             @Override
  38.             public int getIconWidth() { return w; }

  39.             @Override
  40.             public int getIconHeight() { return h; }
  41.         }
  42. }

复制代码

作者: Kelvinhu    时间: 2014-4-12 19:25
leon_hm 发表于 2014-4-12 19:23
Jframe里面得是图片才有效果,看下面这个demo

可为啥是图片才有效果呢。。。不是说组件都可以吗。。:dizzy:不解。。




欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/) 黑马程序员IT技术论坛 X3.2