做出来的窗口上的组件显示不出来,但是用鼠标改变一下窗口的大小后组件就出来了,我是要刷新父窗口吗,还是什么,求解
import java.awt.Component;
import java.awt.Container;
import java.awt.Graphics;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;public class CADTest extends JFrame {

JPanel doPanel;
JLabel x1,x2,x3,y1,y2,y3,xx1,xx2,xx3,yy1,yy2,yy3,original,changed,symmetry,title;
JTextField fx1,fx2,fx3,fy1,fy2,fy3,fxx1,fxx2,fxx3,fyy1,fyy2,fyy3,enlargeNum,rotationNum;
JButton enter,symmetryX,symmetryY,symmetryO,enlarge,rotation;
GridBagLayout layout;
GridBagConstraints constraints;

public CADTest() {
setTitle("CADTest");
setBounds(100,100,700,565);
//setResizable(false);
setVisible(true);

doPanel = new JPanel();

fx1 = new JTextField(3);
fx2 = new JTextField(3);
fx3 = new JTextField(3);
fy1 = new JTextField(3);
fy2 = new JTextField(3);
fy3 = new JTextField(3);
fxx1 = new JTextField(3);
fxx2 = new JTextField(3);
fxx3 = new JTextField(3);
fyy1 = new JTextField(3);
fyy2 = new JTextField(3);
fyy3 = new JTextField(3);
enlargeNum = new JTextField(7);
rotationNum = new JTextField(7);

x1 = new JLabel("x1");
x2 = new JLabel("x2");
x3 = new JLabel("x3");
y1 = new JLabel("y1");
y2 = new JLabel("y2");
y3 = new JLabel("y3");
xx1 = new JLabel("x1");
xx2 = new JLabel("x2");
xx3 = new JLabel("x3");
yy1 = new JLabel("y1");
yy2 = new JLabel("y2");
yy3 = new JLabel("y3");
original = new JLabel("原始的坐标");
changed = new JLabel("改变后的坐标");
symmetry = new JLabel("对称变换");
title = new JLabel("CDA");

enter = new JButton("确定");
symmetryX = new JButton("沿X对称");
symmetryY = new JButton("沿Y对称");
symmetryO = new JButton("沿原点对称");
enlarge = new JButton("放大倍数");
rotation = new JButton("旋转角度");

layout = new GridBagLayout();
constraints = new GridBagConstraints();

doPanel.setLayout(layout);
//doPanel.setBorder(null);

constraints.fill = GridBagConstraints.VERTICAL;
addComponent(title,0,0,14,1);
addComponent(original,1,0,1,1);
addComponent(x1,1,1,1,1);
addComponent(fx1,1,2,1,1);
addComponent(x2,1,3,1,1);
addComponent(fx2,1,4,1,1);
addComponent(x3,1,5,1,1);
addComponent(fx3,1,6,1,1);
addComponent(y1,1,7,1,1);
addComponent(fy1,1,8,1,1);
addComponent(y2,1,9,1,1);
addComponent(fy2,1,10,1,1);
addComponent(y3,1,11,1,1);
addComponent(fy3,1,12,1,1);
addComponent(enter,1,14,1,1);

addComponent(changed,2,0,1,1);
addComponent(xx1,2,1,1,1);
addComponent(fxx1,2,2,1,1);
addComponent(xx2,2,3,1,1);
addComponent(fxx2,2,4,1,1);
addComponent(xx3,2,5,1,1);
addComponent(fxx3,2,6,1,1);
addComponent(yy1,2,7,1,1);
addComponent(fyy1,2,8,1,1);
addComponent(yy2,2,9,1,1);
addComponent(fyy2,2,10,1,1);
addComponent(yy3,2,11,1,1);
addComponent(fyy3,2,12,1,1);

addComponent(symmetry,3,0,1,1);
addComponent(symmetryX,3,1,1,1);
addComponent(symmetryY,3,2,1,1);
addComponent(symmetryO,3,3,1,1);

addComponent(enlarge,4,0,1,1);
addComponent(enlargeNum,4,1,1,1);
addComponent(rotation,4,2,1,1);
addComponent(rotationNum,4,3,1,1);

doPanel.setBounds(0,0,700,130);
add(doPanel);

MyPanel p = new MyPanel();
p.setBounds(0,165,700,400);
add(p);

//his.update(this.getGraphics());
}

private void addComponent(Component component,int row,int column,int width,int height){

constraints.gridx = column;
constraints.gridy = row;
constraints.gridwidth = width;
constraints.gridheight = height;

layout.setConstraints(component, constraints);
doPanel.add(component);

}

public static void main(String[]args){
CADTest application = new CADTest();
application.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}

class MyPanel extends JPanel {

public void paintComponent(Graphics g) {
g.drawLine(0, 165, 700, 165);
g.drawLine(0, 365, 700, 365);
g.drawLine(350, 165, 350, 565);
}
}

}

解决方案 »

  1.   

    import java.awt.Component;
    import java.awt.Graphics;
    import java.awt.GridBagConstraints;
    import java.awt.GridBagLayout;
    import javax.swing.JButton;
    import javax.swing.JFrame;
    import javax.swing.JLabel;
    import javax.swing.JPanel;
    import javax.swing.JTextField;
    import javax.swing.SwingUtilities;public class CADTest extends JFrame {  JPanel doPanel;
      JLabel x1, x2, x3, y1, y2, y3, xx1, xx2, xx3, yy1, yy2, yy3, original, changed, symmetry, title;
      JTextField fx1, fx2, fx3, fy1, fy2, fy3, fxx1, fxx2, fxx3, fyy1, fyy2, fyy3, enlargeNum, rotationNum;
      JButton enter, symmetryX, symmetryY, symmetryO, enlarge, rotation;
      GridBagLayout layout;
      GridBagConstraints constraints;  public CADTest() {
        
        setTitle("CADTest");
        
        doPanel = new JPanel();
        fx1 = new JTextField(3);
        fx2 = new JTextField(3);
        fx3 = new JTextField(3);
        fy1 = new JTextField(3);
        fy2 = new JTextField(3);
        fy3 = new JTextField(3);
        fxx1 = new JTextField(3);
        fxx2 = new JTextField(3);
        fxx3 = new JTextField(3);
        fyy1 = new JTextField(3);
        fyy2 = new JTextField(3);
        fyy3 = new JTextField(3);
        enlargeNum = new JTextField(7);
        rotationNum = new JTextField(7);
        x1 = new JLabel("x1");
        x2 = new JLabel("x2");
        x3 = new JLabel("x3");
        y1 = new JLabel("y1");
        y2 = new JLabel("y2");
        y3 = new JLabel("y3");
        xx1 = new JLabel("x1");
        xx2 = new JLabel("x2");
        xx3 = new JLabel("x3");
        yy1 = new JLabel("y1");
        yy2 = new JLabel("y2");
        yy3 = new JLabel("y3");
        original = new JLabel("原始的坐标");
        changed = new JLabel("改变后的坐标");
        symmetry = new JLabel("对称变换");
        title = new JLabel("CDA");
        enter = new JButton("确定");
        symmetryX = new JButton("沿X对称");
        symmetryY = new JButton("沿Y对称");
        symmetryO = new JButton("沿原点对称");
        enlarge = new JButton("放大倍数");
        rotation = new JButton("旋转角度");
        layout = new GridBagLayout();
        constraints = new GridBagConstraints();
        doPanel.setLayout(layout);
        constraints.fill = GridBagConstraints.VERTICAL;
        addComponent(title, 0, 0, 14, 1);
        addComponent(original, 1, 0, 1, 1);
        addComponent(x1, 1, 1, 1, 1);
        addComponent(fx1, 1, 2, 1, 1);
        addComponent(x2, 1, 3, 1, 1);
        addComponent(fx2, 1, 4, 1, 1);
        addComponent(x3, 1, 5, 1, 1);
        addComponent(fx3, 1, 6, 1, 1);
        addComponent(y1, 1, 7, 1, 1);
        addComponent(fy1, 1, 8, 1, 1);
        addComponent(y2, 1, 9, 1, 1);
        addComponent(fy2, 1, 10, 1, 1);
        addComponent(y3, 1, 11, 1, 1);
        addComponent(fy3, 1, 12, 1, 1);
        addComponent(enter, 1, 14, 1, 1);
        addComponent(changed, 2, 0, 1, 1);
        addComponent(xx1, 2, 1, 1, 1);
        addComponent(fxx1, 2, 2, 1, 1);
        addComponent(xx2, 2, 3, 1, 1);
        addComponent(fxx2, 2, 4, 1, 1);
        addComponent(xx3, 2, 5, 1, 1);
        addComponent(fxx3, 2, 6, 1, 1);
        addComponent(yy1, 2, 7, 1, 1);
        addComponent(fyy1, 2, 8, 1, 1);
        addComponent(yy2, 2, 9, 1, 1);
        addComponent(fyy2, 2, 10, 1, 1);
        addComponent(yy3, 2, 11, 1, 1);
        addComponent(fyy3, 2, 12, 1, 1);
        addComponent(symmetry, 3, 0, 1, 1);
        addComponent(symmetryX, 3, 1, 1, 1);
        addComponent(symmetryY, 3, 2, 1, 1);
        addComponent(symmetryO, 3, 3, 1, 1);
        addComponent(enlarge, 4, 0, 1, 1);
        addComponent(enlargeNum, 4, 1, 1, 1);
        addComponent(rotation, 4, 2, 1, 1);
        addComponent(rotationNum, 4, 3, 1, 1);
        doPanel.setBounds(0, 0, 700, 130);
        add(doPanel);
        MyPanel p = new MyPanel();
        p.setBounds(0, 165, 700, 400);
        add(p);
        
        setSize(700, 565);
      }  private void addComponent(Component component, int row, int column, int width, int height) {
        constraints.gridx = column;
        constraints.gridy = row;
        constraints.gridwidth = width;
        constraints.gridheight = height;
        layout.setConstraints(component, constraints);
        doPanel.add(component);
      }  public static void main(String[] args) {
        
        SwingUtilities.invokeLater(new Runnable() {      @Override
          public void run() {
            
            CADTest application = new CADTest();
            application.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            application.setLocationRelativeTo(null);
            application.setVisible(true);
          }
        });
      }  class MyPanel extends JPanel {    @Override
        public void paintComponent(Graphics g) {
          
          g.drawLine(0, 165, 700, 165);
          g.drawLine(0, 365, 700, 365);
          g.drawLine(350, 165, 350, 565);
        }
      }
    }
      

  2.   

    把 setVisible(true) 放到添加控件后面,因为添加控件并不引发重绘你改变窗口大小,引发了重绘,所以控件显示出来了。如果非要在 setVisible(true) 后添加控件,添加完成以后调用getContentPane().invalidate();也是可以的另外所有的swing程序的mian方法都应该写成 5楼 那样。