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的位置在中间,我想让他放到右边或者左边,怎么写,试了好多方法都不行,求解。
setLayout(null);
然后给你的每个组件
setBounds(x,y,w,h);
这样的布局最自由,想放哪放哪
toolBar.setLayout(new BorderLayout());
就好了
=>
JToolBar toolbar = new JToolBar();
对jframe 设置grid布局 然后把jbutton放在你想放的位置
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();
}
}
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();
}
}
//或者
setLayout(new FlowLayout(FlowLayout.LEFT,1,1));
放右面就用right