import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;import javax.swing.ButtonGroup;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
public class SelectPosition extends JFrame{
 private final BorderLayout borderLayout = new BorderLayout();
 private final JPanel panel = new JPanel();
 private final JLabel label = new JLabel();
public static void main(String[] args) {
SelectPosition frame = new SelectPosition();
frame.setVisible(true);
}

public SelectPosition() {
super();
setTitle("控制内容的对齐方式 ");
setBounds(100, 100, 500, 375);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);


panel.setLayout(borderLayout);
getContentPane().add(panel, BorderLayout.NORTH);


label.setText("请注意我的位置啊!");

JPanel southPanel = new JPanel();
getContentPane().add(southPanel, BorderLayout.CENTER);

ButtonGroup group = new ButtonGroup();

final JRadioButton leftButton = new JRadioButton();
leftButton.setText("靠左侧显示");
group.add(leftButton);
southPanel.add(leftButton);
leftButton.addActionListener(new leftActionListener()); 

final JRadioButton middleButton = new JRadioButton();
middleButton.setText("居中显示");
middleButton.setSelected(true);
group.add(middleButton);
southPanel.add(middleButton);
middleButton.addActionListener(new middleActionListener());

final JRadioButton rightButton = new JRadioButton();
rightButton.setText("靠右侧显示");
group.add(rightButton);
southPanel.add(rightButton);
rightButton.addActionListener(new rightActionListener() );
}

class middleActionListener implements ActionListener {
public void actionPerformed(ActionEvent e){
panel.add(label, BorderLayout.CENTER);
}


}
class rightActionListener implements ActionListener {
public void actionPerformed(ActionEvent e){
panel.add(label, BorderLayout.EAST);
}
}

class leftActionListener implements ActionListener {
public void actionPerformed(ActionEvent e){
panel.add(label, BorderLayout.WEST);
}
}}

解决方案 »

  1.   

    由于你的panel只有一个label,所以使用BorderLayout布局的时候,无论是在north,south,center都是默认占据了整个panel,建议创建三个label对象,分别设为east,center,west,然后按钮的消息响应事件就设置对应的label的setVisible(true),其他的设为false就行了,试过可行了
      

  2.   

    监听类的方法里加pack();方法。panel.add(label, BorderLayout.CENTER);这句后面。这样会改变窗体大小。你看一下pacK()方法是干什么的。
      

  3.   

    package org.test;import java.awt.BorderLayout;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;import javax.swing.JButton;
    import javax.swing.JFrame;
    import javax.swing.JLabel;
    import javax.swing.JPanel;public class SelectPosition extends JFrame implements ActionListener {
    private static final long serialVersionUID = 1L;
    private JPanel jpanel;
    private JLabel label= new JLabel("请注意我的位置啊!");
    private JButton leftButton=new JButton("靠左侧显示");;
    private JButton middleButton=new JButton("居中显示");
    private JButton rightButton=new JButton("靠右侧显示");
    private JPanel southPanel=new JPanel(); public static void main(String[] args) {
    new SelectPosition();
    } public SelectPosition() {
    this.setTitle("控制内容的对齐方式 ");
    this.setSize(500,400);
    init();
    this.getContentPane().add(jpanel,BorderLayout.CENTER);
    this.getContentPane().add(southPanel,BorderLayout.NORTH);
    this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    this.setVisible(true); }
    private void init(){
    leftButton.addActionListener(this);
    middleButton.setSelected(true);
    middleButton.addActionListener(this);

    rightButton.addActionListener(this);
    southPanel.add(leftButton);
    southPanel.add(middleButton);
    southPanel.add(rightButton);
    jpanel=new JPanel();
    jpanel.setLayout(new BorderLayout(0, 0));
    jpanel.add(label, BorderLayout.CENTER);

    }
    public void actionPerformed(ActionEvent e) {
    if (e.getSource() == leftButton) {
    leftAction();
    }
    if (e.getSource() == rightButton) {
    rightAction();
    } if (e.getSource() == middleButton) {
    centerAction();
    } }
    private void centerAction() {

        jpanel.add(label,BorderLayout.CENTER);
        jpanel.doLayout();

    } private void rightAction() {
    jpanel.add(label,BorderLayout.EAST);
    jpanel.doLayout();
    } private void leftAction() {
    jpanel.add(label,BorderLayout.WEST);
    jpanel.doLayout();
    }
    }
      

  4.   

    //加上下面一句即可
    class rightActionListener implements ActionListener {
    public void actionPerformed(ActionEvent e){
    panel.add(label, BorderLayout.EAST);
    panel.doLayout();}