Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/305.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
如何在javagui中将圆划分为60个部分_Java_Swing_User Interface_Geometry - Fatal编程技术网

如何在javagui中将圆划分为60个部分

如何在javagui中将圆划分为60个部分,java,swing,user-interface,geometry,Java,Swing,User Interface,Geometry,我想要一个像时钟的秒针一样工作的圆圈,但我有一个文本字段而不是一个时钟手柄。例如,当时钟的秒针为30时,文本字段位于圆圈的最低位置,其文本为30。但是我不能将圆圈分成60个部分。请帮我编辑代码。 我必须提到我正在使用Timer类。 这是我的密码: public static void main(String[] args) { EventQueue.invokeLater(new Runnable() { public void run() { t

我想要一个像时钟的秒针一样工作的圆圈,但我有一个文本字段而不是一个时钟手柄。例如,当时钟的秒针为30时,文本字段位于圆圈的最低位置,其文本为30。但是我不能将圆圈分成60个部分。请帮我编辑代码。 我必须提到我正在使用Timer类。 这是我的密码:

public static void main(String[] args) {
    EventQueue.invokeLater(new Runnable() {
        public void run() {
            try {
                ClockWiseRotation window = new ClockWiseRotation();
                window.frame.setVisible(true);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    });
}

/**
 * Create the application.
 */
public ClockWiseRotation() {
    initialize();

}

/**
 * Initialize the contents of the frame.
 */
private void initialize() {

    timer = new Timer(1000, new ActionListener() {

        @Override
        public void actionPerformed(ActionEvent arg0) {
            second = Integer.toString(Integer.parseInt(handle.getText()) + 1);
            for (int i = 33; i <= 60; i++) {
                int angle = i * (360 / 60);
                handle.setBounds((int)(202 + 100 * Math.cos(angle)),
                        (int)(119 + 100 * Math.sin(angle)), 23, 26);

            }
            handle.setText(second);

        }
    });
    timer.start();

    frame = new JFrame();
    frame.setBounds(100, 100, 450, 300);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.getContentPane().setLayout(null);

    handle = new JTextField();
    handle.setText("0");
    handle.setBounds(202, 22, 23, 26);
    frame.getContentPane().add(handle);
    handle.setColumns(10);

    JComboBox centerPoint = new JComboBox();
    centerPoint.setBounds(202, 119, 17, 20);
    frame.getContentPane().add(centerPoint);

}
publicstaticvoidmain(字符串[]args){
invokeLater(新的Runnable(){
公开募捐{
试一试{
ClockWiseRotation窗口=新的ClockWiseRotation();
window.frame.setVisible(true);
}捕获(例外e){
e、 printStackTrace();
}
}
});
}
/**
*创建应用程序。
*/
公共顺时针旋转(){
初始化();
}
/**
*初始化框架的内容。
*/
私有void初始化(){
计时器=新计时器(1000,新ActionListener(){
@凌驾
已执行的公共无效操作(操作事件arg0){
second=Integer.toString(Integer.parseInt(handle.getText())+1);

对于(int i=33;iSwing是一个单线程环境,出于任何原因阻止EDT(如
Thread.sleep
)的长时间运行循环)将阻止UI被更新。Swing也不是线程,因此决不能从EDT上下文之外对UI进行更新

记住这一点,在
计时器的每一次滴答声中,您都会循环60到30次试图移动句柄,但句柄出现的唯一位置是
actionPerformed
方法退出时它所处的最后一个位置

有关更多详细信息,请参阅

您应该做的只是根据通话时的要求/需要将手柄放置在当前位置

我也一直在看你的计算

handle.setBounds((int)(202 + 100 * Math.cos(angle)),
                 (int)(119 + 100 * Math.sin(angle)), 23, 26);
根据我的记忆,要找到圆上的点,基于一个角度,你需要使用

x = xOffset + Math.cos(radians) * radius;
y = yOffset - Math.sin(radians) * radius;
有两件事需要指出,但最重要的是
Math.cos
Math.sin
期望值是弧度,而不是度

您还应该避免使用
null
布局,像素完美的布局在现代ui设计中是一种幻觉。有太多的因素会影响组件的单个大小,而这些都是您无法控制的。Swing的设计目的是与核心的布局管理器一起工作,丢弃这些布局将导致无休止的问题你会花越来越多的时间去纠正

您也应该避免以这种方式使用组件,相反,我会考虑使用自定义的绘画处理…


请看一看,有关更多详细信息,Swing是一个单线程环境,出于任何原因阻止EDT(如长时间运行的
线程循环)。sleep
)将阻止UI更新。Swing也不是线程,决不能从EDT上下文之外对UI进行更新

记住这一点,在
计时器的每一次滴答声中,您都会循环60到30次试图移动句柄,但句柄出现的唯一位置是
actionPerformed
方法退出时它所处的最后一个位置

有关更多详细信息,请参阅

您应该做的只是根据通话时的要求/需要将手柄放置在当前位置

我也一直在看你的计算

handle.setBounds((int)(202 + 100 * Math.cos(angle)),
                 (int)(119 + 100 * Math.sin(angle)), 23, 26);
根据我的记忆,要找到圆上的点,基于一个角度,你需要使用

x = xOffset + Math.cos(radians) * radius;
y = yOffset - Math.sin(radians) * radius;
有两件事需要指出,但最重要的是
Math.cos
Math.sin
期望值是弧度,而不是度

您还应该避免使用
null
布局,像素完美的布局在现代ui设计中是一种幻觉。有太多的因素会影响组件的单个大小,而这些都是您无法控制的。Swing的设计目的是与核心的布局管理器一起工作,丢弃这些布局将导致无休止的问题你会花越来越多的时间去纠正

您也应该避免以这种方式使用组件,相反,我会考虑使用自定义的绘画处理…


请查看,有关更多详细信息,请参见您的指导,您的指导非常有帮助,我已经这样解决了问题(通过删除for循环):

public void actionPerformed(ActionEvent arg0){
intSecond=Integer.parseInt(handle.getText())+1;

如果(intSecond谢谢你的指导真的很有帮助,我已经这样解决了(通过删除for循环):

public void actionPerformed(ActionEvent arg0){
intSecond=Integer.parseInt(handle.getText())+1;
如果(毫秒)