【问题标题】:Why doesn't my button work for my encryption program?为什么我的按钮不适用于我的加密程序?
【发布时间】:2015-05-01 00:33:34
【问题描述】:

在我的程序中,我使用 JPG 图像作为 GUI 中的按钮。我的程序是凯撒密码,我遇到的问题是解密按钮。当我点击它时,什么也没有发生。但是当我更改代码行时:JButton decryptButton = new JButton("");to JButton decryptButton = new JButton("Decrypt");,按钮起作用了。 (我把 Decrypt 拿出来是因为我不想让文字出现在我的图片旁边)有什么想法吗?提前谢谢!

public CaesarGUI() {
    setTitle("Caesar Cipher");
    setVisible(true);
    setDefaultCloseOperation(3);
    pack();
    setSize(1435, 990); 

    Container content = getContentPane();

    //Changed rows to 0 so it would be filled up before recalculating layout; achieves the horizontal layout
    GridLayout layout = new GridLayout(3, 1);
    content.setLayout(layout);

    JPanel blankPanel = new JPanel();
    blankPanel.setOpaque(false);
    content.add(blankPanel);

    JPanel mainPanel = new JPanel();
    BoxLayout innerLayout = new BoxLayout(mainPanel, BoxLayout.X_AXIS);

    JPanel leftPanel = new JPanel();
    GridLayout leftLayout = new GridLayout(2, 1);

    //Puts space above buttons and shift box
    leftLayout.setVgap(10);
    leftPanel.setLayout(leftLayout);

    inputTA = new JTextArea("Put the word you want to encrypt or decrypt and press the button", 10, 20);
    inputTA.setLineWrap(true);
    inputTA.setWrapStyleWord(true);
    inputTA.setBorder(BorderFactory.createEmptyBorder(4, 4, 4, 4));
    JScrollPane scroller = new JScrollPane(inputTA);
    scroller.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
    leftPanel.add(scroller);

    JPanel box1 = new JPanel();
    box1.setLayout(new FlowLayout());
    //JButton decryptButton = new JButton("Decrypt");
    JButton decryptButton = new JButton("");

    //Sets Decrypt button to JPG
    decryptButton.setMargin(new Insets(0, 0, 0, 0));
    decryptButton.setIcon(new ImageIcon("decrypt.jpg"));
    decryptButton.setBorder(null);

    JButton encryptButton = new JButton("Encrypt");
    decryptButton.addActionListener(this);
    encryptButton.addActionListener(this);
    box1.add(decryptButton);
    box1.add(encryptButton);
    box1.add(new JLabel("               "));
    box1.add(this.shiftFactor = new JTextField(4));
    box1.setOpaque(false);
    leftPanel.add(box1);

    leftPanel.setOpaque(false);
    leftPanel.setAlignmentY(Component.TOP_ALIGNMENT);
    mainPanel.add(leftPanel);

    //Space between two text boxes
    mainPanel.add(Box.createRigidArea(new Dimension(250, 0)));

    outputTA = new JTextArea(10, 30);
    outputTA.setLineWrap(true);
    outputTA.setWrapStyleWord(true);
    outputTA.setBorder(BorderFactory.createEmptyBorder(4, 4, 4, 4));

    //Made output box uneditable so that it only displays output
    outputTA.setEditable(false);

    JPanel rightPanel = new JPanel();
    rightPanel.setLayout(new GridLayout(2, 1));

    JScrollPane scroller2 = new JScrollPane(outputTA);
    scroller2.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
    scroller2.setAlignmentY(Component.TOP_ALIGNMENT);

    rightPanel.setOpaque(false);

    rightPanel.add(scroller2);
    mainPanel.add(rightPanel);

    mainPanel.setOpaque(false);

    content.add(mainPanel);

    setVisible(true);
    setLayout(new BorderLayout());
    JLabel background = new JLabel(new ImageIcon("background.jpg"));
    add(background);
    background.setLayout(new FlowLayout());

    setResizable(false);           
}

【问题讨论】:

  • actionPerformed() 方法在哪里?
  • 可能在这个构造函数之外...
  • 您的动作监听器必须使用按钮的文本来决定要做什么。错误出现在您未发布的代码中。
  • 哦,好吧,修好了。 :) 感谢您的提示!

标签: java swing user-interface button jpeg


【解决方案1】:

发现 actionPerformed 方法仍在使用“解密”而不是像我想要的那样使用“”。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-06-10
    • 1970-01-01
    • 1970-01-01
    • 2015-05-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-04-12
    相关资源
    最近更新 更多