gpt4 book ai didi

java - 使用 GUI 递增/递减(Java)

转载 作者:行者123 更新时间:2023-12-01 04:10:55 24 4
gpt4 key购买 nike

我应该为用户实现一个具有 2 个按钮(增量/减量)和一个标签的应用程序。当按下增量时,数字增加 1,当按下减量时,数字减少 1。数字从 50 开始。我把它放在显示按钮的位置并且它们可以工作,但是它们作用于 2 个不同的变量,因此它们是打印到屏幕上的 2 个数字而不是 1。我的问题是如何使按钮起作用仅在一个号码上。我见过人们使用推送等,但是有没有另一种方法可以通过向两者或其他东西传递值来做到这一点?谢谢

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."

那你为什么有两个?

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

关于java - 使用 GUI 递增/递减(Java),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19937173/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com