这是《core java》里面的一个按钮测试的程序import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class ButtonTest {

public static void main(String[] agrs)
{
ButtonFrame frame = new ButtonFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.show();
}}
class ButtonFrame extends JFrame
{
     public ButtonFrame()
     {
      setTitle("ButtonTest");
      setSize(300, 200);
      
      ButtonPanel panel = new ButtonPanel();
      Container contentPane = getContentPane();
      contentPane.add(panel);
     }
}
class ButtonPanel extends JPanel
{
public ButtonPanel()
{

JButton yellowButton = new JButton("Yellow");
JButton blueButton = new JButton("Blue");
JButton redButton = new JButton("Red");

add(yellowButton);
    add(redButton);
add(blueButton);

ColorAction yellowAction = new ColorAction(Color.YELLOW);
ColorAction blueAction  = new ColorAction(Color.BLUE);
ColorAction redAction = new ColorAction(Color.RED);

yellowButton.addActionListener(yellowAction);
blueButton.addActionListener(blueAction);
redButton.addActionListener(redAction);

}

private class ColorAction implements ActionListener
{
public ColorAction(Color c)
{
backgroundColor = c;
}
public void actionPerformed(ActionEvent event)
{
setBackground(backgroundColor);
}

private Color backgroundColor;
}
}请问下按钮的位置可不可以自定义的啊?
谢谢!

解决方案 »

  1.   

    Panel的布局方式默认是FlowLayout(没记错的话),就是说加上去的按钮是挨个往上放的,位置自己不能设定,要想自己设置位置,那么这样改
    class ButtonPanel extends JPanel
    {
    public ButtonPanel()
    {
    this.setLayout(null);
    JButton yellowButton = new JButton("Yellow");
    JButton blueButton = new JButton("Blue");
    JButton redButton = new JButton("Red");

                        
                    yellowButton.setBounds(左上角坐标X,左上角坐标Y,按钮宽度,按钮高度);
                     redButton.setBounds(左上角坐标X,左上角坐标Y,按钮宽度,按钮高度);
                     blueButton.setBounds(左上角坐标X,左上角坐标Y,按钮宽度,按钮高度);

    add(yellowButton);
             add(redButton);
    add(blueButton);

    ColorAction yellowAction = new ColorAction(Color.YELLOW);
    ColorAction blueAction  = new ColorAction(Color.BLUE);
    ColorAction redAction = new ColorAction(Color.RED);

    yellowButton.addActionListener(yellowAction);
    blueButton.addActionListener(blueAction);
    redButton.addActionListener(redAction);

    }

    private class ColorAction implements ActionListener
    {
    public ColorAction(Color c)
    {
    backgroundColor = c;
    }
    public void actionPerformed(ActionEvent event)
    {
    setBackground(backgroundColor);
    }

    private Color backgroundColor;
    }
    }
      

  2.   

    /**
     * 
     */
    package cn.com;/**
     * @author Administrator
     * 
     */
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;public class ButtonTest { public static void main(String[] agrs) {
    ButtonFrame frame = new ButtonFrame();
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.show();
    }}class ButtonFrame extends JFrame {
    public ButtonFrame() {
    setTitle("ButtonTest");
    setSize(300, 200); ButtonPanel panel = new ButtonPanel();
    Container contentPane = getContentPane();
    contentPane.add(panel);
    }}class ButtonPanel extends JPanel {
    public ButtonPanel() { JButton yellowButton = new JButton("Yellow");
    JButton blueButton = new JButton("Blue");
    JButton redButton = new JButton("Red");

    this.setLayout(null);

    add(yellowButton);
    yellowButton.setSize(120,30);
    yellowButton.setLocation(10,40);

    add(redButton);
    redButton.setSize(120,30);
    redButton.setLocation(10,80);

    add(blueButton);
    blueButton.setSize(120,30);
    blueButton.setLocation(10,120); ColorAction yellowAction = new ColorAction(Color.YELLOW);
    ColorAction blueAction = new ColorAction(Color.BLUE);
    ColorAction redAction = new ColorAction(Color.RED); yellowButton.addActionListener(yellowAction);
    blueButton.addActionListener(blueAction);
    redButton.addActionListener(redAction); } private class ColorAction implements ActionListener {
    public ColorAction(Color c) {
    backgroundColor = c;
    } public void actionPerformed(ActionEvent event) {
    setBackground(backgroundColor);
    } private Color backgroundColor;
    }
    }