Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/303.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/date/2.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 在JFrame中创建活动计数器_Java_Swing_Jframe_Jlabel_Counter - Fatal编程技术网

Java 在JFrame中创建活动计数器

Java 在JFrame中创建活动计数器,java,swing,jframe,jlabel,counter,Java,Swing,Jframe,Jlabel,Counter,我尝试搜索某种交互式JLabel。我希望它看起来像这样: 我不知道这叫什么,也不知道在哪里可以找到有关显示这一信息的信息。我希望它在按下+/-时打印一个刷新的数字。我可以在eclipse控制台上打印它,但不确定如何将它打印到JFrame 下面是一些代码: String input = JOptionPane.showInputDialog("Please enter the number of laps."); numLaps = Integer.parseInt(input);

我尝试搜索某种交互式
JLabel
。我希望它看起来像这样:

我不知道这叫什么,也不知道在哪里可以找到有关显示这一信息的信息。我希望它在按下+/-时打印一个刷新的数字。我可以在eclipse控制台上打印它,但不确定如何将它打印到
JFrame

下面是一些代码:

String input = JOptionPane.showInputDialog("Please enter the number of laps.");

    numLaps = Integer.parseInt(input);

    //frame creation
    JFrame f = new JFrame("Number of Laps");
    f.setSize(550, 450);
    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    f.setLayout(null);

    // label creation
    JLabel label = new JLabel("You entered " + numLaps + " laps. Press + to add a lap. Press - to subtract a lap.", SwingConstants.CENTER);
    label.setBounds(0,0,500,300);
    f.add(label);
    //display window

    f.setVisible(true);


    //button creation add
    JButton add = new JButton ("+");
    add.setBounds(350,250,50,50);
    add.setPreferredSize(new Dimension(50,50));
    add.addActionListener( new ActionListener()
    {

        @Override
        public void actionPerformed(ActionEvent e) {
            // what happens when button is pressed
            //numLaps++;
            addToLap();
            System.out.println(numLaps);
        }

    });
    f.add(add);

  //button creation subtract
        JButton sub = new JButton ("-");
        sub.setBounds(100,250,50,50);
        sub.setPreferredSize(new Dimension(50,50));

          sub.addActionListener( new ActionListener()
          {

            @Override
            public void actionPerformed(ActionEvent e) {
                // what happens when button is pressed
                //numLaps--;
                subToLap();
                System.out.println(numLaps);
            }

          });
          f.add(sub);
&添加/子方法:

private static void addToLap() {
    // TODO Auto-generated method stub
    numLaps++;
}
private static void subToLap() {
    // TODO Auto-generated method stub
    numLaps--;
}

您可以尝试以下方法:

private int numLaps = 0 // Or the number you want initialize with
然后,在您的方法中,应将numLaps值分配给JLabel,如下所示:

this.myLabel.setText(String.valueOf(numLaps));

我希望这能有所帮助。

你的问题是“这里有一些要求,这里有一些代码”类型的问题,但在你的帖子中没有实际连贯的具体问题。请把这个修好。并且当您想更改它显示的文本时,也可以考虑在您的JLab中调用<代码> SETTY(…)>代码>。请浏览、和部分,了解本网站的工作原理,并帮助您改进当前和未来的问题,从而获得更好的答案。感谢您的回复。我在问我可以用什么方法在两个按钮之间制作计数器。我基本上是想在每次按下+/-键时,将实时打印到该位置。这有用吗?就像我说的,我不知道这个方法将被调用——所以我在寻找建议。再次感谢。作为一个用户,我更喜欢使用带有
SpinnerNumberModel
JSpinner
f.setLayout(null)Java GUI必须在不同的操作系统、屏幕大小、屏幕分辨率等上工作。因此,它们不利于像素完美布局。使用布局管理器,或者与布局填充和边框一起使用。我已经在我的注释中告诉过您该方法——它被称为
setText(…)
。因此,使用该方法,我可以添加标签,但它不会在每次按下“添加/减去”按钮时更改。这是我在这里的目标。有什么建议吗?你能发布你当前的addToLap()和subToLap()方法吗?