如何将JButton放置在JPanel

时间:2015-06-04 16:53:45

标签: java swing jbutton layout-manager

一般代码:以下问题将参考某些课程

public class Board extends JPanel { 

public Board() {
    BoardMethods a = new BoardMethods();//BoardMethods handles logic
    a.printBoard(getBoard());//irrelevant to question, helps determine winner
    JPanel panelLORDY = new JPanel(new FlowLayout());

    //JButton alphaButton = new JButton("Hi");
    //alphaButton.setBounds(213,131,100,100);
    //add(alphaButton);
}

public int[][] getBoard(){
    return boardEvalMatrix;
}
public void paintComponent(Graphics g){
    g.setColor(Color.BLUE);
    g.fillRect(0, 0, 1456, 916);
    Graphics2D newG = (Graphics2D) g;
    newG.setStroke(new BasicStroke(15));
    g.setColor(Color.YELLOW);
    for(int a = 0; a < 6; a++)//rows for board --- rowHeight is 127
        g.drawRect(128, 68 + (a*127), 1200, 127);
    g.setColor(Color.BLACK);
    newG.setStroke(new BasicStroke(8));
    for(int a = 0; a < 6; a++)//columns for board --- columnWidth is 171
        g.drawRect(128 + (a*171), 68, 171, 764);        
    for(int a = 0; a < 6; a++)//give rows black line in middle
        g.drawRect(128, 68 + (a*127), 1200, 127);
    //g.drawString("H", 213, 131); center point     
    //g.drawLine(50,0, 1456, 916); //width 1456 length 916 - school computer monitors
    JButton alphaButton = new JButton("Hi");
    alphaButton.setBounds(213,131,100,100);
    bored.add(alphaButton);

}
}


public class gameBoard extends JPanel {

private Board bored;
private RoundButton[][] buttonArray;//holds all roundButtons on board
private JPanel resetPanel, quitPanel;

public gameBoard() {
    setLayout(new BorderLayout());

    buttonArray = new RoundButton[6][7];
    for(int a = 0; a < buttonArray.length; a++)
        for(int b = 0; b < buttonArray[0].length; b++)


    bored = new Board();
    add(bored, BorderLayout.CENTER);

    resetPanel = new JPanel();
    resetPanel.setLayout(new FlowLayout());
    this.add(resetPanel, BorderLayout.NORTH);
    quitPanel = new JPanel();
    quitPanel.setLayout(new FlowLayout());
    this.add(quitPanel, BorderLayout.SOUTH);

    JButton resetButton = new JButton("Reset");
    resetButton.addActionListener(new resetListener());
    resetPanel.add(resetButton);//resets board
    JButton quitButton = new JButton("Quit");
    quitButton.addActionListener(new quitListener());
    quitPanel.add(quitButton);

    //JButton alphaButton = new JButton("Hi");
    //alphaButton.setBounds(213,131,100,100);
    //bored.add(alphaButton);


}
}

问题1:

我发现question关于如何在某个位置放置JButton,但是,我有圆形显示JButton,我需要JButton居中在某一点上,在这种情况下,213,131。我使用setBounds(int x, int y, int width, int height)方法放置JButton。有没有办法让JButton中心本身位于上面提到的坐标上,或者我是否必须使用这些数字来获取我想要的位置?这行可以在上面的两个类中找到,因为我正在测试某些内容并发现问题2 中描述的问题。

JButton alphaButton = new JButton("Hi");
alphaButton.setBounds(213,131,100,100);
add(alphaButton);

问题2:

如果您查看课程Board,请在paintComponent方法中使用setBounds()方法放置JButton。当调用JPanel方法时左上角位于正确的位置时,这会在paintComponent()上放置一个按钮,其中包含我认为正确尺寸的按钮。但是,当刷新屏幕时,再次调用paintComponent()并且按钮的数量继续增加,有趣但不是我想要的。但是,当我将setBounds方法和alphaButton移到Board的{​​{1}}方法之外的任何地方时,也就是说,将其放入paintComponent()所在的类中在Board构造函数内部绘制,Board传播问题停止,但JButton不再将自身置于正确的坐标点并丢失我给它的尺寸。我该如何解决这个问题?

P.S。您会多次看到JButton方法。在实际代码中,setBounds方法和调用它的setBounds一次只存在于一个类中,我在此处多次输入它以显示我放置它的位置。

1 个答案:

答案 0 :(得分:1)

永远不要使用setBounds(),坚持布局管理器。

FlowLayout 会自动将您的组件置于中心位置。更好的方法是使用BorderLayout,然后设置为CENTER