java - 如何使 jButton 操作不仅仅在一次运行中起作用?

标签 java swing variables jbutton

我下面的代码是一个工作代码,我想为我不那么短的代码道歉。我的按钮有问题,因为它们只执行一次操作。我的问题是,当我单击下一步按钮时,我有一个 validateText() 方法来检查 textarea 是否为空。我的问题来了..当面板返回到空的 textarea 时,左侧的按钮将不再起作用。但是,当您关闭程序并再次运行它时,除非您单击带有空 textarea 的下一个按钮,否则该按钮将起作用。有谁可以指出我的错误吗?

package cardlayoutalignment;

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

public class gridbaglayoutdemo {
        JFrame Card = new JFrame();

        FlowLayout flow = new FlowLayout(FlowLayout.RIGHT,2,2);
        Border etch = BorderFactory.createEtchedBorder(Color.white,Color.gray);
        Border margin = new EmptyBorder(10,10,10,10);

        public static GridBagLayout grid = new GridBagLayout();
        GridBagConstraints c = new GridBagConstraints();
        final static boolean shouldFill = true;

        JPanel container;
        JPanel divider = new JPanel();
        JPanel bodypanel = new JPanel();
        final JPanel buttonpanel = new JPanel();
        JPanel panel_1 = new JPanel();
        JPanel panel_2 = new JPanel();
        JPanel panel_3 = new JPanel();
        CardLayout cl = new CardLayout();

        JTextArea text_2;
        JTextArea text_3;


        String change = "Finish";
        final JButton btnNext;
        final JButton btnBack;
        int currentCard = 0;
        int cardflag = 0;

        AbstractAction backAction;
        AbstractAction nextAction;

    public gridbaglayoutdemo(){

                Card.setVisible(true);
                Card.setSize(605,333);
                Card.setTitle("Tank Delivery");
                Card.setResizable(false);

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

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


            bodypanel.setLayout(new BorderLayout());

            divider.setLayout(new BorderLayout());
            container = new JPanel(cl);
            container.setLayout(cl);
            cl.show(container, "1");

            panel_1.setLayout(grid);

            JLabel label_1 = new JLabel("Enter 1:");
            label_1.setFont(new Font("Arial", Font.PLAIN, 18));
            c.fill = GridBagConstraints.HORIZONTAL;
                c.weightx = 0.5;
                c.weighty = 0;
                c.gridx = 0;
                c.gridy = 0;
                c.insets = new Insets(10,10,0,0);
            panel_1.add(label_1, c);

            JComboBox box_1 = new JComboBox();
            box_1.setPreferredSize(new Dimension(200,30));
            c.fill = GridBagConstraints.HORIZONTAL;
                c.weightx = 0.5;
                c.weighty = 0;
                c.gridx = 0;
                c.gridy = 1;
                c.insets = new Insets(10,10,0,0);
            panel_1.add(box_1,c);

            JLabel label = new JLabel("");
            label.setFont(new Font("Arial", Font.PLAIN, 18));
            c.fill = GridBagConstraints.HORIZONTAL;
                c.weightx = 0.5;
                c.weighty = 1;
                c.gridx = 0;
                c.gridy = 2;
                c.insets = new Insets(10,0,0,0);
            panel_1.add(label, c);


            panel_2.setLayout(grid);

            JLabel label_2 = new JLabel("Enter 2:");
            label_2.setFont(new Font("Arial", Font.PLAIN, 18));
            c.fill = GridBagConstraints.HORIZONTAL;
                c.weightx = 0.5;
                c.weighty = 0;
                c.gridx = 0;
                c.gridy = 0;
                c.insets = new Insets(10,10,0,0);
            panel_2.add(label_2,c);

            text_2 = new JTextArea();
            text_2.setPreferredSize(new Dimension(200,30));
            c.fill = GridBagConstraints.HORIZONTAL;
                c.weightx = 0.5;
                c.weighty = 0;
                c.gridx = 0;
                c.gridy = 20;
                c.insets = new Insets(10,10,0,0);
            panel_2.add(text_2,c);

            JLabel label_22 = new JLabel("");
            label_22.setFont(new Font("Arial", Font.PLAIN, 18));
            c.fill = GridBagConstraints.HORIZONTAL;
                c.weightx = 0.5;
                c.weighty = 1;
                c.gridx = 0;
                c.gridy = 30;
                c.insets = new Insets(10,0,0,0);
            panel_2.add(label_22, c);


            panel_3.setLayout(grid);

            JLabel label_3 = new JLabel("Enter 3:");
            label_3.setFont(new Font("Arial", Font.PLAIN, 18));
            c.fill = GridBagConstraints.HORIZONTAL;
                c.weightx = 0.5;
                c.weighty = 0;
                c.gridx = 0;
                c.gridy = 0;
                c.insets = new Insets(10,10,0,0);
            panel_3.add(label_3,c);

            text_3 = new JTextArea();
            text_3.setPreferredSize(new Dimension(200,30));
            c.fill = GridBagConstraints.HORIZONTAL;
                c.weightx = 0.5;
                c.weighty = 0;
                c.gridx = 0;
                c.gridy = 20;
                c.insets = new Insets(10,10,0,0);
            panel_3.add(text_3,c);

            JLabel label_33 = new JLabel("");
            label_33.setFont(new Font("Arial", Font.PLAIN, 18));
            c.fill = GridBagConstraints.HORIZONTAL;
                c.weightx = 0.5;
                c.weighty = 1;
                c.gridx = 0;
                c.gridy = 30;
                c.insets = new Insets(10,0,0,0);
            panel_3.add(label_33, c);

            buttonpanel.setLayout(new FlowLayout(SwingConstants.RIGHT));
            buttonpanel.setBorder(new EmptyBorder(0,10,0,0));

        backAction = new AbstractAction("< Back") {
            public void actionPerformed(ActionEvent e) {
                currentCard--;
                gridbaglayoutdemo.this.evaluateActions();
            }
        };
        nextAction = new AbstractAction("Next >") {
            public void actionPerformed(ActionEvent e) {
                currentCard++;
                gridbaglayoutdemo.this.evaluateActions();
            }
        };

            buttonpanel.setLayout(new FlowLayout(SwingConstants.RIGHT));
            buttonpanel.setBorder(new EmptyBorder(0,0,0,0));

                btnBack = new JButton("< Back");
                btnBack.addActionListener(new ActionListener() {
                        public void actionPerformed(ActionEvent e) {
                                cl.previous(container);
                                buttonpanel.repaint();
                                cardflag--;
                                if (cardflag==0)
                                {btnBack.setEnabled(false);}
                                if(cardflag<3)
                                {btnNext.setText("Next >");}
                        }   
                });
                btnBack.setEnabled(false);
                btnBack.setFont(new Font("Arial", Font.PLAIN, 20));
                btnBack.setBorder(new BevelBorder(BevelBorder.RAISED, Color.BLACK, null, null, null));
                btnBack.setPreferredSize(new Dimension(110, 40));
                btnBack.setBackground(new Color(224,223,227));
            buttonpanel.add(btnBack);

                btnNext = new JButton("Next >");
                btnNext.addActionListener(new ActionListener() {
                        public void actionPerformed(ActionEvent e) {
                                cl.next(container);
                                buttonpanel.repaint();

                                cardflag++;
                                if(cardflag<3)
                                {btnBack.setEnabled(true);}
                                if(cardflag==2)
                                {btnNext.setText(change);}
                                if (cardflag==3)
                                {cl.show(container, "3");
                                JOptionPane.showMessageDialog(null, "DONE");
                                Window dialog = SwingUtilities.windowForComponent( btnNext );
                                dialog.dispose(); 
                                cardflag=0;
                                btnNext.setText("Next >");
                                }

                                validateText();


                        }   
                });
                btnNext.setFont(new Font("Arial", Font.PLAIN, 20));
                btnNext.setBorder(new BevelBorder(BevelBorder.RAISED, Color.BLACK, null, null, null));
                btnNext.setPreferredSize(new Dimension(110, 40));
                btnNext.setBackground(new Color(224,223,227));
                btnNext.setVisible(true);
            buttonpanel.add(btnNext);

                final JButton btnCancel = new JButton("Cancel");
                btnCancel.addActionListener(new ActionListener() {
                        public void actionPerformed(ActionEvent e) {
                                Window dialog = SwingUtilities.windowForComponent( btnCancel );
                                dialog.dispose(); 
                        }   
                });
                btnCancel.setFont(new Font("Arial", Font.PLAIN, 20));
                btnCancel.setFocusable(false);
                btnCancel.setFocusTraversalKeysEnabled(false);
                btnCancel.setFocusPainted(false);
                btnCancel.setBorder(new BevelBorder(BevelBorder.RAISED, Color.BLACK, null, null, null));
                btnCancel.setPreferredSize(new Dimension(110, 40));
                btnCancel.setBackground(new Color(224,223,227));
            buttonpanel.add(btnCancel);

            JPanel numberpanel = new JPanel();
            numberpanel.setPreferredSize(new Dimension(221,0));
            numberpanel.setBorder(new EmptyBorder(10,0,0,10));
            numberpanel.setBorder(BorderFactory.createEtchedBorder(Color.white,Color.gray));
            numberpanel.setLayout(flow);

                JButton button_7 = new JButton("7");
                button_7.addActionListener(new ActionListener() {
                        public void actionPerformed(ActionEvent e) {
                        buttonaction(e);
                        }   
                });
                button_7.setActionCommand("7");
                button_7.setFont(new Font("Arial", Font.PLAIN, 30));
                button_7.setFocusable(false);
                button_7.setFocusTraversalKeysEnabled(false);
                button_7.setFocusPainted(false);
                button_7.setBorder(new BevelBorder(BevelBorder.RAISED, Color.BLACK, null, null, null));
                button_7.setPreferredSize(new Dimension(70, 70));
                button_7.setBackground(new Color(224,223,227));
            numberpanel.add(button_7);

            JButton button_8 = new JButton("8");
                button_8.addActionListener(new ActionListener() {
                        public void actionPerformed(ActionEvent e) {
                        buttonaction(e);
                        }   
                });
                button_8.setActionCommand("8");
                button_8.setFont(new Font("Arial", Font.PLAIN, 30));
                button_8.setFocusable(false);
                button_8.setFocusTraversalKeysEnabled(false);
                button_8.setFocusPainted(false);
                button_8.setBorder(new BevelBorder(BevelBorder.RAISED, Color.BLACK, null, null, null));
                button_8.setPreferredSize(new Dimension(70, 70));
                button_8.setBackground(new Color(224,223,227));
            numberpanel.add(button_8);

            JButton button_9 = new JButton("9");
                button_9.addActionListener(new ActionListener() {
                        public void actionPerformed(ActionEvent e) {
                        buttonaction(e);
                        }   
                });
                button_9.setActionCommand("9");
                button_9.setFont(new Font("Arial", Font.PLAIN, 30));
                button_9.setFocusable(false);
                button_9.setFocusTraversalKeysEnabled(false);
                button_9.setFocusPainted(false);
                button_9.setBorder(new BevelBorder(BevelBorder.RAISED, Color.BLACK, null, null, null));
                button_9.setPreferredSize(new Dimension(70, 70));
                button_9.setBackground(new Color(224,223,227));
            numberpanel.add(button_9);



            Card.add(bodypanel);
                bodypanel.add(divider, BorderLayout.WEST);
                        divider.add(container, BorderLayout.NORTH);     
                                container.add(panel_1, "1");
                                container.add(panel_2, "2");
                                container.add(panel_3, "3");
                                //container.add(panel_4, "4");
                                //container.add(p5.panel_5, "5");
                                //container.add(p6.panel_6, "6");
                        divider.add(buttonpanel, BorderLayout.SOUTH);
                bodypanel.add(numberpanel, BorderLayout.EAST);
    }

        private void evaluateActions() {
        ((javax.swing.Action) backAction).setEnabled(currentCard > 0);
        ((javax.swing.Action) nextAction).setEnabled(currentCard < container.getComponentCount() - 1);
        }

        private void buttonaction(ActionEvent e){
            try{
            if(cardflag==1)
                {text_2.append("" + e.getActionCommand());}
            if(cardflag==2)
                {text_3.append("" + e.getActionCommand());}

            }catch(Exception IOe){}
        }

        private void validateText(){
            if(cardflag==2)
            {String text = text_2.getText();
                if (text.isEmpty()==true)
                {JOptionPane.showMessageDialog(null, "Text 2 is empty!");
                cl.show(container, "2");
                }
            }
        }


public static void main(String[] args){
    //Use the event dispatch thread for Swing components
EventQueue.invokeLater(new Runnable()
{
    @Override
    public void run()
    {
        new gridbaglayoutdemo();         
    }
});

}
}

最佳答案

我不知道你在做什么,但我刚刚注意到 cardflag 状态的一件事。

当文本字段为空并单击下一个按钮时cardflag = 2,现在单击按钮,然后调用下面的操作监听器。

private void buttonaction(ActionEvent e) {
    try {
        if (cardflag == 1) {
            text_2.append("" + e.getActionCommand());
        }
        if (cardflag == 2) {
            text_3.append("" + e.getActionCommand());
                ^^^
               //This should be text_2
        }

    } catch (Exception IOe) {
    }
}

关于java - 如何使 jButton 操作不仅仅在一次运行中起作用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25112400/

相关文章:

java - 使用 ViewModel 仅在用户干预时调用

Java 堆空间错误,我无法在 Java 中处理大型 xlsx 文件

Java 计时器 Swing 精确到 60 fps

java - JScrollPane 未显示在 JTable 上

javascript - 全局时 JSON 变量未定义

java - Java中如何在不使用全局变量的情况下在两个类之间共享变量?

java - 由于java中的某些条件,如何调用函数来延迟执行?

java - Netty:如何减少来自服务器的连续消息之间的延迟?

java - 如何在Jtable中仅显示日期值?

Javascript访问父公共(public)变量