gpt4 book ai didi

java - 如何从 keyevent 方法调用文本字段变量?

转载 作者:行者123 更新时间:2023-11-30 03:06:20 25 4
gpt4 key购买 nike

好吧,我正在做一个 GUI 程序,但我无法检索用户将输入的信息并从另一个类调用它,以便可以在那里访问它。这是我的 GUI 类的主要部分,它从创建第一个文本字段开始:

/**
*Text genereted by Simple GUI Extension for BlueJ
*/
import javax.swing.UIManager.LookAndFeelInfo;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseWheelEvent;
import java.awt.event.MouseWheelListener;
import javax.swing.border.Border;
import javax.swing.*;


public class GUI_project extends JFrame {

private JMenuBar menuBar;
private JButton button1;
private JLabel label1;
private JLabel label2;
private JLabel label3;
private JLabel label4;
private JTextField textfield1;
private JTextField textfield2;
private JTextField textfield3;
public String SoftName;
public String ReportName;
public String Devices;
public int InDevices;

//Constructor
public GUI_project(){

setTitle("Software Calculator");
setSize(500,400);
//menu generate method
generateMenu();
setJMenuBar(menuBar);

//pane with null layout
JPanel contentPane = new JPanel(null);
contentPane.setPreferredSize(new Dimension(500,400));
contentPane.setBackground(new Color(51,153,0));


button1 = new JButton();
button1.setBounds(180,325,150,35);
button1.setBackground(new Color(214,217,223));
button1.setForeground(new Color(0,0,0));
button1.setEnabled(true);
button1.setFont(new Font("sansserif",0,12));
button1.setText("Calculate and Save");
button1.setVisible(true);
//Set action for key events
//Call defined method
button1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent evt) {
startApp(evt);
}
});


label1 = new JLabel();
label1.setBounds(95,12,300,35);
label1.setBackground(new Color(214,217,223));
label1.setForeground(new Color(0,0,0));
label1.setEnabled(true);
label1.setFont(new Font("SansSerif",0,20));
label1.setText("Software Agreement Calcuator");
label1.setVisible(true);

label2 = new JLabel();
label2.setBounds(18,102,150,35);
label2.setBackground(new Color(214,217,223));
label2.setForeground(new Color(0,0,0));
label2.setEnabled(true);
label2.setFont(new Font("SansSerif",0,16));
label2.setText("Name of Software:");
label2.setVisible(true);

label3 = new JLabel();
label3.setBounds(15,174,300,35);
label3.setBackground(new Color(214,217,223));
label3.setForeground(new Color(0,0,0));
label3.setEnabled(true);
label3.setFont(new Font("SansSerif",0,16));
label3.setText("Number of Devices the Software is on:");
label3.setVisible(true);

label4 = new JLabel();
label4.setBounds(17,244,250,35);
label4.setBackground(new Color(214,217,223));
label4.setForeground(new Color(0,0,0));
label4.setEnabled(true);
label4.setFont(new Font("SansSerif",0,15));
label4.setText("Name of Report to Save Under:");
label4.setVisible(true);

textfield1 = new JTextField();
textfield1.setBounds(335,102,90,35);
textfield1.setBackground(new Color(255,255,255));
textfield1.setForeground(new Color(0,0,0));
textfield1.setEnabled(true);
textfield1.setFont(new Font("sansserif",0,12));
textfield1.setText("");
textfield1.setVisible(true);
//Set action for key events
//Call defined method
textfield1.addKeyListener(new KeyAdapter() {
public void keyTyped(KeyEvent evt) {
getSoftwareName(evt);
}
});


textfield2 = new JTextField();
textfield2.setBounds(335,173,90,35);
textfield2.setBackground(new Color(255,255,255));
textfield2.setForeground(new Color(0,0,0));
textfield2.setEnabled(true);
textfield2.setFont(new Font("sansserif",0,12));
textfield2.setText("");
textfield2.setVisible(true);
//Set action for key events
//Call defined method
textfield2.addKeyListener(new KeyAdapter() {
public void keyTyped(KeyEvent evt) {
number(evt);
}
});


textfield3 = new JTextField();
textfield3.setBounds(336,240,90,35);
textfield3.setBackground(new Color(255,255,255));
textfield3.setForeground(new Color(0,0,0));
textfield3.setEnabled(true);
textfield3.setFont(new Font("sansserif",0,12));
textfield3.setText("");
textfield3.setVisible(true);
//Set action for key events
//Call defined method
textfield3.addKeyListener(new KeyAdapter() {
public void keyTyped(KeyEvent evt) {
FileName(evt);
}
});


//adding components to contentPane panel
contentPane.add(button1);
contentPane.add(label1);
contentPane.add(label2);
contentPane.add(label3);
contentPane.add(label4);
contentPane.add(textfield1);
contentPane.add(textfield2);
contentPane.add(textfield3);

//adding panel to JFrame and seting of window position and close operation
getContentPane().add(contentPane);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLocationRelativeTo(null);
pack();
setVisible(true);
}

//Method keyPressed for button1
public void startApp (ActionEvent evt) {
Print.Start();
}

//Method actionPerformed for textfield1
public String getSoftwareName (KeyEvent evt) {
SoftName = textfield1.getText();
return SoftName;
}

//Method actionPerformed for textfield2

public int number (KeyEvent evt) {
char c=evt.getKeyChar();
if(!(Character.isDigit(c) || (c==KeyEvent.VK_BACK_SPACE)||c==KeyEvent.VK_DELETE)){
evt.consume();
}
this.Devices= this.textfield2.getText();
InDevices= Integer.parseInt(Devices);
return InDevices;
}
//Method actionPerformed for textfield3
public String FileName (KeyEvent evt) {
this.ReportName= this.textfield3.getText();
return ReportName;
}

//method for generate menu
public void generateMenu(){
menuBar = new JMenuBar();

JMenu file = new JMenu("File");

JMenuItem open = new JMenuItem("Open ");
open.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_O,Event.CTRL_MASK));

JMenuItem exit = new JMenuItem("Exit ");
exit.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_Q,Event.CTRL_MASK));


//Setings action for menu item
//Call defined method
open.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent evt) {
openFile(evt);
}
});

//Setings action for menu item
//Call defined method
exit.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent evt) {
Quit(evt);
}
});


file.add(open);
file.add(exit);

menuBar.add(file);
}
//Method for Open from menuFile
private void openFile (ActionEvent evt) {
PopUp Pop= new PopUp();
Pop.Frame();
}

//Method for Exit from menuFile
private void Quit (ActionEvent evt) {
System.exit(0);
}




public static void main(String[] args){
System.setProperty("swing.defaultlaf", "com.sun.java.swing.plaf.nimbus.NimbusLookAndFeel");
javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run() {
new GUI_project();
}
});
}

}

这是第二堂课:

public static int software;
public static String name;
public static void Company(){
GUI_project main= new GUI_project();
name=main.getSoftwareName();
if(name.equals("Microsoft")||name.equals("Norton")||name.equals("Oracle")||name.equals("CRM")){
if(name.equals("Microsoft")){
Name.software = 1200000;
}else if(name.equals("Norton")){
Name.software = 3000000;
}else if(name.equals("Oracle")){
Name.software = 1700000;
}else if(name.equals("CRM")){
Name.software = 2300000;
}
}else{
System.out.println("Not a company, GOOD BYE!");
System.exit(0);
}
}

我确信这是一个愚蠢的错误,但任何帮助都会很棒。一旦我能够让这两个类工作,其余的应该以同样的方式工作:)

最佳答案

尝试类似以下代码。

测试.java

import javax.swing.*;
import java.awt.*;
import javax.swing.event.*;

public class Test
{
JFrame myMainWindow = new JFrame("This is my title");
JPanel firstPanel = new JPanel(null);
JTextField textfield1 = new JTextField();

public void runGUI() {
myMainWindow.setBounds(10, 10, 500, 500);
myMainWindow.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
myMainWindow.setLayout(new GridLayout(1,1));
createFirstPanel();
myMainWindow.getContentPane().add(firstPanel);
myMainWindow.setVisible(true);
}

public void createFirstPanel() {
textfield1.setBounds(335,102,90,35);
textfield1.setBackground(new Color(255,255,255));
textfield1.setForeground(new Color(0,0,0));
textfield1.setEnabled(true);
textfield1.setFont(new Font("sansserif",0,12));
textfield1.setVisible(true);
textfield1.getDocument().addDocumentListener(new MyDocumentListener());
firstPanel.add(textfield1);
}

private class MyDocumentListener implements DocumentListener {
public void insertUpdate(DocumentEvent e) {
new Class2(textfield1.getText());
}

public void removeUpdate(DocumentEvent e) {
new Class2(textfield1.getText());
}

public void changedUpdate(DocumentEvent e) {}
}

public static void main(String[] args)
{
Test t = new Test();
t.runGUI();
}
}

Class2.java

public class Class2 {
int software;

public Class2(String s) {
Company(s);
System.out.println(s);
System.out.println(software);
}

public void Company(String s) {
if(s.equals("Microsoft")||s.equals("Norton")||s.equals("Oracle")||s.equals("CRM")){
if(s.equals("Microsoft")) {
software = 1200000;
System.out.println("Matches Microsoft");
} else if(s.equals("Norton")) {
software = 3000000;
System.out.println("Matches Norton");
} else if(s.equals("Oracle")) {
software = 1700000;
System.out.println("Matches Oracle");
} else if(s.equals("CRM")) {
software = 2300000;
System.out.println("Matches CRM");
}
} else {
System.out.println("Not a company, GOOD BYE!");
}
}
}

我将 KeyAdapter 更改为 Document Listener,因为该事件在更好的时间触发,即在文档更改之后。而在您的版本中,KeyAdapter 在将字母放入 textfield1 之前触发事件。

编辑

如果你想让它在最后检查。删除 Document Listener 并在 Test.java 中添加以下代码

导入 javax.swing.; 导入java.awt。; 导入 javax.swing.event.; 导入 java.awt.event.;

public class Test
{
JFrame myMainWindow = new JFrame("This is my title");
JPanel firstPanel = new JPanel(null);
JTextField textfield1 = new JTextField();
JButton button1 = new JButton("Check Company");

public void runGUI() {
myMainWindow.setBounds(10, 10, 500, 500);
myMainWindow.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
myMainWindow.setLayout(new GridLayout(1,1));
createFirstPanel();
myMainWindow.getContentPane().add(firstPanel);
myMainWindow.setVisible(true);
}

public void createFirstPanel() {
textfield1.setBounds(335,102,90,35);
textfield1.setBackground(new Color(255,255,255));
textfield1.setForeground(new Color(0,0,0));
textfield1.setEnabled(true);
textfield1.setFont(new Font("sansserif",0,12));
textfield1.setVisible(true);
firstPanel.add(textfield1);

button1.setBounds(335,150,150,35);
button1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent evt) {
new Class2(textfield1.getText());
}
});
firstPanel.add(button1);
}

public static void main(String[] args)
{
Test t = new Test();
t.runGUI();
}
}

关于java - 如何从 keyevent 方法调用文本字段变量?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34692399/

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