- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在创建一个简单的应用程序来编码文本,但我正在努力处理一些 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/
在另一个 ActionListener 中嵌入一个 ActionListener 更好,还是将它们作为单独的 ActionListener 更好?我有 JComboBox1、JComboBox2 和
我为我的程序制作了一个面板。它仅由 RadioButtons 组成。选择单选按钮后,我想在其他代码中设置一个 boolean 值。此面板将用作更大面板或框架的组件,该面板或框架也应该能够监听此面板内发
我想知道(如果可能的话)如何更新 JButton 的 ActionListener 执行的操作。我需要在整个程序中保留一个按钮,但根据当前情况更改它的功能。 最佳答案 首先使用removeAction
我的程序的先前版本旨在显示多个系列的魔术图像,所有这些图像都使用相同类型的 ActionListener。例如,在显示每幅图像之间设置时间的所有技巧时,我通过使用扩展 ActionListener 的
也许我做错了。让我知道使用 Swing 和 AWT,我在框架上设置了几个按钮,每个按钮都有一个与其特定功能 I.E 相对应的 ActionListener。 JButton foo_button =
我想把第一个ActionListener(About)改成第二个ActionListener(About2)如果不将第一个复制到第二个,有什么办法可以做到这一点吗? About.addActionLi
我想在单击按钮 b 后检查选中了哪些复选框,但是我的复选框是在我单击按钮 b1 之后声明的 我的意思是,我应该全局声明 checkbox[] 吗?我该怎么做? b1.addActionListener
我有一个这样的方法,它被赋予一个 JButton 数组,并在它们被按下时返回它们的文本: public static String foo(JButton[] buttons) { for (
这个问题在这里已经有了答案: Detect enter press in JTextField (10 个答案) 关闭 7 年前。 我有一个具有三个功能的程序;读取文件、写入文件以及在文件中搜索特定
我有一个 actionlistener,当我单击一个按钮时触发,让我们称之为框,我有另一个 actionlistener 用于另一个按钮调用它重新启动。我想要做的是,当我单击一个框按钮时,除了在该 a
我正在想办法如何让一个类(class)听另一个类(class)的课。这就是我们的想法。 我有一个 MainFrame 类,它只是一个容器类,即 JFrame 容器,它采用 JPanel 类型的参数。基
实现 java.awt.event.ActionListener 接口(interface)的最佳方法是什么? 让您的类实现 ActionListener 并将其添加为 ActionListener:
我有一个框架(此处名为“MainApplication”),它主要有一个 JPanel 来根据上下文显示信息。 启动时,MainApplication 有一个空的 JPanel。 然后它创建一个“Lo
我发现已经有人提出了一些关于这个主题的问题,但我还没有找到答案。我正在编写一个代码,其中用户在 JTextField 中键入内容,然后单击按钮后,他的单词将被替换为与他的单词具有相同数量的字符数的星号
我将稍微解释一下我的代码: 我有一个带有项目列表的 JComboBox当按下 JButton“Select”时,它会注册 JComboBox 中最后一个选定项目的最后一个索引。 现在我需要访问主目录中
我收到错误,“AbstractButton 类型中的方法 addActionListener(ActionListener) 不适用于此代码的参数 (new ActionListener(){})”:
哪种方式实现 ActionListener 更正确?性能上有什么重大差异吗? 在类中实现 ActionListener: public class MainFrame implements Actio
在 Java 中,我对 JButton 数组使用 ActionListener。我希望 ActionListener 的较早部分将新的 ImageIcon 设置为 JButton,该更改将立即显示,然
这里有新海报;如果我违反了任何规则/搞砸了任何事情,请告诉我,我会删除该帖子。 我有一个 MainMenu JFrame,里面有一个 JPanel (mainMenu)。 mainMenu 有一个按钮
我在一个主类中有两个 Action Listener 内部类。每一个都对应于它自己的按钮。其中一个 Action 监听器被编码为生成数组列表。另一个只是将该数组列表写入文本字段。 我的问题是如何从其他
我是一名优秀的程序员,十分优秀!