JFrame里控件不容易显示 刚开始运行程序时,只是一个空白的框架,非要稍微改变下框架大小或者最大化最小化一下里面的控件(有JLabel,JTextField,JTable,JButton)才完全显示,请问该如何解决 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 可能是你没有调用JFrame的pack()或者valiator()这样的方法你添加以下执行这些方法试试,如果还不行你把代码贴出来给你解决 还是不行 构造方法是这样的MyFrame(String s) { this.setTitle(s); this.setLayout(new BorderLayout()); this.setBounds(500, 100, 300, 455); this.setVisible(true); 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(); } 我知道你代码的问题了,你把构造方法改成下面的试试你调用setVisible(true)方法太早了,应该是最后一行调用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);放构造方法最后一句试试 不过为什么放在最后一行就可以了呢,setvisible()难道不是简单的整体框架设为可见,对控件也有影响 怎么替换字符串 用java写小游戏 怎么移动物体更灵敏 学java用什么编译器好??? 帮忙看下这个程序有什么问题 一个头痛的问题? 新手请教一个SWT问题,请不吝赐教! 新人问题 从此以后每天装JDK30次! 菜鸟问题-如何从一个frame调用一个Panel? ProgressMonitorInputStream的问题 请教关于换行\n(新手上路) io流截取视频的问题
你添加以下执行这些方法试试,如果还不行你把代码贴出来给你解决
还是不行 构造方法是这样的MyFrame(String s) {
this.setTitle(s);
this.setLayout(new BorderLayout());
this.setBounds(500, 100, 300, 455);
this.setVisible(true);
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();
}
你调用setVisible(true)方法太早了,应该是最后一行调用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);
}
不过为什么放在最后一行就可以了呢,setvisible()难道不是简单的整体框架设为可见,对控件也有影响