gpt4 book ai didi

java - 为什么我的 ActionListener 不起作用?

转载 作者:行者123 更新时间:2023-12-02 01:15:05 29 4
gpt4 key购买 nike

我正在创建一个简单的应用程序来编码文本,但我正在努力处理一些 ActionListeners。当我从 JComboBox 中选择某些内容时,一定会发生一些事情(如果您选择 Caesar's Cipher,则必须出现偏移量,如果您选择 Beaufort,则偏移量将消失;偏移量是 JTextField),并且根据选择执行按钮将得到相应的 ActionListener 实现。问题是,当我选择 Caesar's Cipher 时,什么也没有发生,即使我添加实现 ActionListener 的 CaesarsCipher 来编码文本,也不会起作用。

MyFrame.java

package cipher;

import java.awt.BorderLayout;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;

@SuppressWarnings("serial")
class MyFrame extends JFrame {

private JButton executeButton;
private ComboBoxPanel leftPanel;
private TextPanel rightPanel;

public MyFrame(String title) {

super(title);

//Initiate the frame
initFrame();
//Initiate the button that need to be pressed to execute a cipher
initButton();

//Init the panels
initPanel();

//Pack everything together;
packing();

//Make the frame visible to the user
setVisible(true);
}

//////////PACKING EVERYTHING TOGETHER//////////

private void packing() {

add(executeButton,BorderLayout.SOUTH);
add(leftPanel, BorderLayout.WEST);
add(rightPanel, BorderLayout.EAST);

}

//////////INITIATING THE FRAME WITH ALL COMPONENTS//////////

private void initPanel() {

leftPanel = new ComboBoxPanel();
rightPanel = new TextPanel();

}

private void initButton() {

executeButton = new JButton("Execute cipher");

}


private void initFrame() {

setSize(800,400);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLocationRelativeTo(null);
setLayout(new BorderLayout());
setResizable(false);

}

//////////HIDE THE OFFSET WHEN CHOOSING ALGORITHMS THAT DO NOT REQUIRE OFFSET//////////

public void hideOffset() {
leftPanel.hideOffset();
}

public void showOffset() {
leftPanel.showOffset();
}


//////////UPDATE LEFT PANEL AFTER HIDING OFFSET//////////
public void updateLeftPanel() {
leftPanel.updatePanel();
}

//////////LISTENERS//////////

/////COMBO BOX/////

public void addComboBoxListener(ActionListener comboBoxListener) {
leftPanel.addComboBoxListener(comboBoxListener);
}

/////EXECUTE BUTTON/////

public void addExecuteButtonListener(ActionListener executeButtonListener) {
executeButton.addActionListener(executeButtonListener);
}

//////////GETTERS//////////

/////OFFSET GETTER/////
public Integer getOffset() {
return Integer.parseInt(leftPanel.getOffset());
}

/////INPUT GETTER/////

public String getInputText() {
return rightPanel.getInputText();
}


//////////SETTERS//////////

/////OUTPUT TEXT SETTER/////
public void setOutputText(String text) {
rightPanel.setOutputText(text);
}

}

ComboBoxPanel.java

package cipher;

import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.event.ActionListener;

import javax.swing.BorderFactory;
import javax.swing.JComboBox;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.border.Border;

@SuppressWarnings("serial")
class ComboBoxPanel extends JPanel {

private final String []CIPHERS = {"Caesar's Cipher", "ROT13", "Beaufort Cipher", "Autokey Cipher"};
@SuppressWarnings("rawtypes")
private JComboBox comboCiphers;

private JTextField offset;

public ComboBoxPanel() {

initSize();
initComboBox();
initBorder();
initLayout();
initTextField();

packing();

}


private void initTextField() {

offset = new JTextField(10);

}

private void initLayout() {

setLayout(new GridBagLayout());

}

private void packing() {
GridBagConstraints gbc = new GridBagConstraints();
gbc.gridx = 0;
gbc.gridy = 0;
gbc.ipadx = 10;
add(comboCiphers,gbc);
gbc.gridy = 1;
add(offset,gbc);

}

private void initBorder() {

Border outer = BorderFactory.createEmptyBorder(5, 5, 5, 5);
Border inner = BorderFactory.createTitledBorder("Choose the cipher");
setBorder(BorderFactory.createCompoundBorder(outer, inner));

}

@SuppressWarnings({ "unchecked", "rawtypes" })
private void initComboBox() {

comboCiphers = new JComboBox(CIPHERS);

}

//Remainder: delete this, let the layout to do its job.
private void initSize() {
Dimension size = getPreferredSize();
size.width = 390;
setPreferredSize(size);

}

public void hideOffset() {
offset.setVisible(false);
}

public void showOffset() {
offset.setVisible(true);
}

public void updatePanel() {
revalidate();
repaint();
}

public void addComboBoxListener(ActionListener comboBoxListener) {
comboCiphers.addActionListener(comboBoxListener);
}

public String getOffset() {
return offset.toString();
}

}

TextPanel.java

package cipher;

import java.awt.Color;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.event.ActionListener;

import javax.swing.BorderFactory;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.border.Border;
import javax.swing.border.LineBorder;

@SuppressWarnings("serial")
class TextPanel extends JPanel {

private JTextArea inputArea, outputArea;
private JScrollPane inputScroll, outputScroll;

public TextPanel() {

//////////PANEL METHODS///////

/*
//Initiate the size of the panel
initSize();
*/

//Initiate the border of the entire panel
initBorder();
//Setup the layout of the panel
initLayout();

//////////TEXT AREAS METHODS///////
//Initiate the text areas
initTextArea();
//Initiate the borders for the 2 text areas
initTextAreaBorder();
//Create scrolls for both text areas
initScrollPane();

//Pack everything together
packing();

}

private void packing() {

GridBagConstraints gbc = new GridBagConstraints();
gbc.gridx = 0;
gbc.gridy = 0;
gbc.weightx = 1;
gbc.weighty = 1;
gbc.fill = GridBagConstraints.BOTH;
add(inputScroll,gbc);
gbc.gridx = 0;
gbc.gridy = 1;
gbc.weightx = 1;
gbc.weighty = 1;
add(outputScroll,gbc);

}

private void initScrollPane() {

inputScroll = new JScrollPane(inputArea);
outputScroll = new JScrollPane(outputArea);

}

private void initTextAreaBorder() {

inputArea.setBorder(new LineBorder(Color.black));
outputArea.setBorder(new LineBorder(Color.black));

}

private void initBorder() {

Border outer = BorderFactory.createEmptyBorder(5, 5, 5, 5);
Border inner = BorderFactory.createTitledBorder("Text");
setBorder(BorderFactory.createCompoundBorder(outer,inner));

}

private void initLayout() {

setLayout(new GridBagLayout());

}

private void initTextArea() {
inputArea = new JTextArea(10,77);

outputArea = new JTextArea(10,77);
outputArea.append("Output...");
outputArea.setEditable(false);

}

public void addComboBoxListener(ActionListener comboBoxListener) {
//.addActionListener(comboBoxListener);
}

/*private void initSize() {

//setPreferredSize sets the minimum
Dimension size = getPreferredSize();
size.width = 390;
setPreferredSize(size);

}*/

public String getInputText() {
return inputArea.toString();
}

public void setOutputText(String text) {
outputArea.append(text);
}

}

ApplicationController.java

package cipher;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JComboBox;

public class ApplicationController{

private MyFrame frame;

public ApplicationController(MyFrame frame) {

this.frame = frame;
this.frame.addComboBoxListener(new ComboBoxListener());

}

class ComboBoxListener implements ActionListener {

@SuppressWarnings("rawtypes")
@Override
public void actionPerformed(ActionEvent e) {

JComboBox c = (JComboBox) e.getSource();
String selected = c.getSelectedItem().toString();

if(selected.equalsIgnoreCase("Caesar's Cipher")) {
frame.addExecuteButtonListener(new CaesarsCipher());
frame.showOffset();
frame.updateLeftPanel();
} else if(selected.equalsIgnoreCase("ROT13")) {
frame.showOffset();
frame.updateLeftPanel();
} else if(selected.equalsIgnoreCase("Beaufort Cipher")) {
frame.hideOffset();
frame.updateLeftPanel();
} else if(selected.equalsIgnoreCase("Autokey Cipher")) {
frame.hideOffset();
frame.updateLeftPanel();
}

}

}

class CaesarsCipher implements ActionListener {

public void actionPerformed(ActionEvent e) {

int offset = frame.getOffset();
char []text = frame.getInputText().toCharArray();

for(int i = 0 ; i < text.length ; i++) {
text[i] = (char)(((int)text[i] + offset) % 26);
}

String newText = new String(text);
frame.setOutputText(newText);

}

}

}

请有人解释一下出了什么问题。我准备好从错误中吸取教训。谢谢。

最佳答案

您没有创建 applicationController 的实例,因此您的组合框监听器从未创建。

将来,您不应该扩展 JFrame。您所需要的只是它的一个实例。通过放入这些额外的添加监听器方法,您还增加了复杂性。

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

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