为什么我将一个框架的宽度和高度设置为400,例如可用空间较小以及我如何解决它以及如何在没有内容被剪切的情况下将一些东西放在中心? Example
public class Main extends JPanel{
static int width = 400;
static int height = 400;
static int arcWidth = 400;
static int arcHeight = 400;
public static void main(String args[]){
JFrame frame = new JFrame();
JFrame mainContent = new JFrame();
Main panel = new Main();
frame.setSize(width, height);
frame.setLocationRelativeTo(null);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setTitle("Clock");
frame.add(panel);
frame.setVisible(true);
}
public void paintComponent(Graphics g){
super.paintComponent(g);
Graphics2D g2 = (Graphics2D) g;
g2.setRenderingHint(
RenderingHints.KEY_ANTIALIASING,
RenderingHints.VALUE_ANTIALIAS_ON);
g.setColor(Color.BLACK);
g2.setStroke(new BasicStroke(4,BasicStroke.CAP_ROUND, BasicStroke.JOIN_ROUND));
g.drawArc(width/2 -arcWidth/2, height/2 - arcHeight/2, arcWidth, arcHeight, 0, 360);
}
}
答案 0 :(得分:1)
首先使用“实际”值,而不是“魔术”值
getWidth
和getHeight
会告诉您组件的“实际”尺寸是什么
@Override
protected void paintComponent(Graphics g){
super.paintComponent(g);
Graphics2D g2 = (Graphics2D) g;
g2.setRenderingHint(
RenderingHints.KEY_ANTIALIASING,
RenderingHints.VALUE_ANTIALIAS_ON);
g.setColor(Color.BLACK);
g2.setStroke(new BasicStroke(4,BasicStroke.CAP_ROUND, BasicStroke.JOIN_ROUND));
g.drawArc(getWidth()/2 -arcWidth/2, getHeight()/2 - arcHeight/2, arcWidth, arcHeight, 0, 360);
}
至于您遇到问题的原因,请参阅: