为什么我的按钮不能用于我的加密程序?

时间:2015-02-28 08:15:49

标签: java swing user-interface button jpeg

在我的程序中,我使用JPG图像作为GUI中的按钮。我的程序是Caesar Cipher,我遇到的问题是解密按钮。当我点击它时,没有任何反应。但是当我将代码行更改为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);           
}

1 个答案:

答案 0 :(得分:0)

想出actionPerformed方法仍在使用“Decrypt”而不是“”,就像我想要的那样。