943,866 Members | Top Members by Rank

Ad:
  • Java Discussion Thread
  • Unsolved
  • Views: 19448
  • Java RSS
Aug 25th, 2004
0

I can't figure out how to change my jbutton font and color ( heres the code )

Expand Post »
Java Syntax (Toggle Plain Text)
  1. /*jesse johnson
  2.   Help.java csc 110
  3.   August 25, 2004 */
  4.  
  5. import java.awt.*;
  6. import java.awt.event.*;
  7. import javax.swing.*;
  8.  
  9. public class Help extends JApplet implements ActionListener
  10. {
  11. // GUI Buttons
  12. JButton ifButton , ifElseButton , switchButton , whileButton , doWhileButton , forButton, clearArea;
  13. JTextArea output;
  14. JLabel statusBar;
  15.  
  16. public void init() {
  17.  
  18. Container container = getContentPane();
  19. container.setLayout( new FlowLayout() );
  20. Font f = new Font("Courier", Font.BOLD, 10);
  21.  
  22.  
  23. ifButton = new JButton( " If..........",f );
  24. ifButton.addActionListener( this );
  25. container.add( ifButton );
  26.  
  27.  
  28. ifElseButton = new JButton( " If..Else ",f );
  29. ifElseButton.addActionListener( this );
  30. container.add( ifElseButton );
  31.  
  32.  
  33. switchButton = new JButton( " Switch.. " );
  34. switchButton.addActionListener( this );
  35. container.add( switchButton );
  36.  
  37.  
  38. whileButton = new JButton( " .While.. " );
  39. whileButton.addActionListener( this );
  40. container.add( whileButton );
  41.  
  42.  
  43. doWhileButton = new JButton( "Do While" );
  44. doWhileButton.addActionListener( this );
  45. container.add( doWhileButton );
  46.  
  47.  
  48. forButton = new JButton( "For Loop" );
  49. forButton.addActionListener( this );
  50. container.add( forButton );
  51.  
  52. output = new JTextArea( 13, 20 );
  53. container.add( output );
  54.  
  55. clearArea = new JButton( "clearArea" );
  56. clearArea.addActionListener( this );
  57. container.add ( clearArea );
  58.  
  59. setSize ( 225, 500 );
  60.  
  61. } // end method init
  62.  
  63. public void actionPerformed(ActionEvent ae )
  64. {
  65. String text;
  66. JButton but = ( JButton )ae.getSource();
  67.  
  68. //Handle what button was pushed
  69. //Action for if button
  70. if( but.getText() == " If..........")
  71. {
  72. text = " if ( condition ) {\n\n statements;\n\n} ";
  73.  
  74. output.setText( text );
  75.  
  76. showStatus("If Statement ");
  77.  
  78. }
  79.  
  80. else if (but.getText() == " If..Else " )
  81. {
  82. text = "if ( condition ) {\n\n statements;\n\n}\nelse { \n\n statements;\n\n}";
  83.  
  84. output.setText( text );
  85.  
  86. showStatus("If...Else Statement");
  87. }
  88.  
  89. else if (but.getText() == " Switch.. " )
  90. {
  91. text = "switch ( condition ) {\n\ncase ABC:\n statement;\n /*falls through */"+
  92. "\n\ncase DEF :\n statements;\n break;\n\ncase XYZ :\n statements;\n break;"+
  93. "\n\ndefault :\n statements;\n break;\n\n}";
  94.  
  95. output.setText( text );
  96.  
  97. showStatus("Switch Statement");
  98. }
  99.  
  100. else if (but.getText() == " .While.. " )
  101. {
  102. text = "while ( condition ) {\n statements;\n\n}";
  103.  
  104. output.setText( text );
  105.  
  106. showStatus(" While Loop");
  107.  
  108. }
  109.  
  110. else if (but.getText() == "Do While" )
  111. {
  112. text = "do {\n statements;\n\n} while ( condition );";
  113.  
  114. output.setText( text );
  115.  
  116. showStatus("Do...While Loop");
  117. }
  118.  
  119. else if (but.getText() == "For Loop" )
  120. {
  121. text = " for ( initialization; condition; counter ) {\n statements;\n\n}";
  122.  
  123. output.setText( text );
  124.  
  125. showStatus("For Loop");
  126.  
  127. }
  128.  
  129. else if (but.getText() == "clearArea" )
  130. {
  131. text = " ";
  132.  
  133. output.setText( text );
  134.  
  135. showStatus("Click Another Button");
  136.  
  137. }
  138.  
  139. }
  140. }
Last edited by peter_budo; Sep 2nd, 2011 at 11:33 am. Reason: Adding code tags to old post
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
jjohnson33 is offline Offline
3 posts
since Aug 2004
Aug 26th, 2004
0

Re: I can't figure out how to change my jbutton font and color ( heres the code )

For changing the color ... button.setForegroundColor(Color.red);
For changing the font ... button.setFont("Verdana");
Team Colleague
Reputation Points: 45
Solved Threads: 56
Unauthenticated Liar
nanosani is offline Offline
1,767 posts
since Jul 2004
Aug 26th, 2004
0

Re: I can't figure out how to change my jbutton font and color ( heres the code )

I dont get it, whats the problem?
Reputation Points: 10
Solved Threads: 0
Newbie Poster
BlackDeath is offline Offline
4 posts
since Jul 2004
Aug 27th, 2004
0

Re: I can't figure out how to change my jbutton font and color ( heres the code )

Quote originally posted by jjohnson33 ...
// GUI Buttons
JButton ifButton , ifElseButton , switchButton , whileButton , doWhileButton , forButton, clearArea;
JTextArea output;
JLabel statusBar
Java Syntax (Toggle Plain Text)
  1. ifButton.setForegroundColor(Color.red);
  2. ifButton.setBackgroundColor(Color.black);
  3. ifButton.setFont("Verdana");

Other buttons are likewise. If you have problems now ... post again.
Team Colleague
Reputation Points: 45
Solved Threads: 56
Unauthenticated Liar
nanosani is offline Offline
1,767 posts
since Jul 2004
Aug 31st, 2004
0

Re: I can't figure out how to change my jbutton font and color ( heres the code )

check out the following copy from the Java API site:

setForeground
public void setForeground(Color fg)Sets the foreground color of this component.

Overrides:
setForeground in class Component
Parameters:
fg - the desired foreground Color
See Also:
Component.getForeground()
Reputation Points: 12
Solved Threads: 0
Newbie Poster
scordle725 is offline Offline
4 posts
since Aug 2004
Sep 2nd, 2011
0
Re: I can't figure out how to change my jbutton font and color ( heres the code )
Try this..:-

Java Syntax (Toggle Plain Text)
  1. String name=new String("<html><font color=RED size=10>Button1</font></html>");
  2. JButton b1=new JButton(name);

You can use HTML tags in strings....
Use these (HTML) strings to put labels on JButtons/JLabels etc..
Last edited by peter_budo; Sep 2nd, 2011 at 11:34 am. Reason: Keep It Clear - Do use code tags when you post any code
Reputation Points: 10
Solved Threads: 0
Newbie Poster
rudematrix is offline Offline
1 posts
since Sep 2011
Sep 2nd, 2011
0
Re: I can't figure out how to change my jbutton font and color ( heres the code )
I guess you didn't notice that the post was from August 2004?
I very much doubt that the OP is still waiting for an answer.
Featured Poster
Reputation Points: 1924
Solved Threads: 951
Posting Expert
JamesCherrill is offline Offline
5,788 posts
since Apr 2008

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
This thread is currently closed and is not accepting any new replies.
Previous Thread in Java Forum Timeline: Editing a PDF using JAVA API PDFBox HELP!! :(
Next Thread in Java Forum Timeline: unable to access jarfile





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC