簡體   English   中英

使用 GUI(Java)遞增/遞減

[英]Increment/decrement with GUI(Java)

我應該為用戶實現一個具有 2 個按鈕(增量/減量)和一個標簽的應用程序。 按下增加時,數字增加一,按下減少時減少一。 數字從 50 開始。我有它顯示按鈕的地方,它們可以工作,但它們處理 2 個不同的變量,所以它們是打印到屏幕上的 2 個數字而不是 1。我的問題是如何讓按鈕起作用只有一個號碼。 我見過人們使用 push 等,但是有沒有另一種方法可以通過將值傳遞給兩者或其他東西來做到這一點? 謝謝

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

}

我應該為用戶實現一個具有 2 個按鈕(增量/減量)和一個標簽的應用程序。”

那你為什么有兩個?

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

只需使用一個並更改該文本上的文本

應該更像這樣

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

您應該有一個JLabel ,它將在您的程序中顯示唯一的數字。

然后你的兩個按鈕將對該號碼進行操作並更新標簽。

你的錯誤是每個面板都有自己的編號和自己的標簽來顯示編號。

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

重要說明: Swing 不是線程安全的,所有與 GUI 組件相關的操作都必須Event Dispatch Thread上執行。 所以你的main實際上必須是這樣的:

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

在主函數中創建 JLabel。 讓 incrementPanel 和 DecrementPanel 類構造函數將 JLabel 作為參數存儲為私有變量。 ButtonListeners csn 也將 JLabel 作為參數傳遞。 現在按鈕監聽器 csn 更新了一個通用的 JLabel。 現在,您可以通過在構造函數中傳遞一個表示 +1 或 -1 增量的 int 來組合 IncrementPanel 和 DecrementPanel 類的代碼來改進事情。 實現該功能的一種快速而骯臟的方法是使用匿名類在單個整體類中實現按鈕偵聽器。

看看這個程序:

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

使用 AWT 制作 Java GUI 應用程序

您需要制作一個標簽(Count)、一個文本框、一個按鈕(Increment)、一個按鈕(Decrement)和一個按鈕(Close)

單擊增量按鈕時,需要增加文本字段中的值,單擊該按鈕時應一次又一次地增加值

單擊遞減按鈕時,需要遞減文本字段中的值,單擊按鈕時應一次又一次遞減值

當關閉按鈕被點擊時,需要關閉AWT Frame

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM