我已創建JPanel
並將其添加到JFrame
。 JPanel
具有TitledBorder
,但是當我使用的方法translate()
,例如使用translate()方法時,JPanel中的TitledBorder會移動
g2.translate(getWidth()/2, getHeight()/2);
代碼粘貼在下面。
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import javax.swing.BorderFactory;
import javax.swing.JFrame;
import javax.swing.JPanel;
class GPanel extends JPanel {
private void doDrawing(Graphics g) {
Graphics2D g2 = (Graphics2D) g;
g2.translate(getWidth()/2, getHeight()/2);
}
@Override
public void paintComponent(Graphics g) {
super.paintComponent(g);
doDrawing(g);
}
}
class Main extends JFrame {
public Main() {
}
public static void main(String[] args) {
Main ex = new Main();
ex.setSize(new Dimension(400, 400));
GPanel panel = new GPanel();
panel.setBorder(BorderFactory.createTitledBorder("Title"));
ex.add(panel);
ex.setLocationRelativeTo(null);
ex.setVisible(true);
}
}
這可能是由JFrame上使用的佈局管理器造成的嗎?如果您創建一個標題爲邊界的JPanel來保存GPanel,錯誤是否會持續? – DCON