Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/308.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在java StillClock类中写入名称? 我的钟显示正确,但我需要在中间写我的名字,我不知道怎么做。还有,如何在每次程序运行时改变时针和分针?我假设我需要使小时和分钟等于一个随机数生成器 class StillClock extends JPanel { private int hour; private int minute; private int second; private boolean hourHandVisible = true; private boolean minuteHandVisible = true; private boolean secondHandVisible = true; /** Construct a default clock with the current time*/ public StillClock() { setCurrentTime(); } /** Construct a clock with specified hour, minute, and second */ public StillClock(int hour, int minute) { this.hour = hour; this.minute = minute; //this.second = second; } /** Return hour */ public int getHour() { return hour; } /** Set a new hour */ public void setHour(int hour) { this.hour = hour; repaint(); } /** Return minute */ public int getMinute() { return minute; } /** Set a new minute */ public void setMinute(int minute) { this.minute = minute; repaint(); } /*public int getSecond() { return second; } public void setSecond(int second) { this.second = second; repaint(); }*/ /** Draw the clock */ protected void paintComponent(Graphics g) { super.paintComponent(g); // Initialize clock parameters int clockRadius = (int)(Math.min(getWidth(), getHeight()) * 0.8 * 0.5); int xCenter = getWidth() / 2; int yCenter = getHeight() / 2; // Draw circle g.setColor(Color.black); g.drawOval(xCenter - clockRadius, yCenter - clockRadius,2 * clockRadius, 2 * clockRadius); // Draw minute hand if (minuteHandVisible) { int mLength = (int)(clockRadius * 0.65); int xMinute = (int)(xCenter + mLength *Math.sin(minute * (2 * Math.PI / 60))); int yMinute = (int)(yCenter - mLength *Math.cos(minute * (2 * Math.PI / 60))); g.setColor(Color. blue); g.drawLine(xCenter, yCenter, xMinute, yMinute); } // Draw hour hand if (hourHandVisible) { int hLength = (int)(clockRadius * 0.5); int xHour = (int)(xCenter + hLength *Math.sin((hour % 12 + minute / 60.0) * (2 * Math.PI / 12))); int yHour = (int)(yCenter - hLength *Math.cos((hour % 12 + minute / 60.0) * (2 * Math.PI / 12))); g.setColor(Color.green); g.drawLine(xCenter, yCenter, xHour, yHour); } // }// Display hours on the clock g.setColor(Color.red); for (int i = 0; i < 12; i++) { int x = (int)(xCenter + 0.8 * clockRadius * Math.sin (i*(2 * Math.PI / 12))); int y = (int)(yCenter - 0.8 * clockRadius * Math.cos (i*(2 * Math.PI / 12))); g.drawString("" + ((i == 0) ? 12 : i), x, y); } } public void setCurrentTime() { // Construct a calendar for the current date and time Calendar calendar = new GregorianCalendar(); // Set current hour, minute and second this.hour = calendar.get(Calendar.HOUR_OF_DAY); this.minute = calendar.get(Calendar.MINUTE); } public Dimension getPreferredSize() { return new Dimension(800, 800); } public boolean isHourHandVisible() { return hourHandVisible; } public boolean isMinuteHandVisible() { return hourHandVisible; } public void setHourHandVisible(boolean hourHandVisible) { this.hourHandVisible = hourHandVisible; repaint(); } public void setMinuteHandVisible(boolean minuteHandVisible) { this.minuteHandVisible = minuteHandVisible; repaint(); } } 类StillClock扩展了JPanel { 私人整数小时; 私人整数分钟; 二等兵; 私有布尔hourHandVisible=true; 私有布尔值minuteHandVisible=true; 私有布尔值secondHandVisible=true; /**用当前时间构建默认时钟*/ 公共时钟() { setCurrentTime(); } /**构造一个具有指定小时、分钟和秒的时钟*/ 公共静态时钟(整小时,整分钟) { 这个小时=小时; this.minute=分钟; //这个秒=秒; } /**返程时间*/ 公共整数getHour() { 返程时间; } /**设置一个新的小时*/ 公共无效设置小时(整小时) { 这个小时=小时; 重新油漆(); } /**返回分钟*/ 公共int getMinute() { 返回分钟; } /**设定新的分钟数*/ 公共无效设置分钟(整数分钟) { this.minute=分钟; 重新油漆(); } /*公共int getSecond() { 返回第二; } 公共无效设置秒(整数秒) { 这个秒=秒; 重新油漆(); }*/ /**画时钟*/ 受保护组件(图形g) { 超级组件(g); //初始化时钟参数 int clockRadius=(int)(Math.min(getWidth(),getHeight())*0.8*0.5); int xCenter=getWidth()/2; int yCenter=getHeight()/2; //画圈 g、 设置颜色(颜色为黑色); g、 drawOval(X中心-时钟半径,Y中心-时钟半径,2*时钟半径,2*时钟半径); //画分针 如果(分钟可见) { int mLength=(int)(时钟半径*0.65); int xMinute=(int)(xCenter+mLength*Math.sin(minute*(2*Math.PI/60)); int-yMinute=(int)(yCenter-mllength*Math.cos(minute*(2*Math.PI/60)); g、 setColor(Color.blue); g、 抽绳(X中心、Y中心、X分钟、Y分钟); } //划时针 如果(小时和可见) { 整数长度=(整数)(时钟半径*0.5); intxhour=(int)(xCenter+hllength*Math.sin((小时%12+分钟/60.0)*(2*Math.PI/12)); intyhour=(int)(yCenter-hllength*Math.cos((小时%12+分钟/60.0)*(2*Math.PI/12)); g、 setColor(Color.green); g、 抽绳(xCenter、yCenter、xHour、yHour); } //}//在时钟上显示小时数 g、 setColor(Color.red); 对于(int i=0;i_Java - Fatal编程技术网

在java StillClock类中写入名称? 我的钟显示正确,但我需要在中间写我的名字,我不知道怎么做。还有,如何在每次程序运行时改变时针和分针?我假设我需要使小时和分钟等于一个随机数生成器 class StillClock extends JPanel { private int hour; private int minute; private int second; private boolean hourHandVisible = true; private boolean minuteHandVisible = true; private boolean secondHandVisible = true; /** Construct a default clock with the current time*/ public StillClock() { setCurrentTime(); } /** Construct a clock with specified hour, minute, and second */ public StillClock(int hour, int minute) { this.hour = hour; this.minute = minute; //this.second = second; } /** Return hour */ public int getHour() { return hour; } /** Set a new hour */ public void setHour(int hour) { this.hour = hour; repaint(); } /** Return minute */ public int getMinute() { return minute; } /** Set a new minute */ public void setMinute(int minute) { this.minute = minute; repaint(); } /*public int getSecond() { return second; } public void setSecond(int second) { this.second = second; repaint(); }*/ /** Draw the clock */ protected void paintComponent(Graphics g) { super.paintComponent(g); // Initialize clock parameters int clockRadius = (int)(Math.min(getWidth(), getHeight()) * 0.8 * 0.5); int xCenter = getWidth() / 2; int yCenter = getHeight() / 2; // Draw circle g.setColor(Color.black); g.drawOval(xCenter - clockRadius, yCenter - clockRadius,2 * clockRadius, 2 * clockRadius); // Draw minute hand if (minuteHandVisible) { int mLength = (int)(clockRadius * 0.65); int xMinute = (int)(xCenter + mLength *Math.sin(minute * (2 * Math.PI / 60))); int yMinute = (int)(yCenter - mLength *Math.cos(minute * (2 * Math.PI / 60))); g.setColor(Color. blue); g.drawLine(xCenter, yCenter, xMinute, yMinute); } // Draw hour hand if (hourHandVisible) { int hLength = (int)(clockRadius * 0.5); int xHour = (int)(xCenter + hLength *Math.sin((hour % 12 + minute / 60.0) * (2 * Math.PI / 12))); int yHour = (int)(yCenter - hLength *Math.cos((hour % 12 + minute / 60.0) * (2 * Math.PI / 12))); g.setColor(Color.green); g.drawLine(xCenter, yCenter, xHour, yHour); } // }// Display hours on the clock g.setColor(Color.red); for (int i = 0; i < 12; i++) { int x = (int)(xCenter + 0.8 * clockRadius * Math.sin (i*(2 * Math.PI / 12))); int y = (int)(yCenter - 0.8 * clockRadius * Math.cos (i*(2 * Math.PI / 12))); g.drawString("" + ((i == 0) ? 12 : i), x, y); } } public void setCurrentTime() { // Construct a calendar for the current date and time Calendar calendar = new GregorianCalendar(); // Set current hour, minute and second this.hour = calendar.get(Calendar.HOUR_OF_DAY); this.minute = calendar.get(Calendar.MINUTE); } public Dimension getPreferredSize() { return new Dimension(800, 800); } public boolean isHourHandVisible() { return hourHandVisible; } public boolean isMinuteHandVisible() { return hourHandVisible; } public void setHourHandVisible(boolean hourHandVisible) { this.hourHandVisible = hourHandVisible; repaint(); } public void setMinuteHandVisible(boolean minuteHandVisible) { this.minuteHandVisible = minuteHandVisible; repaint(); } } 类StillClock扩展了JPanel { 私人整数小时; 私人整数分钟; 二等兵; 私有布尔hourHandVisible=true; 私有布尔值minuteHandVisible=true; 私有布尔值secondHandVisible=true; /**用当前时间构建默认时钟*/ 公共时钟() { setCurrentTime(); } /**构造一个具有指定小时、分钟和秒的时钟*/ 公共静态时钟(整小时,整分钟) { 这个小时=小时; this.minute=分钟; //这个秒=秒; } /**返程时间*/ 公共整数getHour() { 返程时间; } /**设置一个新的小时*/ 公共无效设置小时(整小时) { 这个小时=小时; 重新油漆(); } /**返回分钟*/ 公共int getMinute() { 返回分钟; } /**设定新的分钟数*/ 公共无效设置分钟(整数分钟) { this.minute=分钟; 重新油漆(); } /*公共int getSecond() { 返回第二; } 公共无效设置秒(整数秒) { 这个秒=秒; 重新油漆(); }*/ /**画时钟*/ 受保护组件(图形g) { 超级组件(g); //初始化时钟参数 int clockRadius=(int)(Math.min(getWidth(),getHeight())*0.8*0.5); int xCenter=getWidth()/2; int yCenter=getHeight()/2; //画圈 g、 设置颜色(颜色为黑色); g、 drawOval(X中心-时钟半径,Y中心-时钟半径,2*时钟半径,2*时钟半径); //画分针 如果(分钟可见) { int mLength=(int)(时钟半径*0.65); int xMinute=(int)(xCenter+mLength*Math.sin(minute*(2*Math.PI/60)); int-yMinute=(int)(yCenter-mllength*Math.cos(minute*(2*Math.PI/60)); g、 setColor(Color.blue); g、 抽绳(X中心、Y中心、X分钟、Y分钟); } //划时针 如果(小时和可见) { 整数长度=(整数)(时钟半径*0.5); intxhour=(int)(xCenter+hllength*Math.sin((小时%12+分钟/60.0)*(2*Math.PI/12)); intyhour=(int)(yCenter-hllength*Math.cos((小时%12+分钟/60.0)*(2*Math.PI/12)); g、 setColor(Color.green); g、 抽绳(xCenter、yCenter、xHour、yHour); } //}//在时钟上显示小时数 g、 setColor(Color.red); 对于(int i=0;i

在java StillClock类中写入名称? 我的钟显示正确,但我需要在中间写我的名字,我不知道怎么做。还有,如何在每次程序运行时改变时针和分针?我假设我需要使小时和分钟等于一个随机数生成器 class StillClock extends JPanel { private int hour; private int minute; private int second; private boolean hourHandVisible = true; private boolean minuteHandVisible = true; private boolean secondHandVisible = true; /** Construct a default clock with the current time*/ public StillClock() { setCurrentTime(); } /** Construct a clock with specified hour, minute, and second */ public StillClock(int hour, int minute) { this.hour = hour; this.minute = minute; //this.second = second; } /** Return hour */ public int getHour() { return hour; } /** Set a new hour */ public void setHour(int hour) { this.hour = hour; repaint(); } /** Return minute */ public int getMinute() { return minute; } /** Set a new minute */ public void setMinute(int minute) { this.minute = minute; repaint(); } /*public int getSecond() { return second; } public void setSecond(int second) { this.second = second; repaint(); }*/ /** Draw the clock */ protected void paintComponent(Graphics g) { super.paintComponent(g); // Initialize clock parameters int clockRadius = (int)(Math.min(getWidth(), getHeight()) * 0.8 * 0.5); int xCenter = getWidth() / 2; int yCenter = getHeight() / 2; // Draw circle g.setColor(Color.black); g.drawOval(xCenter - clockRadius, yCenter - clockRadius,2 * clockRadius, 2 * clockRadius); // Draw minute hand if (minuteHandVisible) { int mLength = (int)(clockRadius * 0.65); int xMinute = (int)(xCenter + mLength *Math.sin(minute * (2 * Math.PI / 60))); int yMinute = (int)(yCenter - mLength *Math.cos(minute * (2 * Math.PI / 60))); g.setColor(Color. blue); g.drawLine(xCenter, yCenter, xMinute, yMinute); } // Draw hour hand if (hourHandVisible) { int hLength = (int)(clockRadius * 0.5); int xHour = (int)(xCenter + hLength *Math.sin((hour % 12 + minute / 60.0) * (2 * Math.PI / 12))); int yHour = (int)(yCenter - hLength *Math.cos((hour % 12 + minute / 60.0) * (2 * Math.PI / 12))); g.setColor(Color.green); g.drawLine(xCenter, yCenter, xHour, yHour); } // }// Display hours on the clock g.setColor(Color.red); for (int i = 0; i < 12; i++) { int x = (int)(xCenter + 0.8 * clockRadius * Math.sin (i*(2 * Math.PI / 12))); int y = (int)(yCenter - 0.8 * clockRadius * Math.cos (i*(2 * Math.PI / 12))); g.drawString("" + ((i == 0) ? 12 : i), x, y); } } public void setCurrentTime() { // Construct a calendar for the current date and time Calendar calendar = new GregorianCalendar(); // Set current hour, minute and second this.hour = calendar.get(Calendar.HOUR_OF_DAY); this.minute = calendar.get(Calendar.MINUTE); } public Dimension getPreferredSize() { return new Dimension(800, 800); } public boolean isHourHandVisible() { return hourHandVisible; } public boolean isMinuteHandVisible() { return hourHandVisible; } public void setHourHandVisible(boolean hourHandVisible) { this.hourHandVisible = hourHandVisible; repaint(); } public void setMinuteHandVisible(boolean minuteHandVisible) { this.minuteHandVisible = minuteHandVisible; repaint(); } } 类StillClock扩展了JPanel { 私人整数小时; 私人整数分钟; 二等兵; 私有布尔hourHandVisible=true; 私有布尔值minuteHandVisible=true; 私有布尔值secondHandVisible=true; /**用当前时间构建默认时钟*/ 公共时钟() { setCurrentTime(); } /**构造一个具有指定小时、分钟和秒的时钟*/ 公共静态时钟(整小时,整分钟) { 这个小时=小时; this.minute=分钟; //这个秒=秒; } /**返程时间*/ 公共整数getHour() { 返程时间; } /**设置一个新的小时*/ 公共无效设置小时(整小时) { 这个小时=小时; 重新油漆(); } /**返回分钟*/ 公共int getMinute() { 返回分钟; } /**设定新的分钟数*/ 公共无效设置分钟(整数分钟) { this.minute=分钟; 重新油漆(); } /*公共int getSecond() { 返回第二; } 公共无效设置秒(整数秒) { 这个秒=秒; 重新油漆(); }*/ /**画时钟*/ 受保护组件(图形g) { 超级组件(g); //初始化时钟参数 int clockRadius=(int)(Math.min(getWidth(),getHeight())*0.8*0.5); int xCenter=getWidth()/2; int yCenter=getHeight()/2; //画圈 g、 设置颜色(颜色为黑色); g、 drawOval(X中心-时钟半径,Y中心-时钟半径,2*时钟半径,2*时钟半径); //画分针 如果(分钟可见) { int mLength=(int)(时钟半径*0.65); int xMinute=(int)(xCenter+mLength*Math.sin(minute*(2*Math.PI/60)); int-yMinute=(int)(yCenter-mllength*Math.cos(minute*(2*Math.PI/60)); g、 setColor(Color.blue); g、 抽绳(X中心、Y中心、X分钟、Y分钟); } //划时针 如果(小时和可见) { 整数长度=(整数)(时钟半径*0.5); intxhour=(int)(xCenter+hllength*Math.sin((小时%12+分钟/60.0)*(2*Math.PI/12)); intyhour=(int)(yCenter-hllength*Math.cos((小时%12+分钟/60.0)*(2*Math.PI/12)); g、 setColor(Color.green); g、 抽绳(xCenter、yCenter、xHour、yHour); } //}//在时钟上显示小时数 g、 setColor(Color.red); 对于(int i=0;i,java,Java,首先看一看 但最基本的是 String text = "You're Name Here"; FontMetrics fm = g.getFontMetrics(); int x = (getWidth() - fm.stringWidth(text)) / 2; int y = ((getHeight()- fm.getHeight()) / 2) + fm.getAscent(); g.drawString(text, x, y); 要比较“垂直居中文本”的概念,请查看此内容尝试将其添加到

首先看一看

但最基本的是

String text = "You're Name Here";
FontMetrics fm = g.getFontMetrics();
int x = (getWidth() - fm.stringWidth(text)) / 2;
int y = ((getHeight()- fm.getHeight()) / 2) + fm.getAscent();
g.drawString(text, x, y);

要比较“垂直居中文本”的概念,请查看此内容

尝试将其添加到
paintComponent()的底部


你为这篇课文做了哪些尝试,又是如何失败的?我在你发布的颂歌中没有看到;)我没有尝试过任何东西,但有人发布了一个链接,指向使用文本API。非常感谢。
// get the FontMetrics for the current font
FontMetrics fm = g.getFontMetrics();

// find the center location to display
int stringWidth = fm.stringWidth("MyName");
int stringAccent = fm.getAscent();

// get the position of the leftmost character in the baseline
int xCoordinate = getWidth() / 2 - stringWidth / 2;
int yCoordinate = getHeight() / 2 + stringAccent / 2;

g.drawString("MyName", xCoordinate, yCoordinate);