刚开始运行程序时,只是一个空白的框架,非要稍微改变下框架大小或者最大化最小化一下里面的控件(有JLabel,JTextField,JTable,JButton)才完全显示,请问该如何解决。。。
------解决方案--------------------
我知道你代码的问题了,你把构造方法改成下面的试试
你调用setVisible(true)方法太早了,应该是最后一行调用
- Java code
MyFrame(String s) { this.setTitle(s);this.setLayout(new BorderLayout());this.setBounds(500, 100, 300, 455);JPanel p1 = new JPanel(new FlowLayout());JPanel p3 = new JPanel(new GridLayout(3,1));JLabel lb = new JLabel("索引 :");lb.setFont(new Font("黑体",Font.BOLD ,15));tf = new JTextField(18);p1.add(lb);p1.add(tf);this.add(p1,BorderLayout.NORTH);this.initialize();ta = new JTable(model);ta.setRowSelectionInterval(0, 0);DefaultTableCellRenderer r = new DefaultTableCellRenderer();r.setHorizontalAlignment(JLabel.CENTER);ta.setDefaultRenderer(Object.class, r);ta.setAutoCreateRowSorter(true);ta.setBackground(Color.PINK);sp = new JScrollPane();sp.getViewport().add(ta);this.add(sp,BorderLayout.CENTER);JButton b1 = new JButton("增 加");b1.setFont(new Font("华文行楷",Font.PLAIN,20));JButton b2 = new JButton("删 除");b2.setFont(new Font("华文行楷",Font.PLAIN,20));JButton b3 = new JButton("修 改");b3.setFont(new Font("华文行楷",Font.PLAIN,20));p3.add(b1);p3.add(b2);p3.add(b3);this.add(p3,BorderLayout.SOUTH);b1.addMouseListener(new MouseListener());b2.addMouseListener(new MouseListener2());b3.addMouseListener(new MouseListener3());this.addWindowListener(new windowMonitor());ThreadMonitor t = new ThreadMonitor();t.run();this.setIconImage(img);this.validate();this.setVisible(true);}
------解决方案--------------------
this.setVisible(true);
放构造方法最后一句试试