簡體   English   中英

為什么我的按鈕不能用於我的加密程序?

[英]Why doesn't my button work for my encryption program?

在我的程序中,我使用JPG圖像作為GUI中的按鈕。 我的程序是Caesar Cipher,我遇到的問題是解密按鈕。 當我點擊它時,沒有任何反應。 但是當我更改代碼JButton decryptButton = new JButton("");JButton decryptButton = new JButton(""); 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方法仍然使用“Decrypt”而不是像我想要的那樣。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM