In my program, I'm using a JPG image as the button in the GUI. My program is the Caesar Cipher and the problem I'm having is with the decrypt button. When I click it, nothing happens. But when I change the line of code: JButton decryptButton = new JButton("");
to JButton decryptButton = new JButton("Decrypt");
, the button works. (I took Decrypt out because I don't want the text to show up next to my image) Any ideas? Thank you in advance!
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”而不是像我想要的那样。
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.