我正在做计算机研究控制评估。这是一个加密/解密程序。但是,我试图在一个名为Gui_Maker的类中听一个按钮,它创建了GUI和所有swing元素。然后我想将信息传递给另一个名为Computerscience的类中的方法。但是每次按下按钮都会出错。
我在Java中根本不高级,如果有任何解释给出了我应该在程序中添加的任何代码,并且用非专业人员的术语解释,那将会有所帮助。
这是我的代码:
package computerscience;
import java.awt.EventQueue;
import javax.swing.JFrame;
import java.awt.GridBagLayout;
import javax.swing.JButton;
import java.awt.GridBagConstraints;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JLabel;
import javax.swing.JTextArea;
public class Gui_Maker implements ActionListener {
Computerscience computerscience = new Computerscience();
JButton btnEncrypt = new JButton("Encrypt");
JButton btnDecrypt = new JButton("Decrypt");
JButton btnOpen = new JButton("Open");
protected JLabel lblEnterYourMessage;
protected JLabel lblEnterYourOffset;
protected JTextArea txtrMessage;
protected JTextArea txtrOffset;
private JFrame frame;
boolean test;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
Gui_Maker window = new Gui_Maker();
window.frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the application.
*/
public Gui_Maker() {
initialize();
}
/**
* Initialize the contents of the frame.
*/
private void initialize() {
btnEncrypt.addActionListener(this);
btnDecrypt.addActionListener(this);
btnOpen.addActionListener(this);
frame = new JFrame();
frame.setBounds(100, 100, 475, 240);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
GridBagLayout gridBagLayout = new GridBagLayout();
gridBagLayout.columnWidths = new int[] { 0, 0, 0 };
gridBagLayout.rowHeights = new int[] { 0, 0, 0, 0 };
gridBagLayout.columnWeights = new double[] { 1.0, 0.0, Double.MIN_VALUE };
gridBagLayout.rowWeights = new double[] { 1.0, 1.0, 1.0, 1.0 };
frame.getContentPane().setLayout(gridBagLayout);
JLabel lblEnterYourMessage = new JLabel("Enter your message here");
GridBagConstraints gbc_lblEnterYourMessage = new GridBagConstraints();
gbc_lblEnterYourMessage.anchor = GridBagConstraints.SOUTH;
gbc_lblEnterYourMessage.insets = new Insets(0, 0, 5, 5);
gbc_lblEnterYourMessage.gridx = 0;
gbc_lblEnterYourMessage.gridy = 0;
frame.getContentPane()
.add(lblEnterYourMessage, gbc_lblEnterYourMessage);
JTextArea txtrMessage = new JTextArea();
txtrMessage.setText("Message");
GridBagConstraints gbc_txtrMessage = new GridBagConstraints();
gbc_txtrMessage.insets = new Insets(0, 0, 5, 5);
gbc_txtrMessage.fill = GridBagConstraints.BOTH;
gbc_txtrMessage.gridx = 0;
gbc_txtrMessage.gridy = 1;
frame.getContentPane().add(txtrMessage, gbc_txtrMessage);
GridBagConstraints gbc_btnEncrypt = new GridBagConstraints();
gbc_btnEncrypt.fill = GridBagConstraints.VERTICAL;
gbc_btnEncrypt.insets = new Insets(0, 0, 5, 0);
gbc_btnEncrypt.gridx = 1;
gbc_btnEncrypt.gridy = 1;
frame.getContentPane().add(btnEncrypt, gbc_btnEncrypt);
JLabel lblEnterYourOffset = new JLabel("Enter your offset here");
GridBagConstraints gbc_lblEnterYourOffset = new GridBagConstraints();
gbc_lblEnterYourOffset.anchor = GridBagConstraints.SOUTH;
gbc_lblEnterYourOffset.insets = new Insets(0, 0, 5, 5);
gbc_lblEnterYourOffset.gridx = 0;
gbc_lblEnterYourOffset.gridy = 2;
frame.getContentPane().add(lblEnterYourOffset, gbc_lblEnterYourOffset);
GridBagConstraints gbc_btnDecrypt = new GridBagConstraints();
gbc_btnDecrypt.fill = GridBagConstraints.VERTICAL;
gbc_btnDecrypt.insets = new Insets(0, 0, 5, 0);
gbc_btnDecrypt.gridx = 1;
gbc_btnDecrypt.gridy = 2;
frame.getContentPane().add(btnDecrypt, gbc_btnDecrypt);
JTextArea txtrOffset = new JTextArea();
txtrOffset.setText("Offset");
GridBagConstraints gbc_txtrOffset = new GridBagConstraints();
gbc_txtrOffset.insets = new Insets(0, 0, 0, 5);
gbc_txtrOffset.fill = GridBagConstraints.BOTH;
gbc_txtrOffset.gridx = 0;
gbc_txtrOffset.gridy = 3;
frame.getContentPane().add(txtrOffset, gbc_txtrOffset);
GridBagConstraints gbc_btnOpen = new GridBagConstraints();
gbc_btnOpen.fill = GridBagConstraints.VERTICAL;
gbc_btnOpen.gridx = 1;
gbc_btnOpen.gridy = 3;
frame.getContentPane().add(btnOpen, gbc_btnOpen);
}
public void actionPerformed(ActionEvent e) {
if (e.getSource() == btnEncrypt) {
String input_text;
int input_offset;
test = true;
input_text = txtrMessage.getText();
input_offset = Integer.parseInt(txtrOffset.getText());
computerscience.encrypt(input_text, input_offset, test);
}
if (e.getSource() == btnDecrypt) {
String input_text;
int input_offset;
test = false;
input_text = txtrMessage.getText();
input_offset = Integer.parseInt(txtrOffset.getText());
computerscience.decrypt(input_text, input_offset, test);
}
if (e.getSource() == btnOpen) {
}
}
}
答案 0 :(得分:2)
这是您班级中的字段:
protected JTextArea txtrMessage;
protected JTextArea txtrOffset;
在这里,您可以在方法
中本地重新创建对象JTextArea txtrMessage = new JTextArea();
//...
JTextArea txtrOffset = new JTextArea();
在另一种方法中,您可以访问未初始化的字段:
input_text = txtrMessage.getText();
input_offset = Integer.parseInt(txtrOffset.getText());
忽略init方法中代码的类型。
txtrMessage = new JTextArea();
//...
txtrOffset = new JTextArea();
对于其他一些JComponents来说,尽管它并没有因此而受到伤害:
lblEnterYourMessage = ...
lblEnterYourOffset = ...