[英]JTextArea parameter and user input issues
我有一个 JTextArea,我希望允许用户输入任意数量的字符串,最多 100 个,但它可能会更少。 当我在下面的代码中设置 JTextArea 时,它被注释掉(即 //tfResult= new JTextArea(10, 0);)并且用户输入了十行字符串,然后我的代码完全按预期运行并打印出来我需要它做什么。
但是,如果我尝试输入更多或更少的行,我会得到 java.lang.ArrayIndexOutOfBoundsException 后跟用户输入的行数,无论我是否将其声明为没有边界或将其注释掉。 我是 java 图形的新手,我不知道为什么会这样,我到处寻找答案。 我的边界设置错误还是我声明了 JTextArea 错误?
我也试图包括一个 JScrollPane 但我也遇到了问题,因为它没有出现。
我非常感谢任何帮助,因为我正在努力解决这个问题。
class Window {
JFrame windowFrame;
Panel bottomPanel;
JScrollPane scroll;
JTextArea tfResult;
Button btnPlayAgain;
Font font;
Window(int width, int height, String title)
{
windowFrame = new JFrame();
windowFrame.setTitle(title);
windowFrame.setBounds(0,0,width,height);
windowFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
windowFrame.setResizable(true);
windowFrame.setCursor(new Cursor(Cursor.HAND_CURSOR)); // setting cursor to hand
windowFrame.setLayout(null);
createBottomPanel();
windowFrame.add(bottomPanel);
//windowFrame.add(field.getCanvas());
windowFrame.setVisible(true);
}
private void createBottomPanel()
{
JButton b = new JButton("Compute");
bottomPanel = new Panel();
bottomPanel.setBackground(Color.PINK);
bottomPanel.setBounds(0,400,800,140);
bottomPanel.setLayout(null);
//*********
//tfResult= new JTextArea(10, 0);
tfResult= new JTextArea();
tfResult.setBounds(10,10,600,100);
tfResult.setFont(new Font("SansSerif", Font.BOLD, 16));
tfResult.setFocusable(true);
scroll = new JScrollPane(tfResult);
scroll.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
scroll.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
btnPlayAgain = new Button("Compute");
btnPlayAgain.setBounds(620,10,150,100);
btnPlayAgain.setBackground(Color.RED);
btnPlayAgain.setFont(new Font("SansSerif", Font.BOLD, 24));
btnPlayAgain.setFocusable(true);
bottomPanel.add(tfResult);
bottomPanel.add(btnPlayAgain);
bottomPanel.add(b);
bottomPanel.add(scroll);
tfResult.setVisible(true);
scroll.setVisible(true);
btnPlayAgain.setVisible(true);
bottomPanel.setVisible(true);
btnPlayAgain.addActionListener(new ActionListener()
{
//@Override
public void actionPerformed(ActionEvent e)
{
//should include the code to genrate the output inside here
String input = tfResult.getText();
Mat xy;
xy = new Mat();
//String output = xy.getOutput(input).toString();
String output = xy.getOutput(input);
//String output = Output(input);
tfTarget.setText(output);
}
});
}
}
我继续创建了以下 GUI,允许您使用JTextArea
输入数据。
我做的第一件事是通过调用SwingUtilities
的invokeLater
方法来启动我的 Swing 应用程序。 此方法确保在Event Dispatch Thread上创建和执行 Swing 组件。
接下来,我创建了一个JFrame
。 然后我创建了一个带有BorderLayout
的JPanel
。 JTextArea
位于JScrollPane
内部,然后将其放置在JPanel
的中心内部。
JButton
放置在JPanel
的最后一行之后。
这是完整的可运行代码,也称为最小可运行示例。
import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.SwingUtilities;
public class JTextAreaInputGUI implements Runnable {
public static void main(String[] args) {
SwingUtilities.invokeLater(new JTextAreaInputGUI());
}
private JTextArea textArea;
@Override
public void run() {
JFrame frame = new JFrame("JTextArea Input GUI");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(createMainPanel(), BorderLayout.CENTER);
frame.pack();
frame.setLocationByPlatform(true);
frame.setVisible(true);
}
private JPanel createMainPanel() {
JPanel panel = new JPanel(new BorderLayout());
panel.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
textArea = new JTextArea(10, 40);
JScrollPane scrollPane = new JScrollPane(textArea);
panel.add(scrollPane, BorderLayout.CENTER);
JButton button = new JButton("Submit");
button.addActionListener(new ButtonListener());
panel.add(button, BorderLayout.AFTER_LAST_LINE);
return panel;
}
public class ButtonListener implements ActionListener {
@Override
public void actionPerformed(ActionEvent event) {
System.out.println(textArea.getText().trim());
}
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.