java - Java 中的 ActionPerformed 方法

标签 java swing

我创建了两个相互交互的 JFrame。第一帧询问您的全名,然后当您按“下一步”时,它会将您的名字转移到第二帧,并显示“欢迎,无论您的名字是什么。”但是,我遇到了以下问题:

  1. 当我输入 nameField 并按下一步时,它会覆盖“欢迎”消息。 “欢迎”应始终保持不变,后面跟着您的名字,显然,只要输入不同的名字,您的名字就应该改变。我该如何解决这个问题?

  2. 其次,有时在 nameWelcome.setText 中,nameWelcome 有语法错误,要求我创建此名称的新变量,但我已经创建了 new mathApo();这是该变量所在的位置。为什么我会看到此错误?

<小时/>
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class mathMulti extends JFrame {
    JButton nextButton;
    TextField nameField;
    JLabel fullName;
    JFrame frameOne;
    JPanel panelOne;

    public mathMulti() {

        frameStart();

    }

    public void frameStart() {

        frameOne = new JFrame();
        frameOne.setSize(500, 500);
        frameOne.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        panelOne = new JPanel(new GridBagLayout());
        panelOne.setBackground(Color.gray);
        frameOne.add(panelOne);

        GridBagConstraints g = new GridBagConstraints();
        fullName = new JLabel("Full Name: ");
        g.insets = new Insets(-390, -195, 0, 0);
        g.gridx = 0;
        g.gridy = 0;
        panelOne.add(fullName, g);

        nameField = new TextField(30);
        g.insets = new Insets(10, 10, 10, 10);
        g.gridx = 1;
        g.gridy = 0;
        g.weightx = 0;
        g.weighty = 0;
        g.ipady = 6;
        panelOne.add(nameField, g);

        nextButton = new JButton(" NEXT " + '\u25BA');
        g.insets = new Insets(60, 5, 5, 5);
        g.gridx = 2;
        g.gridy = 5;
        g.weightx = 0;
        g.weighty = 0;

        nextButton.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent e) {

                new mathApo();
                nameWelcome.setText(nameField.getText());
                frameOne.dispose();

            }

        });

        panelOne.add(nextButton, g);

        frameOne.setVisible(true);

    }


    public class mathApo extends JFrame {

        JFrame frameTwo;
        JPanel panelTwo;
        JLabel nameWelcome;

        public mathApo() {

            frameNext();

        }

        public void frameNext() {

            frameTwo = new JFrame();
            frameTwo.setSize(500, 500);
            frameTwo.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

            panelTwo = new JPanel(new GridBagLayout());
            panelTwo.setBackground(Color.gray);
            frameTwo.add(panelTwo);

            nameWelcome = new JLabel("Welcome, ");
            panelTwo.add(nameWelcome);

            frameTwo.setVisible(true);
        }

    }

}

最佳答案

1.) When I type in the nameField and press next, it overrides the "Welcome" message. "Welcome" should always remain the same followed by your name which obviously should change whenever a different name is entered. How can I fix this problem?

JLabel 上调用 setText 会将现有文本替换为新文本。更好的解决方案是在 mathApo 中有一个方法,该方法获取名称并将其应用到自身的标签上,因此调用者不需要知道文本“如何”格式化 - 这是一个为什么不应该将组件的控制权交给其他类的经典示例

2.) Secondly, sometimes in nameWelcome.setText, the nameWelcome has syntax error asking me to create a new variable of this name but I have already created new mathApo(); which is where that variable is located. Why am I seeing this error?

ActionListener 中的

nameWelcomeActionListenerframeStart 中没有任何上下文(或定义的变量) > 类(class)。您应该使用 mathApo 的实例来引用它(或者更好的是,使用 mathApo 中更新它的方法)

有些不同...

我建议稍微改变一下方法......

而不是依赖两个框架,您应该使用模式对话框来收集所需的信息,然后将结果传递到第二个窗口,也许类似于...

import java.awt.EventQueue;
import java.awt.GridBagLayout;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

public class Test {

    public static void main(String[] args) {
        new Test();
    }

    public Test() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                    ex.printStackTrace();
                }

                RequestInfoPane infoPane = new RequestInfoPane();
                int result = JOptionPane.showOptionDialog(null, infoPane, "Name", JOptionPane.OK_CANCEL_OPTION, JOptionPane.PLAIN_MESSAGE, null, null, 0);
                if (result == JOptionPane.OK_OPTION) {
                    JFrame frame = new JFrame("Testing");
                    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

                    WelcomePane welcomePane = new WelcomePane();
                    welcomePane.setFullName(infoPane.getFullName());
                    frame.add();
                    frame.pack();
                    frame.setLocationRelativeTo(null);
                    frame.setVisible(true);
                }
            }
        });
    }

    public class RequestInfoPane extends JPanel {

        private JLabel nameLabel;
        private JTextField nameField;

        public RequestInfoPane() {
            setLayout(new GridBagLayout());
            nameLabel = new JLabel("Full name: ");
            nameField = new JTextField(20);

            add(nameLabel);
            add(nameField);
        }

        public String getFullName() {
            return nameField.getText();
        }

    }

    public class WelcomePane extends JPanel {

        private JLabel nameLabel;

        public WelcomePane(String fullName) {
            setLayout(new GridBagLayout());
            nameLabel = new JLabel("Welcome");
            add(nameLabel);
        }

        public void setFullName(String fullName) {
            nameLabel.setText("Welcome " + fullName);
        }

    }

}

同样,您应该考虑 CardLayout 在这种情况下是否也有用 - 请参阅 How to Use CardLayout了解更多详情

关于java - Java 中的 ActionPerformed 方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51796787/

相关文章:

java - 字符串中出现的字符的一致性 (Java)

java - 2 一个 Tomcat 中的不同 InitialContextFacory 实现

java - 使用 google API key 的 URL 缩短器

java - JXTreeTable : how to use ComponentProvider to set the renderer for one column

java - 如何在 Java 中为组布局设置 Scrollable TextArea?

Java KeyListener 没有注册方向键

java - BigDecimal除法问题

java - 从我的 JButtons 获取颜色

PreferenceChanged 方法中的 Java/Swing 堆栈溢出

java - 删除按钮的矩形边框