我试图在 JPanel
中绘制一个矩形,但该矩形未显示。我错过了什么?
这是我到目前为止尝试过的方法。
public class selectSeat extends JFrame {
JPanel panel = new JPanel();
public static void main(String[] args) {
selectSeat frameTabel = new selectSeat("","","");
}
public selectSeat(String title, String day, String time)
{
super("Select Seat");
setSize(350,350);
setLocation(500,280);
panel.setLayout(null);
RectDraw rect= new RectDraw();
panel.add(rect);
getContentPane().add(panel);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
}
private static class RectDraw extends JPanel
{
protected void paintComponent(Graphics g) {
super.paintComponent(g);
g.drawRect(230,80,10,10);
g.setColor(Color.RED);
g.fillRect(230,80,10,10);
}
public Dimension getPreferredSize() {
return new Dimension(50, 20); // appropriate constants
}
}
}
最佳答案
您正在绘制矩形,但它位于 280, 80,这超出了可见 JPanel 的范围。了解绘图位置是相对于 JPanel 本身内的坐标的。
关于java - JPanel 中没有显示矩形,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37240865/