import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;import javax.swing.*;
public class test_05 extends JFrame {
/**
 * 
 */
private static final long serialVersionUID = 1L;



public test_05() {

this.setSize(780,500); //将窗体的大小设定为780*500
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setResizable(false); //窗体可以改变大小
this.setTitle("连连看");//设置标题

JPanel toolBar = new JPanel();
toolBar.setBackground(Color.white);
toolBar.setPreferredSize(new Dimension(780,48));

JButton about = new JButton("关于");
about.setFocusPainted(false);//取消聚焦框  toolBar.add(about,"EAST");
about.setBounds(1, 1, 1, 1); 
JPanel actionPanel = new JPanel();//新建JPanel型的控件
actionPanel.setBackground(Color.yellow);//设置背景色
actionPanel.setPreferredSize(new Dimension(160,380)); //设置大小 

JPanel contentPanel = new JPanel();
contentPanel.setBackground(Color.blue);
contentPanel.setPreferredSize(new Dimension(620,380));

this.getContentPane().add(toolBar,BorderLayout.NORTH);

this.getContentPane().add(actionPanel,BorderLayout.EAST);
this.getContentPane().add(contentPanel,BorderLayout.CENTER);

}
@SuppressWarnings("deprecation")
public static void main(String[] args)throws HeadlessException {
test_05 test = new test_05();
test.show();
}

}

我在最上面的JPanel中加了一个JButton,但是现在jbutton的位置在中间,我想让他放到右边或者左边,怎么写,试了好多方法都不行,求解。

解决方案 »

  1.   

    最好的方法是用
    setLayout(null);
    然后给你的每个组件
    setBounds(x,y,w,h);
    这样的布局最自由,想放哪放哪
      

  2.   

    这些是有关于布局管理器的不使用布局管理器 自己设置位置 setLayout(null);JButton.setBounds(x,y,w,h);
      

  3.   

    我加了句
    toolBar.setLayout(new BorderLayout());
    就好了
      

  4.   

            JPanel toolBar = new JPanel();
    =>
    JToolBar toolbar = new JToolBar();
      

  5.   


    对jframe 设置grid布局 然后把jbutton放在你想放的位置
      

  6.   

    比较同意:对jframe 设置grid布局 然后把jbutton放在你想放的位置方法比较多,可以多尝试下
      

  7.   

    setLayout(null);然后用JButton这个组件调用setlocation(x1,y1,x2,y2)方法,就可以将该按钮放在任何你想放的位置了
      

  8.   

    要对布局管理器分清楚,Panel的默认布局是FlowLayout()
      

  9.   

    这个应该很简单,楼主给toolbar这个加上这段代码toolBar.setLayout(new BorderLayout());toolBar.add(about,BorderLayout.WEST);JButton就在左边了,帮你写了下在我机器上可以实现。import java.awt.*;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;import javax.swing.*;
    public class test extends JFrame {
        /**
         * 
         */
        private static final long serialVersionUID = 1L;
        
            
        
        public test() {
            
            this.setSize(780,500); //将窗体的大小设定为780*500
            this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            this.setResizable(false); //窗体可以改变大小
            this.setTitle("连连看");//设置标题
            
            JPanel toolBar = new JPanel();
            toolBar.setBackground(Color.white);
            toolBar.setPreferredSize(new Dimension(780,48));
            
            JButton about = new JButton("关于");
            about.setFocusPainted(false);//取消聚焦框 
            toolBar.setLayout(new BorderLayout());
            toolBar.add(about,BorderLayout.WEST);
            about.setBounds(1, 1, 1, 1);         
            JPanel actionPanel = new JPanel();//新建JPanel型的控件
            actionPanel.setBackground(Color.yellow);//设置背景色
            actionPanel.setPreferredSize(new Dimension(160,380)); //设置大小 
            
            JPanel contentPanel = new JPanel();
            contentPanel.setBackground(Color.blue);
            contentPanel.setPreferredSize(new Dimension(620,380));
            
            this.getContentPane().add(toolBar,BorderLayout.NORTH);
            
            this.getContentPane().add(actionPanel,BorderLayout.EAST);
            this.getContentPane().add(contentPanel,BorderLayout.CENTER);
            this.setVisible(true);
        
        }
                public static void main(String[] args)throws HeadlessException {
             new test();
            
            }
    }
      

  10.   

    import java.awt.*;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;import javax.swing.*;
    public class test extends JFrame {
        /**
         * 
         */
        private static final long serialVersionUID = 1L;
        
            
        
        public test() {
            
            this.setSize(780,500); //将窗体的大小设定为780*500
            this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            this.setResizable(false); //窗体可以改变大小
            this.setTitle("连连看");//设置标题
            
            JPanel toolBar = new JPanel();
            toolBar.setBackground(Color.white);
            toolBar.setPreferredSize(new Dimension(780,48));
            
            JButton about = new JButton("关于");
            about.setFocusPainted(false);//取消聚焦框         //加上此段代码!具体摆放位置可以看BorderLayout的API        toolBar.setLayout(new BorderLayout());
            toolBar.add(about,BorderLayout.WEST);        about.setBounds(1, 1, 1, 1);         
            JPanel actionPanel = new JPanel();//新建JPanel型的控件
            actionPanel.setBackground(Color.yellow);//设置背景色
            actionPanel.setPreferredSize(new Dimension(160,380)); //设置大小 
            
            JPanel contentPanel = new JPanel();
            contentPanel.setBackground(Color.blue);
            contentPanel.setPreferredSize(new Dimension(620,380));
            
            this.getContentPane().add(toolBar,BorderLayout.NORTH);
            
            this.getContentPane().add(actionPanel,BorderLayout.EAST);
            this.getContentPane().add(contentPanel,BorderLayout.CENTER);
            this.setVisible(true);
        
        }
                public static void main(String[] args)throws HeadlessException {
             new test();
            
            }
    }
      

  11.   

    JPanel默认为流式布局add(new JButton("按钮"),FlowLayout.LEFT);
    //或者
    setLayout(new FlowLayout(FlowLayout.LEFT,1,1));
      

  12.   

    toolBar.setLayout(new FlowLayout(FlowLayout.LEFT));
    放右面就用right