1

I am supposed to implement an application to the user that has 2 buttons(Increment/decrement) and a label. 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. I have seen people use push etc. but is there another way to do this by passing in a value to both or something? 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);
        }
    }

}
4

6 回答 6

1

I am supposed to implement an application to the user that has 2 buttons(Increment/decrement) and a label."

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);
    }
}
于 2013-11-12T18:38:19.240 回答
0

You should have one JLabel which will display the only number in your program.

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. So your main must actually look this way:

public static void main(String[] args) {
    SwingUtilities.invokeLater(new Runnable() {
        public void run() {
            // Here you create the frame and all the components
        }
    });
}
于 2013-11-12T18:36:57.363 回答
0

在主函数中创建 JLabel。让 incrementPanel 和 DecrementPanel 类的构造函数将 JLabel 作为参数存储为私有变量。ButtonListeners csn 也作为参数传递给 JLabel。现在按钮侦听器 csn 更新了一个通用的 JLabel。现在,您可以通过在构造函数中传递一个指示 +1 或 -1 的增量的 int 来组合 IncrementPanel 和 DecrementPanel 类的代码来改进事情。实现该功能的一种快速而肮脏的方法是使用匿名类在单个整体类中实现按钮侦听器。

于 2013-11-12T18:50:48.373 回答
0

看看这个程序:

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();
    }

}
于 2013-11-12T18:56:25.250 回答
-1
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();
            }
        }
}
于 2019-01-02T21:17:53.307 回答
-1

使用 AWT 制作 Java GUI 应用程序

您需要制作一个标签(计数)、一个文本字段、一个按钮(增量)、一个按钮(减量)和一个按钮(关闭)

单击递增按钮时,需要递增文本字段中的值,单击按钮时值应一次又一次递增

点击递减按钮时,需要递减textfield中的值,点击按钮时,值应该一次又一次递减

单击关闭按钮时,需要关闭 AWT 框架

于 2020-08-12T16:08:38.740 回答