2015-10-14 57 views
1

也許這已經被問過一次,但我有點卡住了,無法自己找到解決方案。 我從第一個按鈕使用文本字段獲得文本。現在我需要將這個文本放入第二個按鈕或文本文件中。Java Swing將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); 
    } 
} 
+0

*「..需要將此文本轉換爲第二個按鈕或文本文件。」*這些是兩個*非常不同*的要求!你應該爲這個問題確定一個或另一個,否則我認爲它對於SO來說太「寬泛」。 –

+0

現在我需要把它變成第二個按鈕。 –

回答

1

您的代碼有許多問題。

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是變量。但不是方法。

+0

謝謝。我的addbutton不好。代碼有點作品,但現在給我空白。我需要從addbutton文本到列表按鈕。 –

+0

只需按照上述2個步驟。它將打印@JustNick – Satya

+0

現在,這是行之有效的。謝謝。 –