Hello again,
I am making another test for my GUI and this one is supposed to have a couple of buttons that react and repaint the string that is displayed. Here is the code so far:
package Tests; import java.awt.*; import java.awt.Event.*; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.*; public class NumbersTest { private static class Display extends JPanel { public String currentMessage = "Click a button and see what happens!"; public void paintComponent(Graphics g) { super.paintComponent(g); g.drawString(currentMessage, 50, 215); } private static class buttonHandler1 implements ActionListener { String currentMessage; public void actionPerformed(ActionEvent e) { currentMessage = "You clicked Button #!"; } } private static class buttonHandler2 implements ActionListener { String currentMessage; public void actionPerformed(ActionEvent e) { currentMessage = "You clicked Button #2!"; } } private static class buttonHandler3 implements ActionListener { String currentMessage; public void actionPerformed(ActionEvent e) { currentMessage = "You clicked Button #3!"; } } private static class closeButtonHandler implements ActionListener { String currentMessage; public void actionPerformed(ActionEvent e) { System.exit(0); } } public static void main(String[] arg) { String currentMessage; Display displayPanel = new Display(); JButton button1 = new JButton("Button #1"); JButton button2 = new JButton("Button #2"); JButton button3 = new JButton("Button #3"); JButton closeButton = new JButton("Close"); buttonHandler1 listenerb1 = new buttonHandler1(); buttonHandler2 listenerb2 = new buttonHandler2(); buttonHandler3 listenerb3 = new buttonHandler3(); closeButtonHandler closelistener = new closeButtonHandler(); button1.addActionListener(listenerb1); button2.addActionListener(listenerb2); button3.addActionListener(listenerb3); closeButton.addActionListener(closelistener); JPanel content = new JPanel(); content.setLayout(new BorderLayout()); content.add(displayPanel, BorderLayout.CENTER); content.add(button1, BorderLayout.NORTH); content.add(button2, BorderLayout.WEST); content.add(button3, BorderLayout.EAST); content.add(closeButton, BorderLayout.SOUTH); content.setVisible(true); JFrame window = new JFrame("Numbers and Buttons"); window.setContentPane(content); window.setSize(500, 500); window.setLocation(100, 100); window.setVisible(true); } } }
How do I make it so that when the buttons are clicked, it repaints the string displayed to whatever I want it to? I was thinking repaint(); but I'm not sure.
Help?
-Silent