在我的Tic-Tac-Toe游戏中,我画了线条,我现在正试图按下按钮。我试图放入的第一个按钮,我试图设置边界,并且它没有设置边界。它只是填满整个屏幕。如何让button
仅位于右上角?
public class Project extends JFrame{
static JButton button = new JButton("");
static JButton button2 = new JButton("");
static JButton button3 = new JButton("");
static JButton button4 = new JButton("");
static JButton button5 = new JButton("");
static JButton button6 = new JButton("");
static JButton button7 = new JButton("");
static JButton button8 = new JButton("");
static JButton button9 = new JButton("");
public Project(){
setSize(1000, 750);
}
public void paint(Graphics g){
super.paint(g);
Graphics2D g2 = (Graphics2D) g;
Line2D line = new Line2D.Float(100, 100, 100, 400);
Line2D line2 = new Line2D.Float(200, 100, 200, 400);
Line2D line3 = new Line2D.Float(0, 200, 300, 200);
Line2D line4 = new Line2D.Float(0, 300, 300, 300);
g2.draw(line);
g2.draw(line2);
g2.draw(line3);
g2.draw(line4);
}
public static void main(String[] args){
Project t = new Project();
t.setVisible(true);
button.setBounds(0, 0, 50, 50);
t.add(button);
}
}
答案 0 :(得分:2)
您的Project类扩展了JFrame,JFrame contentPane(接受新组件的JFrame容器)默认使用BorderLayout。将组件添加到BorderLayout使用容器而不指定组件的位置时,默认情况下会将其添加到BorderLayout.CENTER位置,从而有效地填充GUI。有几种方法可以解决您的困境: