gpt4 book ai didi

java - 为什么我的 Textarea.get 文本不起作用

转载 作者:行者123 更新时间:2023-12-01 18:42:36 25 4
gpt4 key购买 nike

我正在尝试制作一个集成Java和Sqlite的发票软件。 我似乎对 Java Swing 中的 get.Text 功能有问题。现在,我正在尝试让用户在文本字段中输入信息,出于变量名称的原因,我将其命名为 right#。我试图从文本字段中提取信息,并使用名为 Showinfo 的自定义类将其输入到数组中。所有这些都应该通过单击操作按钮来完成。对此过程的任何帮助和提示将不胜感激,但我主要担心的是 .getText 函数的失败

注意:我正在运行 JDK 1.7.0_40 。

package com.zetcode;
import java.applet.*;
import java.awt.*;
import java.awt.Component;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.Toolkit;
import javax.swing.*;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JButton;
import java.awt.event.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import javax.swing.ImageIcon;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.text.Document;
import javax.swing.text.JTextComponent;
import javax.swing.SpringLayout;
import javax.swing.JOptionPane;
import javax.swing.JDialog;
public class Test extends JFrame {

public Test()
{

setSize(600,600);
setTitle("InChoice");
setDefaultCloseOperation(EXIT_ON_CLOSE);
final Toolkit toolkit = getToolkit() ;
Dimension size = toolkit.getScreenSize();
setLocation(size.width/2 - getWidth()/2, size.height/2 - getHeight()/2);
Container contentpane= getContentPane();
SpringLayout layout = new SpringLayout();
contentpane.setLayout(layout);

//文本字段开始

Component left1= new JLabel("Party Name");
final Component right1= new JTextField(25);
Component left2= new JLabel("Date ");
final Component right2= new JTextField(25);
Component left3= new JLabel("Time start ");
final Component right3= new JTextField(25);
Component left4= new JLabel("Time start ");
final Component right4= new JTextField(25);
Component left5= new JLabel("Location ");
final Component right5= new JTextField(25);
Component left6= new JLabel("Price ");
final Component right6= new JTextField(25);
Component left7= new JLabel("Show Type ");
final Component right7= new JTextField(25);

//Component left5= new JLabel("Left");
//Component right5= new JTextField(25);

contentpane.add(left1);
contentpane.add(left2);
contentpane.add(left3);
contentpane.add(left4);
contentpane.add(left5);
contentpane.add(left6);
contentpane.add(left7);
/*contentpane.add(left5);
*/
contentpane.add(right1);
contentpane.add(right2);
contentpane.add(right3);
contentpane.add(right4);
contentpane.add(right5);
contentpane.add(right6);
contentpane.add(right7);
/*contentpane.add(right5);
*/
layout.putConstraint(SpringLayout.WEST,left1,15,SpringLayout.WEST,contentpane);
layout.putConstraint(SpringLayout.NORTH,left1,25, SpringLayout.NORTH,contentpane);
layout.putConstraint(SpringLayout.NORTH,right1,25, SpringLayout.NORTH,contentpane);
layout.putConstraint(SpringLayout.WEST, right1, 20, SpringLayout.EAST, left1);

layout.putConstraint(SpringLayout.WEST,left2,15, SpringLayout.WEST, contentpane);
layout.putConstraint(SpringLayout.NORTH,left2,50, SpringLayout.NORTH, contentpane);
layout.putConstraint(SpringLayout.NORTH,right2,50, SpringLayout.NORTH,contentpane);
layout.putConstraint(SpringLayout.WEST, right2, 20, SpringLayout.EAST, left2);
layout.putConstraint(SpringLayout.WEST, left3,15, SpringLayout.WEST, contentpane);
layout.putConstraint(SpringLayout.NORTH, left3,75, SpringLayout.NORTH,contentpane);
layout.putConstraint(SpringLayout.NORTH, right3,75,SpringLayout.NORTH,contentpane);
layout.putConstraint(SpringLayout.WEST, right3, 20, SpringLayout.EAST, left3);
layout.putConstraint(SpringLayout.WEST,left4, 15, SpringLayout.WEST, contentpane);
layout.putConstraint(SpringLayout.NORTH,left4,100,SpringLayout.NORTH,contentpane);
layout.putConstraint(SpringLayout.NORTH,right4,100,SpringLayout.NORTH,contentpane);
layout.putConstraint(SpringLayout.WEST, right4, 20, SpringLayout.EAST, left4);
layout.putConstraint(SpringLayout.WEST,left5, 15,SpringLayout.WEST,contentpane);
layout.putConstraint(SpringLayout.NORTH,left5,125,SpringLayout.NORTH, contentpane);
layout.putConstraint(SpringLayout.NORTH,right5,125,SpringLayout.NORTH,contentpane);
layout.putConstraint(SpringLayout.WEST, right5, 20, SpringLayout.EAST, left5);
layout.putConstraint(SpringLayout.WEST,left6,15,SpringLayout.WEST,contentpane);
layout.putConstraint(SpringLayout.NORTH,left6,150,SpringLayout.NORTH, contentpane);
layout.putConstraint(SpringLayout.NORTH,right6,150,SpringLayout.NORTH,contentpane);
layout.putConstraint(SpringLayout.WEST,right6,20, SpringLayout.EAST, left5);
layout.putConstraint(SpringLayout.WEST, left7, 15, SpringLayout.WEST, contentpane);
layout.putConstraint(SpringLayout.NORTH,left7,175, SpringLayout.NORTH,contentpane);
layout.putConstraint(SpringLayout.NORTH,right7,175,SpringLayout.NORTH,contentpane);
layout.putConstraint(SpringLayout.WEST,right7,20,SpringLayout.EAST,left5);

//文本字段结束

//按钮

   final JPanel panel = new JPanel();
getContentPane().add(panel);
panel.setLayout(null);
JButton action = new JButton("Action");
action.setBounds(300,250,80,30);
action.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent event) {
String A= right1.getText(0,25);
String B= right2;
String C= right3;
String D= right4;
String E= right5;
String F= right6;
String G= right7;
Showinfo Bloop = new Showinfo();
//System.out.println("blah");
}
});
JButton close = new JButton("Close");
setBounds(150,250,80,30);
close.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent event) {
System.exit(0);
}
});

contentpane.add (close);
contentpane.add (action);
layout.putConstraint(SpringLayout.WEST, action, 15, SpringLayout.WEST,contentpane);
layout.putConstraint(SpringLayout.NORTH,action,200,SpringLayout.NORTH,contentpane);

layout.putConstraint(SpringLayout.NORTH, close,200, SpringLayout.NORTH, contentpane);
layout.putConstraint(SpringLayout.WEST, close, 45, SpringLayout.EAST, left5);

JTextField Potato= new JTextField(" ",25) ;
Potato. setBounds(200,100,120,40);

panel.add(Potato);


JMenuBar menubar = new JMenuBar();
ImageIcon image = new ImageIcon("exit.png");
JMenu menu = new JMenu("File");
menu.setMnemonic(KeyEvent.VK_F);


JMenuItem menuClose = new JMenuItem("Close",image);
menuClose.setMnemonic(KeyEvent.VK_C);
menuClose.setToolTipText("Exit Inchoice");
menuClose.addActionListener(new ActionListener( ){
public void actionPerformed(ActionEvent event) {
System.exit(0);

}
});


JMenu port = new JMenu ("Import");
port.setMnemonic (KeyEvent.VK_M);

JMenuItem news= new JMenuItem("Import newsfeed list .....");
JMenuItem bookm= new JMenuItem("Import Bookmarks...");
JMenuItem mail= new JMenuItem("Import Mail ...") ;

menu.add (menuClose);

menubar.add(menu);

setJMenuBar(menubar);
menubar.add(port);

port.add(news);

port.add(bookm);

port.add(mail);

}

   public static void main(String[] args) {
Test cos = new Test();
cos.setVisible(true);
Test buttons = new Test();
buttons.setVisible(true);

}

}

//我不断收到错误消息 Cannot find Symbol Method getText()

最佳答案

您的问题是您的所有 JTextField 都被定义为 Component,因此您只能访问 Component 中定义的方法> 类。不存在 Component#getText(int, int) 这样的东西。确保所有声明类型都与您尝试分配给它的值的类型匹配。例如:

final Component right1= new JTextField(25);

final JTextField right1= new JTextField(25);

关于java - 为什么我的 Textarea.get 文本不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19309760/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com