创建了一个JFrame子类,setBounds()无论设置什么值,位置都是在屏幕左上角。setLocation()也是这样怎么回事?

解决方案 »

  1.   

    import javax.swing.JFrame;public class FrameLocate extends JFrame { /**
     * @param args
     */
    FrameLocate(String name){
    super(name);
    }
    public static void main(String[] args) {
    // TODO Auto-generated method stub
    FrameLocate fl = new FrameLocate("Myframe");
    fl.setBounds(100,200,300,400);
    fl.setLocation(100,200);
    fl.setVisible(true);
    }}
      

  2.   

    你只要在上面加一句:this.getContentPane.setLayout(null);   
      只要把布局器取消掉就有用,   
      这个问题也许是容器本身的布局影响 
      

  3.   

    你的代码在我机器上运行是没有问题的 可以随着setLocation 值改变你是什么 系统
      

  4.   

    package booksystem;
    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.*;public class Mainwindow extends JFrame {
    public Mainwindow() {
    super ("图书管理系统");
    setSize(500,500);
    setBackground(Color.white);
    setVisible(true);
    pack();
    Container con = getContentPane();
    JPanel panel = new JPanel();
    JButton button1 = new JButton("借出");
    JButton button2 = new JButton("归还");
    JButton button3 = new JButton("关于");
    JLabel label = new JLabel(new ImageIcon("booksystem/mm.jpg"));

    //create Query menu
    JMenu query = new JMenu("书籍查询");

    //create Login menu
    JMenu login = new JMenu("用户登陆");

    //create System management menu
    JMenu management = new JMenu("系统管理");

    //create Students' function menu
    JMenu stuFunction = new JMenu("学生功能");

    //create Help function menu
    JMenu help = new JMenu("帮助");

    //create menubar
    JMenuBar menubar = new JMenuBar();

    //set menu to the menubar
    menubar.add(query);
    menubar.add(login);
    menubar.add(management);
    menubar.add(stuFunction);
    menubar.add(help);

    //set Mainwindow
    setBounds(100,50,500,500);
    menubar.setBounds(0,0,1024,20);
    con.add(menubar);
    panel.setBounds(0,0,500,500);
    con.add(panel);
    panel.setLayout(null);
    button1.setBounds(5,25,60,30);
    panel.add(button1);
    button2.setBounds(70,25,60,30);
    panel.add(button2);
    button3.setBounds(135,25,60,30);
    panel.add(button3);
    label.setBounds(0,60,338,450);
    panel.add(label);
    validate();
    }
    }这是我的代码,上面this.getContentPane.setLayout(null);   我试了,好像不管用各位帮忙看看setBounds的前两个参数就是设定坐标位置的话,同样的对象用setLocation还有什么用?搞不清楚
      

  5.   

    setBounds的前两个参数是坐标位置,后两个是大小。setLocation只是设置位置。
    把你的程序里面的setVisible(true)挪到最后去试试
      

  6.   

    我自己写的却不行,haisenmai的确实可以