Welcome, Guest. Please login or register.
Did you miss your activation email?
Pages: [1]   Go Down
  Print  
Author Topic: A JFrame with a JMenu and an actionlistener to close the JFrame  (Read 3010 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: October 28, 2007, 03:36:38 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. import java.util.Calendar;
  23.  
  24. import javax.swing.JFrame;
  25. import javax.swing.JLabel;
  26. import javax.swing.JMenu;
  27. import javax.swing.JMenuBar;
  28. import javax.swing.JMenuItem;
  29. import javax.swing.JOptionPane;
  30. import javax.swing.JPanel;
  31. import javax.swing.JTextArea;
  32.  
  33.  
  34. public class Test extends JFrame implements ActionListener{
  35. private JMenuBar mb;
  36. private JMenu menu;
  37. private JMenuItem closeFrame, askme, reverse;
  38. private JTextArea txtArea;
  39.  
  40. public Test(){
  41. Container c = getContentPane();
  42. JPanel p = new JPanel();
  43.  
  44. mb = new JMenuBar();
  45. menu = new JMenu("Open");
  46. askme = new JMenuItem("user input");
  47. askme.addActionListener(this);
  48.  
  49. reverse = new JMenuItem("Reverse string");
  50. reverse.addActionListener(this);
  51.  
  52. txtArea = new JTextArea("Do something here..\n",10,30);
  53. txtArea.setEditable(false);
  54. //txtArea.setVisible(false);
  55.  
  56. closeFrame = new JMenuItem("Close this frame");
  57. closeFrame.addActionListener(this);
  58.  
  59.  
  60. c.add(p);
  61. p.add(txtArea);
  62.  
  63. menu.add(askme);
  64. menu.add(reverse);
  65. menu.add(closeFrame);
  66.  
  67. mb.add(menu);
  68. setJMenuBar(mb);
  69. setTitle("A Frame");
  70. setSize(300,300);
  71. pack();
  72. setVisible(true);
  73.  
  74. }
  75. public static void main(String[] args) {
  76. new Test();
  77. Calendar calADD = java.util.GregorianCalendar.getInstance (  ) ;
  78. System.out.println (  calADD.getTime (  )   ) ;
  79. }
  80. public void actionPerformed(ActionEvent e) {
  81.  
  82. if (e.getSource() == askme){
  83. String input = "Number of rows";
  84. input = JOptionPane.showInputDialog(input);
  85. System.out.println(input);
  86.  
  87. String inputChar = "Character to be printed out..";
  88. inputChar = JOptionPane.showInputDialog(inputChar);
  89. System.out.println("Rows: " + input + "Chars: " + inputChar);
  90. System.out.println("Generating output..");
  91. //for(;;) .. figure something on your own..
  92.  
  93. int printRows = Integer.parseInt(input);
  94. String extra = inputChar;
  95. txtArea.setText("Generated output..\n");
  96. for (int i = 0; i < printRows; i++){
  97.  
  98. txtArea.append(inputChar + "\n");
  99. inputChar = inputChar + extra;
  100. System.out.println(inputChar);
  101. }
  102.  
  103. }
  104.  
  105. if (e.getSource() == closeFrame){
  106. this.dispose();
  107. System.out.println("Frame closed!");
  108.  
  109. }
  110.  
  111. if (e.getSource() == reverse){
  112. String inputReverse = "Reverse a string..";
  113. inputReverse = JOptionPane.showInputDialog(inputReverse);
  114. System.out.println(inputReverse);
  115. //txtArea.append(inputReverse+ " -> " + new StringBuffer(inputReverse).reverse());
  116. txtArea.setText(inputReverse+ " -> " + new StringBuffer(inputReverse).reverse());
  117.  
  118. System.out.println(inputReverse+ " reverse -> " + new StringBuffer(inputReverse).reverse());
  119.  
  120. }
  121.  
  122. }
  123. }
  124.  
Created by GeSHI 1.0.7.20

I made that for someone on the official sun forum.
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...
laurens
Member
*

Reputation: 8
Offline Offline
Posts: 121
Referrals: 0

Awards
« Reply #1 on: November 15, 2007, 05:17:59 PM »

thanks i could use this Happy
Logged
Pages: [1]   Go Up
  Print  
 
Jump to:  

Your Ad Here