Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/337.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/1/typo3/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 无法使用JButton递增/递减JTextField的值_Java_Swing_Jbutton_Actionlistener_Jtextfield - Fatal编程技术网

Java 无法使用JButton递增/递减JTextField的值

Java 无法使用JButton递增/递减JTextField的值,java,swing,jbutton,actionlistener,jtextfield,Java,Swing,Jbutton,Actionlistener,Jtextfield,我在使用JButtons递增/递减已设置的JTextField时遇到问题 我知道actionlistener是必要的,但我不知道如何设置它。我还必须为多个JButton设置此选项,这会使此过程更加复杂 import java.awt.Color; import java.awt.Dimension; import java.awt.GridLayout; import java.awt.event.ActionEvent; import java.awt.event.ActionListener

我在使用JButtons递增/递减已设置的JTextField时遇到问题

我知道actionlistener是必要的,但我不知道如何设置它。我还必须为多个JButton设置此选项,这会使此过程更加复杂

import java.awt.Color;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseListener;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;

public class App1 extends JFrame implements ActionListener, MouseListener
{
    JPanel upper, lower, jp1, jp2, jp3;
    JButton jb1, jb2, jb3, jb4, jb5, jb6;
    JLabel jl1, jl2, jl3;
    JTextField jtf1, jtf2, jtf3;
    int count = 128;

    public static void main(String[] args) {
        App1 a1 = new App1();
    }

    public App1() {

        JFrame myFrame = new JFrame("Color Selector");
        myFrame.setLocationRelativeTo(null);
        myFrame.setSize(new Dimension(600,400));

        GridLayout layout = new GridLayout(2,1);
        myFrame.setLayout(layout);

        JPanel upper = new JPanel();
        upper.setBackground(Color.GRAY);

        JPanel lower = new JPanel(); add(lower);
        lower.setLayout(new BoxLayout(lower, BoxLayout.X_AXIS));
        lower.setBackground(Color.GRAY);
        JPanel jp1 = new JPanel(); lower.add(jp1);
        JPanel jp2 = new JPanel(); lower.add(jp2);
        JPanel jp3 = new JPanel(); lower.add(jp3);
        lower.addMouseListener(this);

        JLabel jl1 = new JLabel("Red"); jp1.add(jl1);
        JLabel jl2 = new JLabel("Green"); jp2.add(jl2);
        JLabel jl3 = new JLabel("Blue"); jp3.add(jl3);

        jp1.setBackground(Color.RED);
        jp2.setBackground(Color.GREEN);
        jp3.setBackground(Color.BLUE);

        JButton jb1 = new JButton("R-"); jp1.add(jb1);
        JTextField jtf1 = new JTextField(6);jp1.add(jtf1);
        jtf1.setText("128");
        JButton jb2 = new JButton("R+"); jp1.add(jb2);
        jb1.addActionListener(this);
        jb2.addActionListener(this);

        JButton jb3 = new JButton("G-"); jp2.add(jb3);
        JTextField jtf2 = new JTextField(6);jp2.add(jtf2);
        jtf2.setText("128");
        JButton jb4 = new JButton("G+"); jp2.add(jb4);
        jb3.addActionListener(this);
        jb4.addActionListener(this);

        JButton jb5 = new JButton("B-"); jp3.add(jb5);
        JTextField jtf3 = new JTextField(6);jp3.add(jtf3);
        jtf3.setText("128");
        JButton jb6 = new JButton("B+"); jp3.add(jb6);
        jb5.addActionListener(this);
        jb6.addActionListener(this);

        myFrame.add(upper);
        myFrame.add(lower);

        myFrame.setVisible(true);
    }

    private void start() {
        // TODO Auto-generated method stub
    }

    private static String setEditable(String string) {
        // TODO Auto-generated method stub
        return null;
    }

    private static String setText(String string) {
        // TODO Auto-generated method stub
        return null;
    }

    private static void add(JPanel lower) {
        // TODO Auto-generated method stub
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        // TODO Auto-generated method stub
    }

    private void rebuild() {
        // TODO Auto-generated method stub
    }
}   

我希望JButton使用JButtons将JTextField“128”设置为+1或-1递增/递减。

actionPerfomed()
方法中,您可以确定使用
e.getActionCommand()
单击了哪个按钮,然后在每种情况下执行您需要执行的操作

@Override
public void actionPerformed(ActionEvent e) {
    String sourceButton = e.getActionCommand(); 
    if (sourceButton.equals("B-")) {
       // code for B- button
    } 
    else if (sourceButton.equals("B+")) {
        // code for B+ button
    }
    // etc
    // etc
}

在这种情况下,为每个按钮提供一个单独的操作侦听器会更简单。例如:
而不是

jb1.addActionListener(this);
jb2.addActionListener(this);

(因此
App1
不需要实现
ActionListener

注意:使用lambda表达式会使上述代码如下所示:

jb1.addActionListener(e -> jtf1.setText(String.valueOf(Integer.valueOf(jtf1.getText())-1)));
jb2.addActionListener(e -> jtf1.setText(String.valueOf(Integer.valueOf(jtf1.getText())+1)));
旁注:您声明了两次变量。

例如,您有一个字段
JTextField jtf1
,后面的
JTextField jtf1=新的JTextField(6)。该字段仍然为空。

我仍然无法增加JTextField(“128”)的实际值。我知道某处有个错误。我查阅了我的笔记和前面的示例,但我就是搞不懂
intresult=Integer.parseInt(jtf3.getText())+1;jtf3.setText(String.valueOf(result))首先,感谢您的帮助。我还是搞不懂。这就是我所拥有的:
stringsourcebutton=e.getActionCommand();if(sourceButton.equals(“B-”){int result=Integer.parseInt(jtf3.getText())-1;jtf3.setText(String.valueOf(result));}否则if(sourceButton.equals(“B+”){int result=Integer.parseInt(jtf3.getText())+1;jtf3.setText(String.valueOf(result));}
那么您的问题是什么呢?注意,我在这里使用了jtf3 textfield变量,我不确定在这种情况下是否要更新该textfield,因为它不起作用。我相信jtf3将适用于这种情况。为什么不使用
JSpinner
?它已经提供了向上/向下按钮来增加值。有关更多信息和工作示例,请阅读上Swing教程的部分。@funkyjelly发布的内容也有效。
jb1.addActionListener(e -> jtf1.setText(String.valueOf(Integer.valueOf(jtf1.getText())-1)));
jb2.addActionListener(e -> jtf1.setText(String.valueOf(Integer.valueOf(jtf1.getText())+1)));