September 03, 2010, 10:34:54 AM *
Welcome, Guest. Please login or register.
Did you miss your activation email?

Login with username, password and session length
  Home Help Media Affiliates Login Register  
Pages: [1]   Go Down
  Print  
Author Topic: Program Menu  (Read 2399 times)
0 Members and 1 Guest are viewing this topic.
deAppel
Favorite member
Member
*

Reputation: 0
HappyFace plants one on you
Offline Offline
Posts: 45
Referrals: 0

Awards
« on: June 19, 2007, 01:08:49 PM »

Code
GeSHi (java):
  1. /*--------------------------------------------------------------------------
  2. // Author List:
  3. //      deAppel <Creator>
  4. //
  5. // Description:
  6. // A JFrame with a JMenu and an actionlistener to close the JFrame
  7. // and show x rows with String x and reverse a string.
  8. //
  9. // Environment:
  10. //      This software was developed using Eclipse and Java 1.6
  11. //
  12. // Copyright Information:
  13. //      Copyright (C) 2007      <Institution><None>
  14. // Ark de Appel www.engineeringserver.com
  15. //
  16. //----------------------------------------------------------------------*/
  17.  
  18.  
  19. import java.awt.Container;
  20. import java.awt.event.ActionEvent;
  21. import java.awt.event.ActionListener;
  22.  
  23. import javax.swing.JFrame;
  24. import javax.swing.JLabel;
  25. import javax.swing.JMenu;
  26. import javax.swing.JMenuBar;
  27. import javax.swing.JMenuItem;
  28. import javax.swing.JOptionPane;
  29. import javax.swing.JPanel;
  30. import javax.swing.JTextArea;
  31.  
  32.  
  33. public class Test extends JFrame implements ActionListener{
  34. private JMenuBar mb;
  35. private JMenu menu;
  36. private JMenuItem closeFrame, askme, reverse;
  37. private JTextArea txtArea;
  38.  
  39. public Test(){
  40. Container c = getContentPane();
  41. JPanel p = new JPanel();
  42.  
  43. mb = new JMenuBar();
  44. menu = new JMenu("Open");
  45. askme = new JMenuItem("user input");
  46. askme.addActionListener(this);
  47.  
  48. reverse = new JMenuItem("Reverse string");
  49. reverse.addActionListener(this);
  50.  
  51. txtArea = new JTextArea("Do something here..\n",10,30);
  52. txtArea.setEditable(false);
  53. //txtArea.setVisible(false);
  54.  
  55. closeFrame = new JMenuItem("Close this frame");
  56. closeFrame.addActionListener(this);
  57.  
  58.  
  59. c.add(p);
  60. p.add(txtArea);
  61.  
  62. menu.add(askme);
  63. menu.add(reverse);
  64. menu.add(closeFrame);
  65.  
  66. mb.add(menu);
  67. setJMenuBar(mb);
  68. setTitle("A Frame");
  69. setSize(300,300);
  70. pack();
  71. setVisible(true);
  72.  
  73. }
  74. public static void main(String[] args) {
  75. new Test();
  76. }
  77. public void actionPerformed(ActionEvent e) {
  78.  
  79. if (e.getSource() == askme){
  80. String input = "Number of rows";
  81. input = JOptionPane.showInputDialog(input);
  82. System.out.println(input);
  83.  
  84. String inputChar = "Character to be printed out..";
  85. inputChar = JOptionPane.showInputDialog(inputChar);
  86. System.out.println("Rows: " + input + "Chars: " + inputChar);
  87. System.out.println("Generating output..");
  88. //for(;;) .. figure something on your own..
  89.  
  90. int printRows = Integer.parseInt(input);
  91. String extra = inputChar;
  92. txtArea.setText("Generated output..\n");
  93. for (int i = 0; i < printRows; i++){
  94.  
  95. txtArea.append(inputChar + "\n");
  96. inputChar = inputChar + extra;
  97. System.out.println(inputChar);
  98. }
  99.  
  100. }
  101.  
  102. if (e.getSource() == closeFrame){
  103. this.dispose();
  104. System.out.println("Frame closed!");
  105.  
  106. }
  107.  
  108. if (e.getSource() == reverse){
  109. String inputReverse = "Reverse a string..";
  110. inputReverse = JOptionPane.showInputDialog(inputReverse);
  111. System.out.println(inputReverse);
  112. //txtArea.append(inputReverse+ " -> " + new StringBuffer(inputReverse).reverse());
  113. txtArea.setText(inputReverse+ " -> " + new StringBuffer(inputReverse).reverse());
  114.  
  115. System.out.println(inputReverse+ " reverse -> " + new StringBuffer(inputReverse).reverse());
  116.  
  117. }
  118.  
  119. }
  120. }
  121.  
Created by GeSHI 1.0.7.20

possible output:

10
Rows: 10Chars: a
Generating output..
aa
aaa
aaaa
aaaaa
aaaaaa
aaaaaaa
aaaaaaaa
aaaaaaaaa
aaaaaaaaaa
aaaaaaaaaaa

1234567890 -> 0987654321
Logged

Earn $0.04 or more for each search you make! http://www.myhpf.co.uk/apply001.asp?Friend=77889

If you need homework help, just post your assignment and i'll code it before your deadline =) i'll provide you "t3h c0d3z" but only if you show me that you have done something and not just ask for me to write everything for you. Just tell us what the problem is and i'm sure we can fix it for you!

I was born into the Hakka lineage, A bloodline that
traces it's roots back to the original Han emperors of
China. During the rise of the 3 Kingdoms (China's
famous warring period), the Hans were overthrown
and exiled. Forced to flee, they headed south finding
refuge within the mountains of Guilin Province where
they lived under their new title, the Hakka and patiently
wait for their chance to return to the throne as the
Sons Of Heaven...
Javaforums.net/forum :: A friendly community for software developers and students that want to learn Java!
« on: June 19, 2007, 01:08:49 PM »

 Logged
Pages: [1]   Go Up
  Print  
 
Jump to:  

Your Ad Here
Powered by MySQL Powered by PHP Powered by SMF 1.1.10 | SMF © 2006-2009, Simple Machines LLC
TinyPortal v0.9.8 © Bloc
Valid XHTML 1.0! Valid CSS!
Page created in 0.31 seconds with 32 queries.

Google visited last this page August 24, 2010, 03:35:59 AM