JTextArea connot be converted to int, Jscroll in Jpanel issue
JTextArea connot be converted to int, Jscroll in Jpanel issue
我找到了一些类似的答案,但 none 帮助我解决了这个问题。出于某种原因,在我的 createMiddlePanel 方法中,我的 JtextArea txaResults 给了我一个区域,说 JTextArea 不能转换为 int。任何帮助都会有所帮助。我不确定这是否与它在面板中有关,但我想不出我会收到错误的任何其他原因。
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;
}
}
这对我有用:
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;
}
我试过你的代码,但我在这一行遇到了错误:
jsp = new JScrollPane(txaResults, JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
JScrollPane 需要组件和 2 个整数,指向垂直滚动和水平滚动,所以如果你添加其他参数,它应该可以工作,
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;
}
我找到了一些类似的答案,但 none 帮助我解决了这个问题。出于某种原因,在我的 createMiddlePanel 方法中,我的 JtextArea txaResults 给了我一个区域,说 JTextArea 不能转换为 int。任何帮助都会有所帮助。我不确定这是否与它在面板中有关,但我想不出我会收到错误的任何其他原因。
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;
}
}
这对我有用:
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;
}
我试过你的代码,但我在这一行遇到了错误:
jsp = new JScrollPane(txaResults, JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
JScrollPane 需要组件和 2 个整数,指向垂直滚动和水平滚动,所以如果你添加其他参数,它应该可以工作,
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;
}