2017-09-28 29 views
-1

我發現了一些類似的答案,但沒有幫助我解決這個問題。出於某種原因,在我的createMiddlePanel方法中,我的JtextArea txaResults給了我一個區域說,JTextArea不能轉換爲int。任何援助都會有所幫助。我不確定這是否與它在一個面板中有關,但我想不出任何其他原因爲什麼我會得到一個錯誤。JTextArea connot被轉換爲int,jscroll在jpanel問題

package Tell; 

import java.awt.*; 
import java.awt.event.ActionEvent; 
import java.awt.event.ActionListener; 
import java.awt.image.BufferedImage; 
import java.io.File; 
import java.io.IOException; 
import javax.imageio.ImageIO; 
import javax.swing.*; 

public class FortuneTellerFrame extends JFrame { 

JPanel pnlTop, pnlMiddle, pnlBottom; 
JButton btnSubmit, btnQuit; 
JLabel lblFortuneTeller, lblPassword; 
JTextField txtFortuneTeller, txtPassword; 
JTextArea txaResults; 
JScrollPane jsp; 
public FortuneTellerFrame() { 
    add(this.createTopPanel(), BorderLayout.NORTH); 
    add(this.createMiddlePanel(), BorderLayout.CENTER); 
    add(this.createBottomPanel(), BorderLayout.SOUTH); 

    // Always set the size of data and the default close operation. 
    // Visibility needs to be set to true to be seen as well 
    this.setSize(400, 300); 
    this.setVisible(true); 
    this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 


    } 
    private JPanel createTopPanel() 
    { 
     pnlTop = new JPanel(); 
     pnlTop.setLayout(new GridLayout(2,2)); 
     ImageIcon icon = new ImageIcon("ball.jpg"); 
     Image image1 = icon.getImage(); // transform it 
     Image newimg = image1.getScaledInstance(50, 50, java.awt.Image.SCALE_SMOOTH); 
     icon = new ImageIcon(newimg); // transform back 

     JLabel label = new JLabel("Fortune Teller",icon,JLabel.LEFT); 
     label.setHorizontalTextPosition(JLabel.CENTER); 
     label.setVerticalTextPosition(JLabel.BOTTOM); 
     lblPassword = new JLabel("Password: "); 
     txtFortuneTeller = new JTextField(10); 
     txtPassword = new JTextField(10); 

     pnlTop.add(label); 
     pnlTop.add(txtFortuneTeller); 
     pnlTop.add(lblPassword); 
     pnlTop.add(txtPassword); 

     return pnlTop; 
    } 

    private JPanel createMiddlePanel() 
    { 
     pnlMiddle = new JPanel(); 
     txaResults = new JTextArea(10, 30); 
     jsp = new JScrollPane(txaResults, JScrollPane.VERTICAL_SCROLLBAR_ALWAYS); 
     pnlMiddle.add(jsp); 

     return pnlMiddle; 
    } 
    private JPanel createBottomPanel() 
    { 
     pnlBottom = new JPanel(); 
     btnSubmit = new JButton("Read My Fortune!"); 
     btnQuit = new JButton("Quit"); 

     btnQuit.addActionListener(e ->{ 
      System.exit(0); 
     }); 

     btnSubmit.addActionListener(e ->{ 
      String username = this.txtFortuneTeller.getText(); 
      String password = this.txtPassword.getText(); 

      this.txaResults.append("Attempting to login with username " 
        + username + " and password " + password + "\n"); 
     }); 

     pnlBottom.add(btnSubmit); 
     pnlBottom.add(btnQuit); 

     return pnlBottom; 
    } 

}

回答

0

這爲我工作:

private JPanel createMiddlePanel() 
{ 
    pnlMiddle = new JPanel(); 
    txaResults = new JTextArea(10, 30); 
    jsp = new JScrollPane(txaResults); 
    jsp.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS); 
    pnlMiddle.add(jsp); 

    return pnlMiddle; 
} 
0

我想你的代碼,我得到了一個錯誤這一行:

jsp = new JScrollPane(txaResults, JScrollPane.VERTICAL_SCROLLBAR_ALWAYS); 

JScrollPane的預期成分和2個整數,指向垂直滾動和水平,所以如果你添加另一個參數,它應該rk,

private JPanel createMiddlePanel() 
    { 
     pnlMiddle = new JPanel(); 
     txaResults = new JTextArea(10, 30); 
     jsp = new JScrollPane(txaResults, JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED); 
     pnlMiddle.add(jsp); 

     return pnlMiddle; 
    } 
+0

非常感謝。我不知道爲什麼它需要hoizontalScroll的價值,但這似乎確實有用。 ty –

+0

@BallaBaby由於[構造函數文檔](http://docs.oracle.com/javase/9​​/docs/api/javax/swing/JScrollPane.html#constructor.summary)清楚地顯示沒有構造函數需要組件和一個int作爲參數。 – VGR

+0

@VGR你會看看嗎 –