Java Swing 将 JTextField 值移动到第二个按钮

标签 java swing jpanel jtextfield

也许这个问题已经被问过一次,但我有点卡住了,无法自己找到解决方案。 我使用文本字段从第一个按钮获取文本。现在我需要将此文本放入第二个按钮或文本文件中。

下面的代码,我知道这个会出错。

System.out.println("Author's name: " + newauthor());
System.out.println("Book name: " + newbook());


import java.awt.GridLayout;
import java.awt.event.*;
import java.io.FileNotFoundException;
import javax.swing.*;

public class library extends JFrame 
{

private JPanel pnl;

public library() throws FileNotFoundException {

    pnl = (JPanel) getContentPane();
    JPanel panel = new JPanel();
    getContentPane().add(panel);

    panel.setLayout(null);

    JButton addbutton = new JButton("Add new book");
    addbutton.setBounds(75, 30, 150, 30);
    addbutton.addActionListener(new ActionListener() 
    {
        public void actionPerformed(ActionEvent event) 
        {
            JTextField authorfield = new JTextField(15);
            JTextField bookField = new JTextField(15);
            JPanel mypanel = new JPanel(new GridLayout(0, 1));
            mypanel.add(new JLabel("Type Author's Name and Book name:"));
            mypanel.add(new JLabel("Author's Name:"));
            mypanel.add(authorfield);
            mypanel.add(new JLabel("Book name:"));
            mypanel.add(bookField);


            int result = JOptionPane.showConfirmDialog(null, mypanel, 
            "Add a new book", JOptionPane.OK_CANCEL_OPTION);
            if (result == JOptionPane.OK_OPTION) 
                {
                String newauthor = authorfield.getText();
                String newbook = bookField.getText();
                if (!newauthor.isEmpty() && !newbook.isEmpty()) 
                        {       
                                JOptionPane.showMessageDialog(pnl, "Book "+bookField.getText()+"\nAuthor "+authorfield.getText()+"\nSuccessfully added to the list.",
                                "Book was added.", JOptionPane.INFORMATION_MESSAGE);


                        }
                else    
                        {
                                JOptionPane.showMessageDialog(pnl, "Both must be filled!",
                                "Error", JOptionPane.ERROR_MESSAGE);
                        }
                }
       }                
    });
    panel.add(addbutton);


    JButton listbutton = new JButton("List");
    listbutton.setBounds(75, 60, 150, 30);
    panel.add(listbutton);
    addbutton.addActionListener(new ActionListener() 
        {
            public void actionPerformed(ActionEvent event) 
                {

                        System.out.println("Author's name: " + newauthor());
                        System.out.println("Book name: " + newbook());
                }
        });
       JButton deletebutton = new JButton("Delete");
       deletebutton.setBounds(75, 90, 150, 30);
       panel.add(deletebutton);



       setTitle("Library Menu");
       setSize(300, 200);
       setLocationRelativeTo(null);
       setDefaultCloseOperation(EXIT_ON_CLOSE);
    }

    public static void main(String[] args) throws FileNotFoundException {
           library ex = new library();
           ex.setVisible(true);
    }
}

最佳答案

您的代码有很多问题。

1.两者都应该在类内部但在任何方法外部声明。

String newauthor = "";
String newbook = "";

现在处于if条件

         if (result == JOptionPane.OK_OPTION) 
                {
                newauthor = authorfield.getText();
                newbook = bookField.getText();
                ..............................

2.

JButton listbutton = new JButton("List");
listbutton.setBounds(75, 60, 150, 30);
panel.add(listbutton);
listbutton.addActionListener(new ActionListener()// its listbutton not addbutton
    {
        public void actionPerformed(ActionEvent event) 
            {

                    System.out.println("Author's name: " + newauthor);
                    System.out.println("Book name: " + newbook);
            }
    });

newauthornewbook 都是变量。但不是方法。

关于Java Swing 将 JTextField 值移动到第二个按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33124770/

相关文章:

java - 在 Wicket 中实现后台下载最简单的方法是什么?

java - 在 Java 中对微调器进行零填充

java - 为什么Java中不指定类或对象就可以调用setLayout()?

JAVA JPanel同时滑出和滑入

Java 堆栈跟踪正则表达式

java - 出现错误 - org.openqa.selenium.StaleElementReferenceException : stale element reference: element is not attached to the page document

Java 将 JPanel 高度设置为窗口高度的百分比

Java Swing,看不到错误

Java:打开默认邮件客户端

java - 为什么我必须导入 java.awt.* 和 java.awt.event.* (例如)