Simple windowed application: Difference between revisions
(+D) |
(Added Java (#100 yay!), alphabetized, added syntax highlighting, libheader for theseus) |
||
Line 4: | Line 4: | ||
{{works with|D|1}} |
{{works with|D|1}} |
||
{{libheader|DFL}} |
{{libheader|DFL}} |
||
< |
<d>module winapp ; |
||
import dfl.all ; |
import dfl.all ; |
||
import std.string ; |
import std.string ; |
||
Line 34: | Line 34: | ||
void main() { |
void main() { |
||
Application.run(new MainForm); |
Application.run(new MainForm); |
||
}</ |
}</d> |
||
=={{header|Delphi}}== |
=={{header|Delphi}}== |
||
{{works with|Delphi|5.0}} |
{{works with|Delphi|5.0}} |
||
Line 43: | Line 43: | ||
'''NOTE:''' The project name here must match the name of the file. |
'''NOTE:''' The project name here must match the name of the file. |
||
<delphi> |
|||
<pre> |
|||
-- begin file -- |
-- begin file -- |
||
Line 132: | Line 131: | ||
end. // Program |
end. // Program |
||
</ |
</delphi> |
||
=={{header|E}}== |
=={{header|E}}== |
||
{{libheader|Swing}} |
{{libheader|Swing}} |
||
{{works with|E-on-Java}} |
{{works with|E-on-Java}} |
||
when (currentVat.morphInto("awt")) -> { |
when (currentVat.morphInto("awt")) -> { |
||
Line 158: | Line 156: | ||
{{works with|bigFORTH}} |
{{works with|bigFORTH}} |
||
{{libheader|MINOS}} |
{{libheader|MINOS}} |
||
also minos |
also minos |
||
text-label ptr click-label |
text-label ptr click-label |
||
Line 172: | Line 169: | ||
===The same with Theseus=== |
===The same with Theseus=== |
||
{{libheader|Theseus}} |
|||
#! xbigforth |
#! xbigforth |
||
\ automatic generated code |
\ automatic generated code |
||
Line 219: | Line 217: | ||
frame.pack() |
frame.pack() |
||
frame.show() |
frame.show() |
||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
end |
|||
=={{header|J}}== |
=={{header|J}}== |
||
Line 250: | Line 262: | ||
simpleApp_run<nowiki>''</nowiki> |
simpleApp_run<nowiki>''</nowiki> |
||
=={{header|Java}}== |
|||
{{libheader|AWT}} |
|||
{{libheader|Swing}} |
|||
<java>import java.awt.BorderLayout; |
|||
import java.awt.event.ActionEvent; |
|||
import java.awt.event.ActionListener; |
|||
import javax.swing.JButton; |
|||
import javax.swing.JFrame; |
|||
import javax.swing.JLabel; |
|||
public class Clicks extends JFrame implements ActionListener{ |
|||
private int clicks = 0; |
|||
private JLabel label; |
|||
private JButton clicker; |
|||
private String text; |
|||
public Clicks(){ |
|||
⚫ | |||
text = "There have been " + ((clicks == 0) ? "no clicks yet" : (clicks + " clicks")); |
|||
⚫ | |||
label = new JLabel(text); |
|||
⚫ | |||
clicker = new JButton("click me"); |
|||
⚫ | |||
clicker.addActionListener(this);//listen to the button |
|||
⚫ | |||
setLayout(new BorderLayout());//handles placement of components |
|||
⚫ | |||
add(label,BorderLayout.CENTER);//add the label to the biggest section |
|||
⚫ | |||
add(clicker,BorderLayout.SOUTH);//put the button underneath it |
|||
⚫ | |||
setSize(300,200);//stretch out the window |
|||
⚫ | |||
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//stop the program on "X" |
|||
⚫ | |||
setVisible(true);//show it |
|||
⚫ | |||
} |
|||
⚫ | |||
public static void main(String[] args){ |
|||
⚫ | |||
new Clicks();//call the constructor where all the magic happens |
|||
⚫ | |||
} |
|||
public void actionPerformed(ActionEvent arg0) { |
|||
if(arg0.getSource() == clicker){//if they clicked the button |
|||
clicks++; |
|||
text = "There have been " + clicks + " clicks"; |
|||
label.setText(text);//change the text |
|||
} |
|||
} |
|||
}</java> |
|||
=={{header|MAXScript}}== |
=={{header|MAXScript}}== |
||
Line 284: | Line 319: | ||
=={{header|Perl}}== |
=={{header|Perl}}== |
||
{{libheader|Tk}} |
{{libheader|Tk}} |
||
<perl> use Tk; |
|||
$main = MainWindow->new; |
$main = MainWindow->new; |
||
Line 293: | Line 328: | ||
-command => sub { $l->configure(-text => 'Number of clicks: '.(++$count).'.'); }, |
-command => sub { $l->configure(-text => 'Number of clicks: '.(++$count).'.'); }, |
||
)->pack; |
)->pack; |
||
MainLoop(); |
MainLoop();</perl> |
||
{{libheader|GTK}} |
{{libheader|GTK}} |
||
<perl> use Gtk '-init'; |
|||
# Window. |
# Window. |
||
Line 322: | Line 357: | ||
# Main loop. |
# Main loop. |
||
Gtk->main; |
Gtk->main;</perl> |
||
=={{header|Python}}== |
=={{header|Python}}== |
||
{{libheader|Tkinter}} |
{{libheader|Tkinter}} |
||
⚫ | |||
<pre> |
|||
⚫ | |||
def update_label(): |
def update_label(): |
||
Line 340: | Line 374: | ||
l.pack() |
l.pack() |
||
Button(w, text="click me", command=update_label).pack() |
Button(w, text="click me", command=update_label).pack() |
||
w.mainloop() |
w.mainloop()</python> |
||
</pre> |
|||
===The same in OO manner=== |
===The same in OO manner=== |
||
⚫ | |||
<pre> |
|||
⚫ | |||
from Tkinter import Button, Frame, Label, Pack |
from Tkinter import Button, Frame, Label, Pack |
||
Line 365: | Line 397: | ||
if __name__=="__main__": |
if __name__=="__main__": |
||
ClickCounter().mainloop() |
ClickCounter().mainloop()</python> |
||
</pre> |
|||
=={{header|Tcl}}== |
=={{header|Tcl}}== |
Revision as of 14:30, 29 February 2008
You are encouraged to solve this task according to the task description, using any language you may know.
This task asks to create a window with a label that says "There have been no clicks yet" and a button that says "click me". Upon clicking the button with the mouse, the label should change and show the number of times the button has been clicked.
D
<d>module winapp ; import dfl.all ; import std.string ;
class MainForm: Form {
Label label ; Button button ; this() { width = 240 ; with(label = new Label) { text = "There have been no clicks yet" ; dock = DockStyle.TOP ; parent = this ; } with(button = new Button) { dock = DockStyle.BOTTOM ; text = "Click Me" ; parent = this ; click ~= &onClickButton ; } height = label.height + button.height + 36 ; } private void onClickButton(Object sender, EventArgs ea) { static int count = 0 ; label.text = "You had been clicked me " ~ std.string.toString(++count) ~ " times." ; }
}
void main() {
Application.run(new MainForm);
}</d>
Delphi
Creating a "basic windowed application" in Delphi is quite simple. Start up the IDE and the default application is a single windowed app. This will have two files, the main project file "project1" and a unit file, "unit1". What I a going to do here is going to be somewhat different. I will do it all dynamically from the main "program file". Create this in any text editor and you can then either open it in the IDE to build it, or invoke the command line compiler. There is quite a bit going on behind the scenes with the VCL and the RTL.
Filename = SingleWinApp.dpr
NOTE: The project name here must match the name of the file.
<delphi> -- begin file --
Program SingleWinApp ; // This is the equivalent of the C #include Uses Forms, Windows, Messages, Classes, Graphics, Controls, StdCtrls ;
type // The only reason for this declaration is to allow the connection of the // on click method to the forms button object. This class declaration adds // a procedure.
TMainForm class(tform) Procedure AddClicks(sender : tObject); end;
// Use these globals. var MainForm : tForm ; aLabel : tLabel ; aButton : tButton ; i : integer = 0 ;
// This is the Method call that we connect to the button object // to start counting the clicks. Procedure tMainForm.AddClicks(sender :tObject) begin inc(i); aLabel.Caption := IntToStr(i) + ' Clicks since startup' ; end;
Begin // Do all the behind the scenes stuff that sets up the Windows environment Application.Initialize ;
// Create the form // Forms can either be created with an owner, like I have done here, or with // the owner set to Nil. In pascal (all versions of Borland) NIL is a // reserved, (the equivalent of NULL in Ansi C) word and un-sets any pointer // variable. Setting the owner to the application object will ensure that the form is // freed by the application object when the application shuts down. If I had set // the owner to NIL then i would have had to make sure I freed the form explicitly // or it would have been orphaned, thus creating a memory leak.
// I must direct your attention to the CreateNew constructor. This is // a non standard usage. Normally the constructor Create() will call this // as part of the initialization routine for the form. Normally as you drop // various components on a form in deign mode, a DFM file is created with // all the various initial states of the controls. This bypasses the // DFM file altogether although all components AND the form are created // with default values. (see the Delphi help file).
MainForm := tMainForm.CreateNew(Application); MainForm.Parent := Application ; MainForm.Position := poScreenCenter ; MainForm.Caption := 'Single Window Application' ;
// Create the Label, set its owner as MaiaForm aLabel := tLabel.Create(mainForm); aLabel.Parent := MainForm; aLabel.Caption := IntToStr(i) + ' Clicks since startup' ; aLabel.Left := 20 ; aLabel.Top := MainForm.ClientRect.Bottom div 2 ;
// Create the button, set its owner to MainForm aButton := tButton.Create(MainForm); aButton.Parent := MainForm ; aButton.Caption := 'Click Me!'; aButton.Left := (MainForm.ClientRect.Right div 2)-(aButton.Width div 2 ); aButton.Top := MainForm.ClientRect.Bottom - aButton.Height - 10 ; aButton.OnClick := AddClicks ;
// Show the main form, Modaly. The ONLY reason to do this is because in this // demonstration if you only call the SHOW method, the form will appear and // disappear in a split second. MainForm.ShowModal ; Application.Run ;
end. // Program
</delphi>
E
when (currentVat.morphInto("awt")) -> { var clicks := 0 def w := <swing:makeJFrame>("Rosetta Code 'Simple Windowed Application'") w.setContentPane(JPanel` ${def l := <swing:makeJLabel>("There have been no clicks yet.")} $\ ${def b := <swing:makeJButton>("Click Me")} `) b.addActionListener(def _ { to actionPerformed(_) { clicks += 1 l.setText(`Number of clicks: $clicks`) } }) w.pack() w.show() }
Forth
also minos text-label ptr click-label Variable click# click# off : click-win ( -- ) screen self window new window with X" There have been no clicks yet" text-label new dup F bind click-label ^ S[ 1 click# +! click# @ 0 <# #S s" Number of clicks: " holds #> click-label assign ]S X" Click me" button new &2 vabox new panel s" Clicks" assign show endwith ; click-win
The same with Theseus
#! xbigforth \ automatic generated code \ do not edit also editor also minos also forth component class ccount public: early widget early open early dialog early open-app text-label ptr click# ( [varstart] ) cell var clicks ( [varend] ) how: : open new DF[ 0 ]DF s" Click counter" open-component ; : dialog new DF[ 0 ]DF s" Click counter" open-dialog ; : open-app new DF[ 0 ]DF s" Click counter" open-application ; class; ccount implements ( [methodstart] ) ( [methodend] ) : widget ( [dumpstart] ) X" There have been no clicks yet" text-label new ^^bind click# ^^ S[ 1 clicks +! clicks @ 0 <# #S s" Number of clicks: " holds #> click# assign ]S ( MINOS ) X" Click me" button new &2 vabox new panel ( [dumpend] ) ; : init ^>^^ assign widget 1 :: init ; class; : main ccount open-app $1 0 ?DO stop LOOP bye ; script? [IF] main [THEN] previous previous previous
Groovy
import groovy.swing.SwingBuilder def countLabel, count = 0, swing = new SwingBuilder() def frame = swing.frame(title:'Click frame') { flowLayout() countLabel = label(text:"There have been no clicks yet." ) button(text:'Click Me', actionPerformed: {count++; countLabel.text = "Clicked ${count} time(s)."}) } frame.pack() frame.show()
IDL
pro counter, ev widget_control, ev.top, get_uvalue=tst tst[1] = tst[1]+1 widget_control, tst[0], set_value="Number of clicks: "+string(tst[1],format='(i0)') widget_control, ev.top, set_uvalue=tst end id = widget_base(title = 'Window Title',column=1) ld = widget_label(id, value = 'There have been no clicks yet.') widget_control, /realize, id, set_uvalue=[ld,0] dummy = widget_button(id,value=' Click Me ',event_pro='counter') xmanager, "Simple", Id end
J
SIMPLEAPP=: 0 : 0 pc simpleApp; xywh 136 8 44 12;cc inc button;cn "Click me"; xywh 136 23 44 12;cc cancel button;cn "Cancel"; xywh 7 10 115 11;cc shownText static;cn "There have been no clicks yet."; pas 6 6;pcenter; rem form end; ) simpleApp_run=: 3 : 0 wd SIMPLEAPP simpleApp_accum=: 0 NB. initialize accumulator wd 'pshow;' ) simpleApp_close=: 3 : 0 wd'pclose' ) simpleApp_cancel_button=: 3 : 0 simpleApp_close'' ) simpleApp_inc_button=: 3 : 0 wd 'set shownText *','Button-use count: ',": simpleApp_accum=: >: simpleApp_accum ) simpleApp_run''
Java
<java>import java.awt.BorderLayout; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JLabel; public class Clicks extends JFrame implements ActionListener{ private int clicks = 0; private JLabel label; private JButton clicker; private String text;
public Clicks(){ text = "There have been " + ((clicks == 0) ? "no clicks yet" : (clicks + " clicks")); label = new JLabel(text); clicker = new JButton("click me"); clicker.addActionListener(this);//listen to the button setLayout(new BorderLayout());//handles placement of components add(label,BorderLayout.CENTER);//add the label to the biggest section add(clicker,BorderLayout.SOUTH);//put the button underneath it setSize(300,200);//stretch out the window setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//stop the program on "X" setVisible(true);//show it } public static void main(String[] args){ new Clicks();//call the constructor where all the magic happens } public void actionPerformed(ActionEvent arg0) { if(arg0.getSource() == clicker){//if they clicked the button clicks++; text = "There have been " + clicks + " clicks"; label.setText(text);//change the text }
} }</java>
MAXScript
rollout buttonClick "Button Click" ( label l "There have been no clicks yet" button clickMe "Click me" local clickCount = 0 on clickMe pressed do ( clickCount += 1 l.text = ("Number of clicks: " + clickCount as string) ) ) createDialog buttonClick
Perl
<perl> use Tk;
$main = MainWindow->new; $l = $main->Label('-text' => 'There have been no clicks yet.')->pack; $count = 0; $main->Button( -text => ' Click Me ', -command => sub { $l->configure(-text => 'Number of clicks: '.(++$count).'.'); }, )->pack; MainLoop();</perl>
<perl> use Gtk '-init';
# Window. $window = Gtk::Window->new; $window->signal_connect('destroy' => sub { Gtk->main_quit; }); # VBox. $vbox = Gtk::VBox->new(0, 0); $window->add($vbox); # Label. $label = Gtk::Label->new('There have been no clicks yet.'); $vbox->add($label); # Button. $count = 0; $button = Gtk::Button->new(' Click Me '); $vbox->add($button); $button->signal_connect('clicked', sub { $label->set_text(++$count); }); # Show. $window->show_all; # Main loop. Gtk->main;</perl>
Python
<python>from Tkinter import Tk, Label, Button
def update_label():
global n n += 1 l["text"] = "Number of clicks: %d" % n
w = Tk() n = 0 l = Label(w, text="There have been no clicks yet") l.pack() Button(w, text="click me", command=update_label).pack() w.mainloop()</python>
The same in OO manner
<python>#!/usr/bin/env python from Tkinter import Button, Frame, Label, Pack
class ClickCounter(Frame):
def click(self): self.count += 1 self.label['text'] = 'Number of clicks: %d' % self.count def createWidgets(self): self.label = Label(self, text='here have been no clicks yet') self.label.pack() self.button = Button(self, text='click me', command=self.click) self.button.pack() def __init__(self, master=None): Frame.__init__(self, master) Pack.config(self) self.createWidgets() self.count = 0
if __name__=="__main__":
ClickCounter().mainloop()</python>
Tcl
pack [label .l -text "There have been no clicks yet."] set count 0 pack [button .b -text " Click Me " -command upd] proc upd {} {.l configure -text "Number of clicks: [incr ::count]."}