这就是我在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和一个用于递增的向上和向下按钮。它以我喜欢的方式工作,但是如果您运行该程序并从上到下单击一次向上按钮,则第四行将是2而不是1。即使您仍然添加行,第四行也总是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);
    }
}


down操作的代码

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在我的PC上的样子...



现在,我坐在这里挠着脑袋“ ...”

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



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



仍然需要一些工作,但效果要好得多(恕我直言)...

看看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/

10-11 01:17