Results 1 to 4 of 4
- 08-08-2011, 04:55 AM #1
Member
- Join Date
- Aug 2011
- Posts
- 2
- Rep Power
- 0
getContentPane not displaying text
Working through 'Learning Java' and this particular code doesn't work properly. Its meant to change the color of the text each time the button is clicked. Well, It creates the frame and the button, but no text to change colors. Any help?? Thanks.
Java Code://file: HelloJava3.java import java.awt.*; import java.awt.event.*; import javax.swing.*; public class HelloJava3 extends JComponent implements MouseMotionListener, ActionListener { // Coordinates for the message int messageX = 125, messageY = 95; String theMessage; JButton theButton; // Current index into someColors int colorIndex; static Color[] someColors = {Color.black, Color.red, Color.green, Color.blue, Color.magenta}; public HelloJava3(String message) { theMessage = message; theButton = new JButton("Change Color"); setLayout(new FlowLayout()); add(theButton); theButton.addActionListener(this); addMouseMotionListener(this); } public void paintCompoment(Graphics g) { g.drawString(theMessage, messageX, messageY); } public void mouseDragged(MouseEvent e) { // Save the mouse coordinates and paint the message. messageX = e.getX(); messageY = e.getY(); repaint(); } public void mouseMoved(MouseEvent e) {} public void actionPerformed(ActionEvent e) { // Did somebody push our button? if (e.getSource() == theButton) changeColor(); } synchronized private void changeColor() { // Change the indiex to the next color. if (++colorIndex == someColors.length) colorIndex = 0; setForeground(currentColor()); //Use the new color. repaint(); // Paint again so we can see the change. } synchronized private Color currentColor() { return someColors[colorIndex]; } public static void main(String[] args) { JFrame f = new JFrame("HelloJava3"); // Make the application exit when the window is closed. f.addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent we) { System.exit(0); } }); f.setSize(300, 300); f.getContentPane().add(new HelloJava3("Hello, Java!")); f.setVisible(true); } }
- 08-08-2011, 06:00 AM #2
Senior Member
- Join Date
- Jul 2009
- Posts
- 1,236
- Rep Power
- 13
So have you narrowed down the problem?
1) When you click the button does the ActionListener get invoked
2) If the actionListener gets invoked does the color change
3) does the paintComponent() method get invoked
Add some debug statements to your code to narrow down the problem.
- 08-08-2011, 06:22 AM #3
Member
- Join Date
- Aug 2011
- Posts
- 2
- Rep Power
- 0
I'm sorry. I'm rather new to the swing utilities and whatnot. The frame is created along with the button, which is click-able. However, the text ("Hello Java!") passed as a parameter in main is not showing up in the frame. Therefore, there is no text view the color change. So essentially this is creating the frame, and the button, which does nothing.
- 08-08-2011, 06:52 AM #4
Senior Member
- Join Date
- Jul 2009
- Posts
- 1,236
- Rep Power
- 13
Similar Threads
-
Displaying text in a JPanel
By DrKilljoy in forum New To JavaReplies: 7Last Post: 04-15-2011, 08:28 PM -
Difference between add() and getContentPane().add()?
By AcousticBruce in forum New To JavaReplies: 3Last Post: 02-17-2011, 07:18 PM -
getContentPane update from actionPerformed method
By cazmo in forum New To JavaReplies: 1Last Post: 02-27-2010, 01:35 AM -
Displaying text in a tabel
By pele in forum SWT / JFaceReplies: 5Last Post: 05-28-2008, 08:05 AM -
Displaying data into text area
By abhiN in forum New To JavaReplies: 1Last Post: 01-22-2008, 11:30 AM
Bookmarks