Java JButton

52 views
Skip to first unread message

[mck]

unread,
Jun 8, 2016, 6:20:17 AM6/8/16
to Haxe
Hi all, 

I have been trying to figure out Haxe and Java.
And document my progress.

Which is difficult so I end up here.

So perhaps could you help me:

I try to activate a JButton:


import java.javax.swing.JFrame;

import java.javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

class Main extends JFrame {
public function new () 
{
super('test');

var button = new JButton('Press me to quit');

// button.addActionListener((new ActionListener() : {

// }));


// button.addActionListener(function() {
// button.setText('thank you');
// });


var pane = this.getContentPane();
var layout = new java.javax.swing.GroupLayout(pane);
pane.setLayout(layout);
layout.setAutoCreateContainerGaps(true);
layout.setHorizontalGroup(layout.createSequentialGroup().addComponent(button));
layout.setVerticalGroup(layout.createSequentialGroup().addComponent(button));
this.setTitle('Hello');
this.setSize(400, 300);
this.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
this.setVisible(true);
}

static public function main () {
var app = new Main ();
}
}

I have tried 
https://gist.github.com/cambiata/b137158782f7a3f45c67414cbcb38516
but the macro used here, needs tink/macro/core and then fails

So I was curious how it (without the use of macros) should work

thx

JLM

unread,
Jun 8, 2016, 8:44:35 AM6/8/16
to Haxe




class Main extends JFrame implements MouseListener {
                
var button:JButton;
        public function new () 
{
super('test');

var button = new JButton('Press me to quit');
button.addEventListener(this);
}
        public function mousePressed( e: MouseEvent ) {
                button.setText('no worries');

        }
static public function main () {
var app = new Main ();
}

}
Something like this should work you may want to try implements ActionListener and use actionPerformed as the method instead, the compiler will probably make you add all the implements methods but you can leave them empty if you don't need them.
Message has been deleted

[mck]

unread,
Jun 8, 2016, 10:28:28 AM6/8/16
to Haxe
ah that helps:

it is button.addMouseListener
and indeed you need to have more methods

Field mouseClicked needed by java.awt.event.MouseListener is missing

Field mouseEntered needed by java.awt.event.MouseListener is missing

Field mouseExited needed by java.awt.event.MouseListener is missing

Field mouseReleased needed by java.awt.event.MouseListener is missing


package;

import java.javax.swing.JFrame;
import java.javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseEvent;


class Main extends JFrame implements MouseListener {
var button : JButton;

public function new () 
{
super('test');

button = new JButton('Press me to quit');
button.addMouseListener(this);

var pane = this.getContentPane();
var layout = new java.javax.swing.GroupLayout(pane);
pane.setLayout(layout);
layout.setAutoCreateContainerGaps(true);
layout.setHorizontalGroup(layout.createSequentialGroup().addComponent(button));
layout.setVerticalGroup(layout.createSequentialGroup().addComponent(button));
this.setTitle('Hello');
this.setSize(400, 300);
this.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
this.setVisible(true);
}

public function mousePressed( e : MouseEvent ) {
button.setText('no worries');
haxe.Timer.delay(kill, 1000);
}
public function mouseClicked( e : MouseEvent ) {}
public function mouseEntered( e : MouseEvent ) {}
public function mouseExited( e : MouseEvent ) {}
public function mouseReleased( e : MouseEvent ) {}


function kill (){
Sys.exit(0);

[mck]

unread,
Jun 8, 2016, 10:36:56 AM6/8/16
to Haxe
btw this works also:

package;

import java.javax.swing.JFrame;
import java.javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

 
class Main extends JFrame implements ActionListener {
var button : JButton;

public function new () 
{
super('test');

button = new JButton('Press me to quit');
button.addActionListener(this);

var pane = this.getContentPane();
var layout = new java.javax.swing.GroupLayout(pane);
pane.setLayout(layout);
layout.setAutoCreateContainerGaps(true);
layout.setHorizontalGroup(layout.createSequentialGroup().addComponent(button));
layout.setVerticalGroup(layout.createSequentialGroup().addComponent(button));
this.setTitle('Hello');
this.setSize(400, 300);
this.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
this.setVisible(true);
}

public function actionPerformed( e : ActionEvent) {
button.setText('no worries');
haxe.Timer.delay(kill, 1000);
}


function kill (){
Sys.exit(0);
Reply all
Reply to author
Forward
0 new messages