Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/318.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 Swing:让我的简单GUI正常工作的问题_Java_Swing - Fatal编程技术网

Java Swing:让我的简单GUI正常工作的问题

Java Swing:让我的简单GUI正常工作的问题,java,swing,Java,Swing,我试图构建一个计数器,它使用GUI简单地递增或递减一个整数。请有人帮我找出哪里出了问题。我的按钮不增加或减少起始整数值0 import javax.swing.JFrame; import javax.swing.JTextArea; import javax.swing.JTextField; import java.awt.BorderLayout; import java.awt.Container; import java.aw

我试图构建一个计数器,它使用GUI简单地递增或递减一个整数。请有人帮我找出哪里出了问题。我的按钮不增加或减少起始整数值0

    import javax.swing.JFrame;
     import javax.swing.JTextArea;
     import javax.swing.JTextField;

     import java.awt.BorderLayout;
     import java.awt.Container;
     import java.awt.GridLayout;
     import java.awt.event.ActionEvent;
     import java.awt.event.ActionListener;

     import javax.swing.*;

     import org.omg.CORBA.TCKind;

     public class MainFrame extends JFrame {
    // Field Variables
    private int counter;
    private int currentNumber = 0;


    // Methods
    public void setCounter(int counter){
        currentNumber = counter;
    }

    public int getCounter(){
        return currentNumber;
    }

    public void IncrementCounter() {
        currentNumber++;

    }

    public void DecrementCounter() {
        currentNumber = 222;
    }

    public MainFrame(String title) {
        super(title);

        // SetLayout
        setLayout(new GridLayout(2, 2));

        // Add Swing components
        final JTextField tCounter = new JTextField();
        tCounter.setEditable(false);
        tCounter.setText(currentNumber + "");

        JTextField label = new JTextField();
        label.setText("The Count: ");
        label.setEditable(false);

        JButton btn1 = new JButton("up");
        JButton btn2 = new JButton("down");

        // Add components to content pane

        getContentPane().add(btn1);
        getContentPane().add(btn2);
        getContentPane().add(label);
        getContentPane().add(tCounter);

        // Add btn1 ad btn2 behaviour.
        btn1.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent arg0) {
                IncrementCounter();
            }
        });

        // Add btn2 Behaviour
        btn2.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                DecrementCounter();

            }
        });

    }
}

无论何时更改计数,您都必须更新标签t计数器。为此,将tCounter变量转换为字段,添加updateLabel()方法,从递增计数和递减计数调用它。也可以从构造函数调用它来初始化文本

JTextField tCounter=new JTextField();

void updateLabel(){
  tCounter.setText(currentNumber+"");
}


public void IncrementCounter() {
  currentNumber++;
  updateLabel()
}

...
更新代码:

package foo;

import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JTextField;

public class MainFrame extends JFrame {
// Field Variables
private int counter;
private int currentNumber = 0;

// Methods
public void setCounter(int counter) {
    currentNumber = counter;
}

public int getCounter() {
    return currentNumber;
}

public void IncrementCounter() {
    currentNumber++;
    updateLabel();
}

void updateLabel() {
    tCounter.setText(currentNumber + "");
}

public void DecrementCounter() {
    currentNumber = 222;
    updateLabel();
}

public static void main(String... args) {
    MainFrame frame = new MainFrame("Foo");
    frame.pack();
    frame.setVisible(true);
}

final JTextField tCounter = new JTextField();

public MainFrame(String title) {
    super(title);

    // SetLayout
    setLayout(new GridLayout(2, 2));

    // Add Swing components
    tCounter.setEditable(false);
    updateLabel();

    JTextField label = new JTextField();
    label.setText("The Count: ");
    label.setEditable(false);

    JButton btn1 = new JButton("up");
    JButton btn2 = new JButton("down");

    // Add components to content pane

    getContentPane().add(btn1);
    getContentPane().add(btn2);
    getContentPane().add(label);
    getContentPane().add(tCounter);

    // Add btn1 ad btn2 behaviour.
    btn1.addActionListener(new ActionListener() {

        @Override
        public void actionPerformed(ActionEvent arg0) {
            IncrementCounter();
        }
    });

    // Add btn2 Behaviour
    btn2.addActionListener(new ActionListener() {

        @Override
        public void actionPerformed(ActionEvent e) {
            DecrementCounter();

        }
    });

}
}

代码中有两个问题。首先,您从未实际更新标签。这不是单独完成的,必须在
actionPerformed()
方法中完成。第二,必须在任何要更新的组件上使用方法
repaint()
。下面是在程序中运行的
actionPerformed()
方法的一个版本

// Add btn1 ad btn2 behaviour.
    btn1.addActionListener(new ActionListener() {

        @Override
        public void actionPerformed(ActionEvent arg0) {
            IncrementCounter();
            tCounter.setText(currentNumber + "");
            tCounter.repaint();
        }
    });

    // Add btn2 Behaviour
    btn2.addActionListener(new ActionListener() {

        @Override
        public void actionPerformed(ActionEvent e) {
            DecrementCounter();
            tCounter.setText(currentNumber + "");
            tCounter.repaint();
        }
    });

我建议您研究一下如何在Java中更新swing。

@user3274549:通过调用JLabel上的
setText(…)
,使用您想要显示的新文本。@user3274549:但是,由于您似乎试图在JTextField中显示您的计数,这个建议可能有点不合适。如果是这样,那么您需要更新JTextField,然后再次调用
setText(…)
。谢谢。我完全按照你的建议做了,这对我很有意义。但是现在当我点击我的任何一个按钮时,我得到了异常…不确定发生了什么。我只是自己尝试了一下。我没有任何例外。你在这段时间对你的代码做了一些花哨的修改吗?在答案中添加了我的代码。