gpt4 book ai didi

java - 如何修复 Action 监听器输出?

转载 作者:行者123 更新时间:2023-12-01 22:48:44 24 4
gpt4 key购买 nike

这就是我使用 previous question 中教给我的 actionlistener 所做的事情。 。我首先要为我的代码很长表示歉意。我已经稍微修剪了一下。

package testpanel;

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
import javax.swing.border.Border;

public class TestPanel {

JFrame Card = new JFrame();
GridBagConstraints c = new GridBagConstraints();
Border etch = BorderFactory.createEtchedBorder(Color.white,Color.gray);

JButton up;
JButton up_1; JButton up_2; JButton up_3; JButton up_4;
JButton up_5; JButton up_6; JButton up_7; JButton up_8;
JButton down;
JButton down_1; JButton down_2; JButton down_3; JButton down_4;
JButton down_5; JButton down_6; JButton down_7; JButton down_8;

JTextField number;
JTextField number_1; JTextField number_2; JTextField number_3;
JTextField number_4; JTextField number_5; JTextField number_6;
JTextField number_7; JTextField number_8;

int value1000 = 0;
int value500 = 0;
int value200 = 0;
int value100 = 0;
int value50 = 0;
int value20 = 0;
int value10 = 0;
int value5 = 0;
int value1 = 0;
int valuecoins = 0;

ActionListener upaction;
ActionListener downaction;

public TestPanel(){
Card.setVisible(true);
Card.setSize(522,500);
Card.setTitle("Frequency Distribution");
Card.setResizable(false);

final Toolkit toolkit = Toolkit.getDefaultToolkit();
Dimension dimension = Toolkit.getDefaultToolkit().getScreenSize();
int x=(int)((dimension.getWidth() - Card.getWidth())/4);
int y=(int)((dimension.getHeight() - Card.getHeight())/2);

Card.setLocation(x, y);
Card.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);


JPanel centerpanel = new JPanel();
centerpanel.setBorder(etch);
centerpanel.setPreferredSize(new Dimension(0,324));
centerpanel.setBackground(new Color(0,120,0));
centerpanel.setLayout(new GridBagLayout());

JLabel lbl1000 = new JLabel("1,000.00");
lbl1000.setFont(new Font("Calibri", Font.BOLD, 20));
lbl1000.setForeground(Color.YELLOW);
c.fill = GridBagConstraints.WEST;
c.weightx = 0.5;
c.weighty = 0;
c.gridx = 0;
c.gridy = 0;
c.insets = new Insets(0,30,0,0);
centerpanel.add(lbl1000,c);

JLabel multiply = new JLabel("X");
multiply.setFont(new Font("Calibri", Font.BOLD, 20));
multiply.setForeground(Color.YELLOW);
c.fill = GridBagConstraints.BOTH;
c.weightx = 0.5;
c.weighty = 0;
c.gridx = 1;
c.gridy = 0;
c.insets = new Insets(0,0,0,0);
centerpanel.add(multiply,c);

final JTextField number = new JTextField();
number.setText(value100+"");
number.setColumns(3);
number.setFont(new Font("Calibri", Font.BOLD, 20));
number.setForeground(Color.BLACK);
c.fill = GridBagConstraints.BOTH;
c.weightx = 0.5;
c.weighty = 0;
c.gridx = 2;
c.gridy = 0;
c.insets = new Insets(0,0,0,0);
centerpanel.add(number,c);

upaction = new ActionListener() {
public void actionPerformed(ActionEvent e) {

if(e.getSource() == up){
value100++;
if(value100>0)
{
number.setText(value100+"");
down.setEnabled(true);
}}
if(e.getSource() == up_1){
value500++;
if(value500>0)
{
number_1.setText(value500+"");
down_1.setEnabled(true);
}}
if(e.getSource() == up_2){
value200++;
if(value200>0)
{
number_2.setText(value200+"");
down_2.setEnabled(true);
}}
if(e.getSource() == up_3){
value100++;
if(value100>0)
{
number_3.setText(value100+"");
down_3.setEnabled(true);
}}
if(e.getSource() == up_4){
value50++;
if(value50>0)
{
number_4.setText(value50+"");
down_4.setEnabled(true);
}}

}
};

downaction = new ActionListener() {
public void actionPerformed(ActionEvent e) {

if(e.getSource() == down){
value1000--;
if(value1000>0)
{number.setText(value1000+"");}
if(value1000==0)
{
number.setText(value1000+"");
down.setEnabled(false);
value1000=0;
}}
if(e.getSource() == down_1){
value500--;
if(value500>0)
{number_1.setText(value500+"");}
if(value500==0)
{
number_1.setText(value500+"");
down_1.setEnabled(false);
value500=0;
}}

if(e.getSource() == down_2){
value200--;
if(value200>0)
{number_2.setText(value200+"");}
if(value200==0)
{
number_2.setText(value200+"");
down_2.setEnabled(false);
value200=0;
}}
if(e.getSource() == down_3){
value100--;
if(value100>0)
{number_3.setText(value100+"");}
if(value100==0)
{
number_3.setText(value100+"");
down_3.setEnabled(false);
value100=0;
}}
if(e.getSource() == down_4){
value50--;
if(value50>0)
{number_4.setText(value50+"");}
if(value50==0)
{
number_4.setText(value50+"");
down_4.setEnabled(false);
value50=0;
}}


}
};

up = new JButton("up");
up.setBackground(new Color(224,223,227));
up.setPreferredSize(new Dimension(5,15));
up.addActionListener(upaction);


down = new JButton("down");
down.setBackground(new Color(224,223,227));
down.setPreferredSize(new Dimension(5,15));
down.setEnabled(false);
down.addActionListener(downaction);

JPanel updownpanel = new JPanel();
updownpanel.setPreferredSize(new Dimension(10,10));
updownpanel.setLayout(new BorderLayout());
updownpanel.add(up, BorderLayout.NORTH);
updownpanel.add(down, BorderLayout.SOUTH);
c.fill = GridBagConstraints.BOTH;
c.weightx = 0.5;
c.weighty = 0;
c.gridx = 3;
c.gridy = 0;
c.insets = new Insets(0,0,0,0);
centerpanel.add(updownpanel,c);

JLabel equals = new JLabel("=");
equals.setFont(new Font("Calibri", Font.BOLD, 20));
equals.setForeground(Color.YELLOW);
c.fill = GridBagConstraints.BOTH;
c.weightx = 1;
c.weighty = 0;
c.gridx = 4;
c.gridy = 0;
c.insets = new Insets(0,30,0,0);
centerpanel.add(equals,c);

JLabel total = new JLabel();
total.setText(value100+".00");
total.setFont(new Font("Calibri", Font.BOLD, 20));
total.setForeground(Color.YELLOW);
c.fill = GridBagConstraints.EAST;
c.weightx = 1;
c.weighty = 0;
c.gridx = 5;
c.gridy = 0;
c.insets = new Insets(0,30,0,0);
centerpanel.add(total,c);

//-----------------------------------------------------------------------------------------

JLabel lbl500 = new JLabel("500.00");
lbl500.setFont(new Font("Calibri", Font.BOLD, 20));
lbl500.setForeground(Color.YELLOW);
c.fill = GridBagConstraints.WEST;
c.weightx = 0.5;
c.weighty = 0;
c.gridx = 0;
c.gridy = 1;
c.insets = new Insets(0,30,0,0);
centerpanel.add(lbl500,c);

JLabel multiply_1 = new JLabel("X");
multiply_1.setFont(new Font("Calibri", Font.BOLD, 20));
multiply_1.setForeground(Color.YELLOW);
c.fill = GridBagConstraints.BOTH;
c.weightx = 0.5;
c.weighty = 0;
c.gridx = 1;
c.gridy = 1;
c.insets = new Insets(0,0,0,0);
centerpanel.add(multiply_1,c);

number_1 = new JTextField();
number_1.setText(value500+"");
number_1.setColumns(3);
number_1.setFont(new Font("Calibri", Font.BOLD, 20));
number_1.setForeground(Color.BLACK);
c.fill = GridBagConstraints.BOTH;
c.weightx = 0.5;
c.weighty = 0;
c.gridx = 2;
c.gridy = 1;
c.insets = new Insets(0,0,0,0);
centerpanel.add(number_1,c);

up_1 = new JButton("up");
up_1.setBackground(new Color(224,223,227));
up_1.setPreferredSize(new Dimension(5,15));
up_1.addActionListener(upaction);

down_1 = new JButton("down");
down_1.setBackground(new Color(224,223,227));
down_1.setPreferredSize(new Dimension(5,15));
down_1.setEnabled(false);
down_1.addActionListener(downaction);

JPanel updownpanel_1 = new JPanel();
updownpanel_1.setPreferredSize(new Dimension(10,10));
updownpanel_1.setLayout(new BorderLayout());
updownpanel_1.add(up_1, BorderLayout.NORTH);
updownpanel_1.add(down_1, BorderLayout.SOUTH);
c.fill = GridBagConstraints.BOTH;
c.weightx = 0.5;
c.weighty = 0;
c.gridx = 3;
c.gridy = 1;
c.insets = new Insets(0,0,0,0);
centerpanel.add(updownpanel_1,c);

JLabel equals_1 = new JLabel("=");
equals_1.setFont(new Font("Calibri", Font.BOLD, 20));
equals_1.setForeground(Color.YELLOW);
c.fill = GridBagConstraints.BOTH;
c.weightx = 1;
c.weighty = 0;
c.gridx = 4;
c.gridy = 1;
c.insets = new Insets(0,30,0,0);
centerpanel.add(equals_1,c);

JLabel total_1 = new JLabel();
total_1.setText(value500+".00");
total_1.setFont(new Font("Calibri", Font.BOLD, 20));
total_1.setForeground(Color.YELLOW);
c.fill = GridBagConstraints.EAST;
c.weightx = 1;
c.weighty = 0;
c.gridx = 5;
c.gridy = 1;
c.insets = new Insets(0,30,0,0);
centerpanel.add(total_1,c);

//-----------------------------------------------------------------------------------------

JLabel lbl200 = new JLabel("200.00");
lbl200.setFont(new Font("Calibri", Font.BOLD, 20));
lbl200.setForeground(Color.YELLOW);
c.fill = GridBagConstraints.WEST;
c.weightx = 0.5;
c.weighty = 0;
c.gridx = 0;
c.gridy = 2;
c.insets = new Insets(0,30,0,0);
centerpanel.add(lbl200,c);

JLabel multiply_2 = new JLabel("X");
multiply_2.setFont(new Font("Calibri", Font.BOLD, 20));
multiply_2.setForeground(Color.YELLOW);
c.fill = GridBagConstraints.BOTH;
c.weightx = 0.5;
c.weighty = 0;
c.gridx = 1;
c.gridy = 2;
c.insets = new Insets(0,0,0,0);
centerpanel.add(multiply_2,c);

number_2 = new JTextField();
number_2.setText(value200+"");
number_2.setColumns(3);
number_2.setFont(new Font("Calibri", Font.BOLD, 20));
number_2.setForeground(Color.BLACK);
c.fill = GridBagConstraints.BOTH;
c.weightx = 0.5;
c.weighty = 0;
c.gridx = 2;
c.gridy = 2;
c.insets = new Insets(0,0,0,0);
centerpanel.add(number_2,c);

up_2 = new JButton("up");
up_2.setBackground(new Color(224,223,227));
up_2.setPreferredSize(new Dimension(5,15));
up_2.addActionListener(upaction);

down_2 = new JButton("down");
down_2.setBackground(new Color(224,223,227));
down_2.setPreferredSize(new Dimension(5,15));
down_2.setEnabled(false);
down_2.addActionListener(downaction);

JPanel updownpanel_2 = new JPanel();
updownpanel_2.setPreferredSize(new Dimension(10,10));
updownpanel_2.setLayout(new BorderLayout());
updownpanel_2.add(up_2, BorderLayout.NORTH);
updownpanel_2.add(down_2, BorderLayout.SOUTH);
c.fill = GridBagConstraints.BOTH;
c.weightx = 0.5;
c.weighty = 0;
c.gridx = 3;
c.gridy = 2;
c.insets = new Insets(0,0,0,0);
centerpanel.add(updownpanel_2,c);

JLabel equals_2 = new JLabel("=");
equals_2.setFont(new Font("Calibri", Font.BOLD, 20));
equals_2.setForeground(Color.YELLOW);
c.fill = GridBagConstraints.BOTH;
c.weightx = 1;
c.weighty = 0;
c.gridx = 4;
c.gridy = 2;
c.insets = new Insets(0,30,0,0);
centerpanel.add(equals_2,c);

JLabel total_2 = new JLabel();
total_2.setText(value200+".00");
total_2.setFont(new Font("Calibri", Font.BOLD, 20));
total_2.setForeground(Color.YELLOW);
c.fill = GridBagConstraints.EAST;
c.weightx = 1;
c.weighty = 0;
c.gridx = 5;
c.gridy = 2;
c.insets = new Insets(0,30,0,0);
centerpanel.add(total_2,c);

//-----------------------------------------------------------------------------------------

JLabel lbl100 = new JLabel("100.00");
lbl100.setFont(new Font("Calibri", Font.BOLD, 20));
lbl100.setForeground(Color.YELLOW);
c.fill = GridBagConstraints.WEST;
c.weightx = 0.5;
c.weighty = 0;
c.gridx = 0;
c.gridy = 3;
c.insets = new Insets(0,30,0,0);
centerpanel.add(lbl100,c);

JLabel multiply_3 = new JLabel("X");
multiply_3.setFont(new Font("Calibri", Font.BOLD, 20));
multiply_3.setForeground(Color.YELLOW);
c.fill = GridBagConstraints.BOTH;
c.weightx = 0.5;
c.weighty = 0;
c.gridx = 1;
c.gridy = 3;
c.insets = new Insets(0,0,0,0);
centerpanel.add(multiply_3,c);

number_3 = new JTextField();
number_3.setText(value100+"");
number_3.setColumns(3);
number_3.setFont(new Font("Calibri", Font.BOLD, 20));
number_3.setForeground(Color.BLACK);
c.fill = GridBagConstraints.BOTH;
c.weightx = 0.5;
c.weighty = 0;
c.gridx = 2;
c.gridy = 3;
c.insets = new Insets(0,0,0,0);
centerpanel.add(number_3,c);

up_3 = new JButton("up");
up_3.setBackground(new Color(224,223,227));
up_3.setPreferredSize(new Dimension(5,15));
up_3.addActionListener(upaction);

down_3 = new JButton("down");
down_3.setBackground(new Color(224,223,227));
down_3.setPreferredSize(new Dimension(5,15));
down_3.setEnabled(false);
down_3.addActionListener(downaction);

JPanel updownpanel_3 = new JPanel();
updownpanel_3.setPreferredSize(new Dimension(10,10));
updownpanel_3.setLayout(new BorderLayout());
updownpanel_3.add(up_3, BorderLayout.NORTH);
updownpanel_3.add(down_3, BorderLayout.SOUTH);
c.fill = GridBagConstraints.BOTH;
c.weightx = 0.5;
c.weighty = 0;
c.gridx = 3;
c.gridy = 3;
c.insets = new Insets(0,0,0,0);
centerpanel.add(updownpanel_3,c);

JLabel equals_3 = new JLabel("=");
equals_3.setFont(new Font("Calibri", Font.BOLD, 20));
equals_3.setForeground(Color.YELLOW);
c.fill = GridBagConstraints.BOTH;
c.weightx = 1;
c.weighty = 0;
c.gridx = 4;
c.gridy = 3;
c.insets = new Insets(0,30,0,0);
centerpanel.add(equals_3,c);

JLabel total_3 = new JLabel();
total_3.setText(value100+".00");
total_3.setFont(new Font("Calibri", Font.BOLD, 20));
total_3.setForeground(Color.YELLOW);
c.fill = GridBagConstraints.EAST;
c.weightx = 1;
c.weighty = 0;
c.gridx = 5;
c.gridy = 3;
c.insets = new Insets(0,30,0,0);
centerpanel.add(total_3,c);

JLabel coins = new JLabel("Coins:");
coins.setFont(new Font("Calibri", Font.BOLD, 20));
coins.setForeground(Color.YELLOW);
c.fill = GridBagConstraints.BOTH;
c.weightx = 1;
c.weighty = 0;
c.gridx = 4;
c.gridy = 9;
c.insets = new Insets(0,0,0,0);
centerpanel.add(coins,c);

JTextField txttotal = new JTextField();
txttotal.setText(valuecoins+"");
txttotal.setColumns(6);
txttotal.setFont(new Font("Calibri", Font.BOLD, 20));
txttotal.setForeground(Color.BLACK);
c.fill = GridBagConstraints.EAST;
c.weightx = 1;
c.weighty = 0;
c.gridx = 5;
c.gridy = 9;
c.insets = new Insets(0,0,0,35);
centerpanel.add(txttotal,c);

JLabel divider = new JLabel("---------------------");
divider.setFont(new Font("Calibri", Font.BOLD, 20));
divider.setForeground(Color.GRAY);
c.fill = GridBagConstraints.BOTH;
c.weightx = 1;
c.weighty = 0;
c.gridx = 5;
c.gridy = 10;
c.insets = new Insets(0,0,0,0);
centerpanel.add(divider,c);

JLabel txtCoinTotal = new JLabel();
txtCoinTotal.setText(valuecoins+".00");
txtCoinTotal.setFont(new Font("Calibri", Font.BOLD, 20));
txtCoinTotal.setForeground(Color.YELLOW);
c.fill = GridBagConstraints.EAST;
c.weightx = 1;
c.weighty = 0;
c.gridx = 5;
c.gridy = 11;
c.insets = new Insets(0,0,0,35);
centerpanel.add(txtCoinTotal,c);


Card.add(centerpanel);


}

public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
TestPanel test = new TestPanel();
}
});
}
}

我这里有 4 行数字,其中有一个 textfield 和一个用于递增的向上和向下按钮。它按照我喜欢的方式工作,但是如果您运行程序并从上到下单击向上按钮一次,则第 4 行将是 2 而不是 1。即使您添加行,第 4 行始终会获得 2 而不是 1。如果您从底部开始单击,第一行将为 2。如果不是第四行,则第一个获得 2。我没有发现我的代码有任何问题,但就是这样。即使我正确输入了代码,第一行的后退按钮也不起作用。我已经检查了我的代码两次以上,但我没有看到任何可以使它成为这样的东西。

最佳答案

如果我们比较 up 操作的代码...

if (e.getSource() == up) {
value100++;
if (value100 > 0) {
number.setText(value100 + "");
down.setEnabled(true);
}
}
//...
if (e.getSource() == up_3) {
value100++;
if (value100 > 0) {
number_3.setText(value100 + "");
down_3.setEnabled(true);
}
}

使用向下操作的代码

if (e.getSource() == down) {
value1000--;
if (value1000 > 0) {
number.setText(value1000 + "");
}
if (value1000 == 0) {
number.setText(value1000 + "");
down.setEnabled(false);
value1000 = 0;
}
}
//...
if (e.getSource() == down_3) {
value100--;
if (value100 > 0) {
number_3.setText(value100 + "");
}
if (value100 == 0) {
number_3.setText(value100 + "");
down_3.setEnabled(false);
value100 = 0;
}
}

我们可以看到差异...up正在修改value100,而down正在修改value1000,但 up_3down_3 正在修改 value100...

趁我还有时间...

这就是您的 UI 在我的电脑上的样子...

BadScreen

现在,我坐在这里挠头“......”

现在,如果我用 pack 替换 setSize (在构造函数末尾调用),我会得到...

BadScreen

现在,如果我删除所有 setPreferredSize 调用,我会得到...

BetterScreen

仍然需要一些工作,但这是一个更好的结果(恕我直言)......

看看Should I avoid the use of set(Preferred|Maximum|Minimum)Size methods in Java Swing?关于该主题的决定...

哦,我不知道这是否重要,但你可能想看看 How to use spinners

关于java - 如何修复 Action 监听器输出?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24993255/

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