JButton两个显示2个地方和大小,有两个类
与尺寸和位置不一致 请帮忙,谢谢 代码分为两部分
最终公共课ChessMain {
JFrame frame;
public static void main(String[] args) {
new ChessMain().go();
}
private void go() {
frame = new JFrame("Test");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// Container contentPane = frame.getContentPane();
frame.setContentPane(new DrawPanelChess ());
frame.setVisible(true);
frame.setExtendedState(Frame.MAXIMIZED_BOTH);
Icon warnIcon = new ImageIcon("white_King_Chess.png");
JButton button2 = new JButton(warnIcon);
button2.setLayout(null);
button2.setBounds(100,0, 100, 100);
frame.add(button2);
}
}
类DrawPanelChess扩展了JPanel {
private static final long serialVersionUID = 9166938856558245300L;
public void paintComponent(Graphics g){
g.setColor(Color.BLACK);
g.fillRect(0, 0, this.getWidth(), this.getHeight());
g.setColor(Color.WHITE);
{//row 1
g.fillRect(0, 0, this.getWidth() / 8, this.getHeight() / 8);
g.fillRect(this.getWidth() / 4, 0, this.getWidth() / 8, this.getHeight() / 8);
g.fillRect(this.getWidth() / 2, 0, this.getWidth() / 8, this.getHeight() / 8);
g.fillRect(this.getWidth() / 8 * 6, 0, this.getWidth() / 8, this.getHeight() / 8);
}
{//row 2
g.fillRect(this.getWidth() / 8, this.getHeight() / 8, this.getWidth() / 8, this.getHeight() / 8);
g.fillRect(this.getWidth() / 8 * 3, this.getHeight() / 8, this.getWidth() / 8, this.getHeight() / 8);
g.fillRect(this.getWidth() / 8 * 5, this.getHeight() / 8, this.getWidth() / 8, this.getHeight() / 8);
g.fillRect(this.getWidth() / 8 * 7, this.getHeight() / 8, this.getWidth() / 8, this.getHeight() / 8);
}
{//row 3
g.fillRect(0, this.getHeight() / 4, this.getWidth() / 8, this.getHeight() / 8);
g.fillRect(this.getWidth() / 4, this.getHeight() / 4, this.getWidth() / 8, this.getHeight() / 8);
g.fillRect(this.getWidth() / 2, this.getHeight() / 4, this.getWidth() / 8, this.getHeight() / 8);
g.fillRect(this.getWidth() / 8 * 6, this.getHeight() / 4, this.getWidth() / 8, this.getHeight() / 8);
}
{//row 4
g.fillRect(this.getWidth() / 8, this.getHeight() / 8 * 3, this.getWidth() / 8, this.getHeight() / 8);
g.fillRect(this.getWidth() / 8 * 3, this.getHeight() / 8 * 3, this.getWidth() / 8, this.getHeight() / 8);
g.fillRect(this.getWidth() / 8 * 5, this.getHeight() / 8 * 3, this.getWidth() / 8, this.getHeight() / 8);
g.fillRect(this.getWidth() / 8 * 7, this.getHeight() / 8 * 3, this.getWidth() / 8, this.getHeight() / 8);
}
{//row 5
g.fillRect(0, this.getHeight() / 2, this.getWidth() / 8, this.getHeight() / 8);
g.fillRect(this.getWidth() / 4, this.getHeight() / 2, this.getWidth() / 8, this.getHeight() / 8);
g.fillRect(this.getWidth() / 2, this.getHeight() / 2, this.getWidth() / 8, this.getHeight() / 8);
g.fillRect(this.getWidth() / 8 * 6, this.getHeight() / 2, this.getWidth() / 8, this.getHeight() / 8);
}
{//row 6
g.fillRect(this.getWidth() / 8, this.getHeight() / 8 * 5, this.getWidth() / 8, this.getHeight() / 8);
g.fillRect(this.getWidth() / 8 * 3, this.getHeight() / 8 * 5, this.getWidth() / 8, this.getHeight() / 8);
g.fillRect(this.getWidth() / 8 * 5, this.getHeight() / 8 * 5, this.getWidth() / 8, this.getHeight() / 8);
g.fillRect(this.getWidth() / 8 * 7, this.getHeight() / 8 * 5, this.getWidth() / 8, this.getHeight() / 8);
}
{//row 7
g.fillRect(0, this.getHeight() / 8 * 6, this.getWidth() / 8, this.getHeight() / 8);
g.fillRect(this.getWidth() / 4, this.getHeight() / 8 * 6, this.getWidth() / 8, this.getHeight() / 8);
g.fillRect(this.getWidth() / 2, this.getHeight() / 8 * 6, this.getWidth() / 8, this.getHeight() / 8);
g.fillRect(this.getWidth() / 8 * 6, this.getHeight() / 8 * 6, this.getWidth() / 8, this.getHeight() / 8);
}
{//row 8
g.fillRect(this.getWidth() / 8, this.getHeight() / 8 * 7, this.getWidth() / 8, this.getHeight() / 8);
g.fillRect(this.getWidth() / 8 * 3, this.getHeight() / 8 * 7, this.getWidth() / 8, this.getHeight() / 8);
g.fillRect(this.getWidth() / 8 * 5, this.getHeight() / 8 * 7, this.getWidth() / 8, this.getHeight() / 8);
g.fillRect(this.getWidth() / 8 * 7, this.getHeight() / 8 * 7, this.getWidth() / 8, this.getHeight() / 8);
}
// System.out.println("\u2654");
repaint();
} }
答案 0 :(得分:0)
在将DrawPanelChess JPanel添加为框架的contentPane之后,似乎将按钮添加到框架中。当您更改帧大小时,它会重新定位。如果您打算添加更多组件,但暂时不用添加
,那么未来不是一件好事frame.setResizable(false);
我还建议使用像GridBagLayout这样的东西来指定你希望它与棋盘一起定位的位置。