Button Help
Author |
Message |
Krocker
|
Posted: Mon Dec 19, 2011 5:58 pm Post subject: Button Help |
|
|
Hey guys, im trying to make a computer cleaning software that deletes specific files specified within the program. I made a basic interface with only 2 buttons for now, but i need help positioning it and making the buttons do something when clicked. im really new to this so please be specific:
code: |
import java.awt.*;
import java.awt.event.*;
public class ButtonText
{
public static void main (String[] args)
{
Frame frame = new Frame ("KCleaner");
Button button1 = new Button ("Clean");
frame.add (button1);
Button button2 = new Button ("About");
frame.add (button2);
frame.setLayout (new FlowLayout ());
frame.setSize (300, 100);
frame.setVisible (true);
frame.addWindowListener (new WindowAdapter ()
{
public void windowClosing (WindowEvent e)
{
System.exit (0);
}
}
);
}
}
|
|
|
|
|
|
![](images/spacer.gif) |
Sponsor Sponsor
![Sponsor Sponsor](templates/subSilver/images/ranks/stars_rank5.gif)
|
|
![](images/spacer.gif) |
randint
|
Posted: Fri Dec 30, 2011 10:18 pm Post subject: Re: Button Help |
|
|
Well, you can use the setBounds(int x, int y, int width, int height) to position your buttons within the JFrame, as for how to make the buttons do something, you need:
a constructor (some method that has the same name, same case that the class does) and some ActionListener objects, for example:
Java: |
import java.awt.event.*;
import javax.swing.*;
public class Test extends JFrame implements ActionListener
{
public JFrame f1 = new JFrame ("Frame");
public JPanel p1 = new JPanel ();
public JButton b1 = new JButton ("Button 1");
public JButton b2 = new JButton ("Button 2");
public Test ()
{
b1. addActionListener(this);
b2. addActionListener(this);
f1. setContentPane(p1 );
f1. setSize(400, 400);
p1. add (b1 );
p1. add(b2 );
f1. setVisible (true);
}
public void actionPerformed (ActionEvent e )
{
if (e. getSource () == b1 )
{
JOptionPane. showMessageDialog (null, "Button 1 is clicked!");
}
else if (e. getSource () == b2 )
{
JOptionPane. showMessageDialog(null, "Button 2 is clicked");
}
}
public static void main (String args [])
{
new Test ();
}
}
|
So, there needs to be a void method named "actionPerformed" with parameter ActionPerformed e (it has to be that way, otherwise there WILL be a SYNTAX ERROR), then use the getSource() method to respond to mouse events (clicks) that are "listened" by ActionListener. |
|
|
|
|
![](images/spacer.gif) |
Velocity
![](http://compsci.ca/v3/uploads/user_avatars/1809397984eb9e2888e99b.jpg)
|
Posted: Wed Jan 11, 2012 11:33 am Post subject: Re: Button Help |
|
|
import java.awt.event.*;
import javax.swing.*;
public class Test extends JFrame implements ActionListener
{
public JFrame f1 = new JFrame ("Frame");
public JPanel p1 = new JPanel ();
public JButton b1 = new JButton ("Button 1");
public JButton b2 = new JButton ("Button 2");
public Test ()
{
b1.addActionListener(this);
b2.addActionListener(this);
f1.setContentPane(p1);
f1.setSize(400, 400);
p1.add (b1);
p1.add(b2);
f1.setVisible (true);
}
public void actionPerformed (ActionEvent e)
{
if (e.getSource () == b1)
{
JOptionPane.showMessageDialog (null, "button one was pressed");
}
if (e.getSource () == b2)
}
JOptionPane.showMessageDialog (null, "button two was pressed");
{
return (value) 0 + (newValue);
System.out ("pause")
}
is a true statement ^^^^ |
|
|
|
|
![](images/spacer.gif) |
|
|