简体   繁体   English

使用 GUI(Java)递增/递减

[英]Increment/decrement with GUI(Java)

I am supposed to implement an application to the user that has 2 buttons(Increment/decrement) and a label.我应该为用户实现一个具有 2 个按钮(增量/减量)和一个标签的应用程序。 When increment is pressed the number increases and decreases by one when decrement is pressed.按下增加时,数字增加一,按下减少时减少一。 The number starts at 50. I have it to where it shows the buttons and they work, but they work on 2 different variables, so their is 2 number printed to the screen instead of 1. My question is how can i make the button act on only one number.数字从 50 开始。我有它显示按钮的地方,它们可以工作,但它们处理 2 个不同的变量,所以它们是打印到屏幕上的 2 个数字而不是 1。我的问题是如何让按钮起作用只有一个号码。 I have seen people use push etc. but is there another way to do this by passing in a value to both or something?我见过人们使用 push 等,但是有没有另一种方法可以通过将值传递给两者或其他东西来做到这一点? Thanks谢谢

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

public class ButtonModifier 
{
    public static void main(String[] args)
    {
        JFrame frame = new JFrame();

        FlowLayout flow = new FlowLayout();
        frame.getContentPane().setLayout(flow);

        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setSize(400,300);
        frame.setTitle("Button Modifier");

        IncrementPanel panel = new IncrementPanel();
        DecrementPanel panel1 = new DecrementPanel();

        frame.add(panel);
        frame.add(panel1);

        frame.setVisible(true);
    }
}

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

public class DecrementPanel extends JPanel
{
    private JButton button1;
    private JLabel label;
    private int number = 50;

    public DecrementPanel()
    {
        button1 = new JButton("Decrement");
        button1.addActionListener(new /*DecrementPanel.*/ButtonListener());

        label = new JLabel("" + number);


        this.add(button1);
        this.add(label);
    }

    private class ButtonListener implements ActionListener
    {
        public void actionPerformed(ActionEvent event)
        {
            //int increment = 50;

            number--;

            label.setText("" + number);


        }
    }

}

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

public class IncrementPanel extends JPanel
{
    private JButton button;
    private JLabel label;
    int number = 50;

    public IncrementPanel()
    {
        button = new JButton("Increment");
        button.addActionListener(new ButtonListener());

        label = new JLabel("" + number);

        this.add(button);
        this.add(label);
    }

    private class ButtonListener implements ActionListener
    {
        public void actionPerformed(ActionEvent event)
        {
            //int increment = 50;

            number++;

            label.setText("" + number);
        }
    }

}

I am supposed to implement an application to the user that has 2 buttons(Increment/decrement) and a label."我应该为用户实现一个具有 2 个按钮(增量/减量)和一个标签的应用程序。”

Then why do you have two?那你为什么有两个?

 IncrementPanel panel = new IncrementPanel();
 DecrementPanel panel1 = new DecrementPanel();

Just use one and change the text on that one只需使用一个并更改该文本上的文本

Should be more like this应该更像这样

public class ButtonModifier extends JFrame {
    private JLabel numberLabel = new JLable("50");
    private JButton decrease = new JButton("-1");
    private JButton increase = new JButton("+1");
    private static int num = 50;

    public ButtonModifier(){
        setLayout(new GridLayout(1, 3));
        add(increase);
        add(numberLabel);
        add(decrease);

        increase.addActionListener(new ActionListener(){
            public void actionPerformed(ActionEvent e){
                num++;
                numLabel.setText("" + num);               
            }
        });
        decrease.addActionListener(new ActionListener(){
            public void actionPerformed(ActionEvent e){
                num--;
                numLabel.setText("" + num);               
            }
        }); 
    }

    public static void main(String[] args){
        JFrame frame = ButtonModifier();

        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setSize(400,300);
        frame.setTitle("Button Modifier");
        frame.setVisible(true);
    }
}

You should have one JLabel which will display the only number in your program.您应该有一个JLabel ,它将在您的程序中显示唯一的数字。

Then your two buttons will do operations on that number and update the label.然后你的两个按钮将对该号码进行操作并更新标签。

Your mistake is that each Panel has its own number and its own Label to display the number.你的错误是每个面板都有自己的编号和自己的标签来显示编号。

public class ButtonModifier {
    private static int number = 50;
    private static JLabel label;

    public static void main(String[] args) {
        JFrame frame = new JFrame();

        label = new JLabel("" + number);

        // <SNIP>

        JButton increment = new JButton("Increment");
        increment.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent event) {
                number++;
                label.setText("" + number);
            }
        }

        JButton decrement = new JButton("Increment");
        increment.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent event) {
                number--;
                label.setText("" + number);
            }
        }

        frame.add(label);
        frame.add(increment);
        frame.add(decrement);

        frame.setVisible(true);
    }
}

An important note: Swing is not thread-safe , and all the operations with GUI components must be performed on Event Dispatch Thread .重要说明: Swing 不是线程安全的,所有与 GUI 组件相关的操作都必须Event Dispatch Thread上执行。 So your main must actually look this way:所以你的main实际上必须是这样的:

public static void main(String[] args) {
    SwingUtilities.invokeLater(new Runnable() {
        public void run() {
            // Here you create the frame and all the components
        }
    });
}

Create the JLabel in the main function.在主函数中创建 JLabel。 Have the incrementPanel and DecrementPanel classes constructors take a JLabel as an argument that they store as a private variable.让 incrementPanel 和 DecrementPanel 类构造函数将 JLabel 作为参数存储为私有变量。 The ButtonListeners csn also be passed the JLabel as an argument. ButtonListeners csn 也将 JLabel 作为参数传递。 Now the button listeners csn update a common JLabel.现在按钮监听器 csn 更新了一个通用的 JLabel。 Now, you can improve things by combining the code of IncrementPanel and DecrementPanel classes by passing an int in the constructor indicating the increment of +1 or -1.现在,您可以通过在构造函数中传递一个表示 +1 或 -1 增量的 int 来组合 IncrementPanel 和 DecrementPanel 类的代码来改进事情。 A quick and dirty way to implement the functionality is through the use of anonymous classes implementing button listeners within a single monolithic class.实现该功能的一种快速而肮脏的方法是使用匿名类在单个整体类中实现按钮侦听器。

Take a look at this program :看看这个程序:

import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;

public class IncDecApp extends JFrame {

    private JButton incBtn = new JButton("Increment");
    private JButton decBtn = new JButton("Decrement");
    private JPanel lowPanel = new JPanel(new FlowLayout(FlowLayout.CENTER));
    private JLabel showLbl = new JLabel("00", JLabel.CENTER);
    private Font myFont = new Font("Tahoma", Font.BOLD, 60);

    private int valueInt;

    public IncDecApp() {

        setTitle("IncDec Application =)");
        setDefaultCloseOperation(EXIT_ON_CLOSE);

        lowPanel.add(incBtn);
        lowPanel.add(decBtn);

        incBtn.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                valueInt = Integer.parseInt(showLbl.getText());
                valueInt++;
                if (valueInt >= 10) {
                    showLbl.setText(String.valueOf(valueInt));
                } else {
                    showLbl.setText("0" + String.valueOf(valueInt));
                }
            }
        });

        decBtn.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                valueInt = Integer.parseInt(showLbl.getText());
                if (valueInt > 0) {
                    valueInt--;
                }
                if (valueInt >= 10) {
                    showLbl.setText(String.valueOf(valueInt));
                } else {
                    showLbl.setText("0" + String.valueOf(valueInt));
                }
            }
        });

        showLbl.setFont(myFont);

        add(showLbl, BorderLayout.CENTER);
        add(lowPanel, BorderLayout.SOUTH);
        pack();
        setLocationRelativeTo(null);
        setVisible(true);

    }

    public static void main(String[] args) {
        new IncDecApp();
    }

}
import java.awt.*;
import java.awt.event.*;
import java.applet.*;

public class ButtonApplet extends Applet implements ActionListener{

    Button buttonInc, buttonDec;
    int x=0;

    public void init(){

        buttonInc=new Button("Increment");
        buttonDec=new Button("Decrement");
        buttonInc.addActionListener(this);
        buttonDec.addActionListener(this);

        add(buttonInc);
        add(buttonDec);
    }

    public void paint(Graphics g){

        g.drawString("Count is : "+x,50,100);
    }

    public void actionPerformed(ActionEvent ev){

            if(ev.getSource() == buttonInc)
            {
                x++;
                repaint();
            }
            else if(ev.getSource() == buttonDec){
                x--;
                repaint();
            }
        }
}

Make Java GUI Application using AWT使用 AWT 制作 Java GUI 应用程序

You are required to make one label (Count), one textfield, one button (Increment), one button (Decrement) and one button (Close)您需要制作一个标签(Count)、一个文本框、一个按钮(Increment)、一个按钮(Decrement)和一个按钮(Close)

When the increment button is clicked, you need to increment the value in textfield, value should be incremented again and again when the button is clicked单击增量按钮时,需要增加文本字段中的值,单击该按钮时应一次又一次地增加值

When the decrement button is clicked, you need to decrement the value in textfield, value should be decremented again and again when the button is clicked单击递减按钮时,需要递减文本字段中的值,单击按钮时应一次又一次递减值

When the close button is clicked, you need to close the AWT Frame当关闭按钮被点击时,需要关闭AWT Frame

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM