so i was trying to make a quiz application but i got this error below
I Want to close the JFrame window so i tried setVisible(false); to close window if someone click back button but it is not working help me as im new. i even tried Jframe.dispose(); and other methods i dont what error i have made if you help me pls
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class Login extends JFrame implements ActionListener {
JButton Rules,Back;
Login() {
getContentPane().setBackground(Color.white);
ImageIcon i1 = new ImageIcon(ClassLoader.getSystemResource("icon/login.jpeg"));//<-- to pick images from directory
setLayout(null);
JLabel image = new JLabel(i1);
JLabel heading = new JLabel("Quiz Bazzi");
heading.setBounds(750,50,300,50);
heading.setFont(new Font("Mongolian Baiti",Font.BOLD,42));
heading.setForeground(Color.ORANGE);
add(heading);
JLabel name = new JLabel("Enter Your Name");
name.setBounds(785,150,350,20);
name.setFont(new Font("Mongolian Baiti",Font.BOLD,18));
name.setForeground(Color.black);
add(name);
JTextField tfname = new JTextField();
tfname.setBounds(715,180,300,25);
tfname.setFont(new Font("Times New Roman",Font.BOLD,18));
tfname.setForeground(Color.black);
add(tfname);
Rules = new JButton("Rules");
Rules.setBounds(715,220,120,25);
Rules.setBackground(Color.black);
Rules.setForeground(Color.white);
add(Rules);
Back = new JButton("Back");
Back.setBounds(895,220,120,25);
Back.setBackground(Color.black);
Back.setForeground(Color.white);
add(Back);
image.setBounds(0,0,500,400);
add(image);
setSize(1200,439);
setLocation(180,100);
setVisible(true);
}
public void actionPerformed(ActionEvent e){
if(e.getSource() == Rules){
setVisible(true);
} else if (e.getSource() == Back) {
setVisible(false);
}
}
public static void main(String[] args) {
new Login();
}
}
Do you know what it the purpose of:
JFrame.dispose();
?
It is exactly what you need :)
Related
Hi im am working on my Java project which I have to create a login page for users to enter, and when login is successful, an alert is supposed to tell them that the login was ok. However, I am having some troubles as my application is not showing up at all, I think it is something to do with the code that I have written. Below is the code:
public class UserLoginPage implements ActionListener {
//Put all JLabels,Frames and buttons here etc
JPanel panel = new JPanel();
JFrame frame = new JFrame();
JLabel userLabel = new JLabel("Username");
JLabel passwordLabel = new JLabel("Password");
JTextField userText = new JTextField();
JTextField passwordText = new JTextField();
JButton loginButton = new JButton("Login");
//Label for successful login
JLabel success = new JLabel("Login Successful");
//Default Constructor to add the frames and panels etc
public UserLoginPage(){
panel.setLayout(null);
userLabel.setBounds(10,20,80,25);
panel.add(userLabel);
passwordLabel.setBounds(10,50,80,25);
panel.add(passwordLabel);
userText.setBounds(100,20,165,25);
panel.add(userText);
passwordText.setBounds(100,50,165,25);
panel.add(passwordText);
loginButton.setBounds(10,80,80,25);
loginButton.addActionListener(new UserLoginPage());
panel.add(loginButton);
success.setBounds(10,110,300,25);
panel.add(success);
//success.setText();
frame.setSize(500,500);
frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
frame.setVisible(true);
frame.add(panel);
}
public static void main(String[] args){
SwingUtilities.invokeLater(new Runnable() {
#Override
public void run() {
new UserLoginPage();
}
});
}
#Override
public void actionPerformed(ActionEvent e) {
System.out.println("Button Clicked");
}
I think the problem lies with loginButton.addActionListener(new UserLoginPage()); But I might be wrong, do let me know of how to solve the problem, thank you.
There are multiple issues in your program:
panel.setLayout(null); and .setBounds(...). You shouldn't be using null-layouts, as Swing has to deal with multiple PLAFs, OS, screen sizes and resolutions. You might end up with issues like this one. Pixel-perfect layouts might seem like the easiest way to create complex UIs but it's not, it'll just lead you to endless issues. Instead use layout managers or combinations of them.
loginButton.addActionListener(new UserLoginPage()); You're creating a new instance of your program every time, and on every instance of it you're creating a new object because all your code is inside the constructor. Just, don't! It's a recursive call that finally creates a java.lang.StackOverflowError, to solve this use this instead of new UserLoginPage()
frame.setVisible(true); this line should always be the last line in your program, after you've added everything to your JFrame, not before.
With the above recommendations, here's an updated version of your code:
import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;
import javax.swing.SwingUtilities;
public class UserLoginPage implements ActionListener {
// Put all JLabels,Frames and buttons here etc
private JPanel panel;
private JFrame frame;
private JLabel userLabel;
private JLabel passwordLabel;
private JTextField userText;
private JTextField passwordText;
private JButton loginButton;
// Label for successful login
private JLabel success;
// Default Constructor to add the frames and panels etc
public UserLoginPage() {
}
private void createAndShowGUI() {
frame = new JFrame(getClass().getSimpleName());
panel = new JPanel();
panel.setLayout(new GridLayout(0, 2));
userLabel = new JLabel("Username");
passwordLabel = new JLabel("Password");
userText = new JTextField(10);
passwordText = new JPasswordField(10);
loginButton = new JButton("Login");
success = new JLabel("Login Successful");
loginButton.addActionListener(this);
panel.add(userLabel);
panel.add(userText);
panel.add(passwordLabel);
panel.add(passwordText);
panel.add(loginButton);
frame.add(panel);
frame.add(success, BorderLayout.SOUTH);
frame.pack();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
#Override
public void run() {
new UserLoginPage().createAndShowGUI();;
}
});
}
#Override
public void actionPerformed(ActionEvent e) {
System.out.println("Button Clicked");
}
}
You were correct. loginButton.addActionListener(new UserLoginPage()) causes a java.lang.StackOverflowError. The constructor is always calling itself without a base case to default to. You should be passing that very instance of the UserLoginPage as a parameter, not a new instance.
Use this code instead:
loginButton.addActionListener(this);
I am new to JAVA swing , where i develop two different JFrame if I click on button frame should move to another frame and previous frame should close by opening of next frame.
I click on button a next frame open but data inside frame is not displaying and previous frame is not closed on button . Please help to find the problem in code.
Frame 1:---------------------------------
package com.demo.test;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.GroupLayout;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import com.demo.gui.TestjigWindow;
public class TestjigWindowCheck extends JFrame{
private JFrame mainFrame;
private JLabel headerLabel;
private JLabel statusLabel;
private JPanel controlPanel;
public TestjigWindowCheck() {
initUI();
}
private void initUI() {
mainFrame = new JFrame("Fuse Test jig");
mainFrame.setSize(400,400);
mainFrame.setLayout(new GridLayout(3, 1));
headerLabel = new JLabel("", JLabel.CENTER);
statusLabel = new JLabel("",JLabel.CENTER);
statusLabel.setSize(500,500);
controlPanel = new JPanel();
controlPanel.setLayout(new FlowLayout());
mainFrame.add(headerLabel);
mainFrame.add(controlPanel);
mainFrame.add(statusLabel);
mainFrame.setVisible(true);
}
public void showEventDemo(){
//TestjigWindow frame1 = new TestjigWindow();
headerLabel.setText("Fuse Test Jig");
headerLabel.setFont(new Font( "Arial", Font.BOLD, 25));
headerLabel.setBackground(Color.green);
JButton startButton = new JButton("Start");
startButton.setActionCommand("Start");
JButton closeButton = new JButton("Close");
closeButton.setActionCommand("close");
startButton.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
try
{
if(e.getSource() == startButton)
{
TestjigWindow2 frame2 = new TestjigWindow2();
frame2.setVisible(true);
dispose();
}
else if(e.getSource() == closeButton)
{
System.exit(0);
}
}
catch (Exception ex)
{
ex.printStackTrace();
}
}
});
closeButton.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
try
{
System.exit(0);
}
catch (Exception ex)
{
ex.printStackTrace();
}
}
});
controlPanel.add(startButton);
controlPanel.add(closeButton);
mainFrame.setVisible(true);
}
public static void main(String[] args) {
TestjigWindowCheck test = new TestjigWindowCheck();
test.showEventDemo();
//test.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
Frame 2----------------------------------- .
package com.demo.test;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import javax.swing.*;
public class TestjigWindow2 extends JFrame{
private JFrame mainFrame;
private JLabel headerLabel;
private JLabel statusLabel;
private JPanel controlPanel;
private JPanel controlPanel1;
public TestjigWindow2()
{
prepareGUI();
}
public static void main(String args[])
{
TestjigWindow2 test = new TestjigWindow2();
test.showRadioButton();
}
private void prepareGUI()
{
mainFrame = new JFrame("Fuse Test2 jig");
mainFrame.setSize(400,400);
mainFrame.setLayout(new GridLayout(3, 1));
headerLabel = new JLabel("", JLabel.CENTER);
statusLabel = new JLabel("",JLabel.CENTER);
statusLabel.setSize(500,500);
controlPanel = new JPanel();
controlPanel.setLayout(new FlowLayout());
mainFrame.add(headerLabel);
mainFrame.add(controlPanel);
mainFrame.add(statusLabel);
mainFrame.setVisible(true);
}
public void showRadioButton()
{
headerLabel.setText("Fuse Mode");
final JRadioButton setting =new JRadioButton("Setting");
final JRadioButton testing =new JRadioButton("Testing");
setting.setBounds(75,50,100,30);
testing.setBounds(75,100,100,30);
setting.setMnemonic(KeyEvent.VK_S);
testing.setMnemonic(KeyEvent.VK_T);
ButtonGroup group = new ButtonGroup();
group.add(setting);
group.add(testing);
controlPanel.add(setting);
controlPanel.add(testing);
JButton button = new JButton("Next");
button.setActionCommand("Next");
controlPanel.add(button);
mainFrame.setVisible(true);
}
}
For this problem, I think it's a fairly simple solution, as Andrew commented, you don't need to keep creating JFrames, you can create your JFrame in your first program, and pass it to your second class through the constructor.
Why I think your program is closing is because you are calling dispose() after creating the new frame which might be destroying the components in your new frame.
You could take this approach, which uses only one frame creating in the opening class and carried over to the second class
For Example (using snipplets of your code):
Frame 1
//This is where you are moving to the second frame.
if(e.getSource() == startButton)
{
mainFrame.getContentPane().removeAll(); //removeAll() method wipes all components attached to the contentpane of the frame
//Frame can be reused when passed to second class
TestjigWindow2 frame2 = new TestjigWindow2(this.mainFrame);
}
Frame 2
//In your constructor you could have something like this
private JFrame mainFrame;
/*
* Other variables and constants go here
*
*/
public TestjigWindow2(JFrame mainFrame)
{
this.mainFrame = mainFrame;
prepareGUI();
}
And then in prepareGUI(), you would then be adding your components to your frame, without creating a new frame. With this, your first page will be closed, and the second frame will be open, without you having to creating mutiple JFrames.
You should just create a new Instance of TestjigWindow2 in the actionPerformed method within your first frame. Instead of adding actionPerformed on the startbutton and stopbutton seperately, implement ActionListener interface in your Frame1 class and just keep one method since you are checking for the source inside the method anyways.
#Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
try
{
if(e.getSource() == startButton)
{
TestjigWindow2 frame2 = new TestjigWindow2();
//frame2.setVisible(true); do this inside the frame2 preparegui method
dispose();
}
else if(e.getSource() == closeButton)
{
System.exit(0);
}
}
catch (Exception ex)
{
ex.printStackTrace();
}
}
});
Also the code will have a more generalized flow if you have the main method inside Frame1 and instantiate the Frame1 in it.
And you don't need to use setVisible inside the actionPerformed of Frame1.
I have to write some Java code to create GUI and the idea is this. The user will input two numbers and then he will choose one of the radiobuttons. If he chooses "zgjidh", he should click a button that will do the calculations depending on what kind of calculation the button is for. "Fshij"button is kinda like reset to clear all field. If he chooses "dil" the window will close. For some reason it doesn't seem to work. Can someone help me?
import java.awt.*;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import javax.swing.*;
import java.awt.event.ActionEvent;
public class Gui extends JFrame implements ActionListener{
JButton plus,minus,fshi;
JTextField vn1,vn2,vrez;
JLabel n1,n2,rezultati;
JRadioButton zgjidh,dil;
ButtonGroup grupi;
public Gui(){
setLayout(new FlowLayout());
JPanel veri=new JPanel();
veri.setLayout(new GridLayout(3,2));
n1=new JLabel("Jep numrin e 1");
veri.add(n1);
vn1=new JTextField(10);
veri.add(vn1);
n2=new JLabel("Jep numrin e 2");
veri.add(n2);
vn2=new JTextField(10);
veri.add(vn2);
rezultati=new JLabel("Rezultati");
veri.add(rezultati);
vrez=new JTextField(10);
veri.add(vrez);
add(veri,BorderLayout.NORTH);
JPanel qender=new JPanel();
qender.setLayout(new GridLayout(2,1));
zgjidh=new JRadioButton("Zgidh Veprimin");
zgjidh.addActionListener(this);
dil=new JRadioButton("Dil");
dil.addActionListener(this);
grupi=new ButtonGroup();
grupi.add(zgjidh);
grupi.add(dil);
qender.add(zgjidh);
qender.add(dil);
add(qender,BorderLayout.CENTER);
JPanel jug=new JPanel();
jug.setLayout(new GridLayout(1,3));
plus=new JButton("+");
plus.addActionListener(this);
jug.add(plus);
minus=new JButton("-");
minus.addActionListener(this);
jug.add(minus);
fshi=new JButton("Fshij");
fshi.addActionListener(this);
jug.add(fshi);
add(jug,BorderLayout.SOUTH);
}
public void actionPerformed (ActionEvent e){
double n1=Double.parseDouble(vn1.getText());
double n2=Double.parseDouble(vn2.getText());
if (e.getSource()==zgjidh) {
if (e.getSource()==plus) {
vrez.setText(""+(n1+n2));
}
else if (e.getSource()==minus) {
vrez.setText(""+(n1-n2));
}
else if (e.getSource()==fshi) {
vn1.setText("");
vn2.setText("");
vrez.setText("");
}
}
else if(e.getSource()==dil) {
System.exit(0);
}
}
public static void main(String args[]) {
Gui ob=new Gui();
ob.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
ob.setSize(500,500);
ob.setVisible(true);
}
}
Was able to fix it. This is the modified code
public void actionPerformed(ActionEvent e){
double n1=Double.parseDouble(vn1.getText());
double n2=Double.parseDouble(vn2.getText());
if (zgjidh.isSelected()) {
if (e.getSource()==plus) {
vrez.setText(""+(n1+n2));
}
else if (e.getSource()==minus) {
vrez.setText(""+(n1-n2));
}
else if (e.getSource()==fshi) {
vn1.setText("");
vn2.setText("");
vrez.setText("");
}
}
else if(dil.isSelected()) {
System.exit(0);
}
}
This question already has answers here:
Once exported, java cannot find/draw images
(2 answers)
Closed 6 years ago.
I'm a student currently studying Java Programming and using Netbeans to create the application. The program is already done and loads nicely in the IDE (with images). I have to build it into JAR for a presentation to my lecturer and has done it but the images are not present in the JAR.
First of all, I've checked all the available answers for allowing images to be present in the JAR but I couldn't get it right with the program as it doesn't load even in IDE and shows error. Which most have pointed out that I have to input (getClass().getClassLoader().getResource("image.jpg")). I tried inputting it but it shows errors, mostly because my codes for placing the ImageIcon are different.
Below is my full code of the JFrame presenting the program:
import java.awt.Color;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;
import javax.swing.*;
import javax.swing.ImageIcon;
import java.util.logging.Level;
import java.util.logging.Logger;
import static javax.swing.JFrame.EXIT_ON_CLOSE;
public class A2{
public void GUI () throws IOException {
JFrame frame = new JFrame();
frame.setVisible(true);
frame.setTitle("Minus");
frame.setSize(700,500);
frame.setDefaultCloseOperation(EXIT_ON_CLOSE);
//Set the layout
JPanel panel;
panel = new JPanel();
panel.setLayout(null);
frame.setContentPane(panel);
// Create all components
JLabel ctgy = new JLabel("Minus");
JLabel minus2 = new JLabel("What is 6 squares minus 3 squares?");
JButton ans_a = new JButton("9");
JButton ans_b = new JButton("3");
JButton ans_c = new JButton("7");
JButton back = new JButton("Back");
JLabel min2 = new JLabel();
//Add objects to layout
panel.add(ctgy);
panel.add(minus2);
panel.add(min2);
panel.add(ans_a);
panel.add(ans_b);
panel.add(ans_c);
panel.add(back);
// Set position of objects in content pane
min2.setLocation(100,100);
minus2.setLocation(20,50);
ctgy.setLocation(10,3);
ans_a.setLocation(500,100);
ans_b.setLocation(500,150);
ans_c.setLocation(500,200);
back.setLocation(500, 400);
//Set size of object
min2.setSize(300,300);
ctgy.setSize(200,50);
minus2.setSize(350,50);
ans_a.setSize(100,30);
ans_b.setSize(100,30);
ans_c.setSize(100,30);
back.setSize(100, 30);
//Set the fonts and colors
Font font1 = new Font("Cooper Black", Font.BOLD, 26);
Font font2 = new Font("Calisto MT", Font.BOLD, 20);
ctgy.setFont(font1);
ctgy.setBackground(Color.white);
minus2.setFont(font2);
minus2.setBackground(Color.red);
panel.setBackground (Color.RED);
ans_a.setBackground(Color.white);
ans_b.setBackground(Color.white);
ans_c.setBackground(Color.white);
min2.setIcon(new ImageIcon("src/images/6-3.png"));
ans_b.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent arg0) {
JOptionPane.showMessageDialog(null, "Correct!");
try {
A3.main(null);
} catch (IOException ex) {
Logger.getLogger(A1.class.getName()).log(Level.SEVERE, null, ex);
}
}
});
ans_a.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent arg0) {
JOptionPane.showMessageDialog(null, "Incorrect! Please try again.");
}
});
ans_c.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent arg0) {
JOptionPane.showMessageDialog(null, "Incorrect! Please try again.");
}
});
frame.repaint();
back.addActionListener(new ActionListener () {
public void actionPerformed(ActionEvent f){
Categories.main(null);
}
});
}
public static void main (String [] args) throws IOException {
A2 gd = new A2();
gd.GUI();
}
}
This is the part of my code where I state the JLabel to put my image in which I named as min2:
JLabel ctgy = new JLabel("Minus");
JLabel minus2 = new JLabel("What is 6 squares minus 3 squares?");
JButton ans_a = new JButton("9");
JButton ans_b = new JButton("3");
JButton ans_c = new JButton("7");
JButton back = new JButton("Back");
JLabel min2 = new JLabel();
This is adding the panel for the JLabel:
panel.add(min2);
The size and location:
min2.setLocation(100,100);
min2.setSize(300,300);
Lastly the image itself and location:
min2.setIcon(new ImageIcon("src/images/6-3.png"));
This part will show error because I set this to open a new Class when a user click the JButton so that would happen since you don't have the A3 Class:
public void actionPerformed(ActionEvent arg0) {
JOptionPane.showMessageDialog(null, "Correct!");
try {
A3.main(null);
} catch (IOException ex) {
Logger.getLogger(A1.class.getName()).log(Level.SEVERE, null, ex);
}
I've checked the JAR file with WinRAR and confirmed that images folder and the images are inside. I wanted to post the screenshots but Imgur isn't working for me.
The pathfile for all images are inside src/images.
Please suggest what changes I need to make. Thank you and sorry if it has been asked too much.
jLabel1.setIcon(new javax.swing.ImageIcon(getClass().getResource("/images/img.png")));
this is how you should actually load the image file.your given file path is wrong try it with this approach.
By now, I already know my code is flawed. I just want to know why it's flawed. I want to activate the "webButton" so that when it gets clicked, it prints a message on the console that reads "This opens Mozilla Firefox."
package smartphone;
import java.awt.*;
import java.awt.event.ActionEvent;
import javax.swing.*;
import java.util.Scanner;
public class Smartphone implements ActionListener {
public static void main(String[] args) {
{
{
JFrame container = new JFrame();
container.setLayout(new BorderLayout());
Scanner daniel = new Scanner(System.in);
JButton webButton = new JButton(new ImageIcon("Firefox.png"));
JButton phoButton = new JButton(new ImageIcon("Facebook.png"));
JButton texButton = new JButton(new ImageIcon("Phone.png"));
JButton setButton = new JButton(new ImageIcon("Settings.png"));
JButton smsButton = new JButton(new ImageIcon("sms.png"));
container.setTitle("Smartphone Interface!");
container.setSize(240,340);
container.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
container.add(setButton, BorderLayout.CENTER);
container.add(webButton, BorderLayout.SOUTH);
container.add(texButton, BorderLayout.NORTH);
container.add(phoButton, BorderLayout.EAST);
container.add(smsButton, BorderLayout.WEST);
container.setVisible(true);
webButton.addActionListener(instanceofSmartphone);
}
}
}
}
Do this.
webButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e)
{
//Execute when button is pressed
}
});
If you want to use the ActionListener interface then, implement it in your Frame class and then replace that instanceOfSmartphone with
webButton.addActionListener(this);
And put this outside the method
public void actionPerformed(ActionEvent e) {
if(e.getSource() == webButton) {
}
}
You need to implement the actionPerformed method. An example follows.
public void actionPerformed(ActionEvent e) {
System.out.println("This method opens Mozilla Firefox.");
}
Additionally, you need to change how you add the action listener to the following.
webButton.addActionListener(this);
There are also a number of other issues. Here is a modified version of your code to get it working but far from perfect or what you will want in the end. I strongly recommend you walk through all of the tutorials in order at the below website. It doesn't get any easier than what they have. Also, if your not already using it, you should try Netbeans or some other IDE. It give you feedback that may help while you are starting out.
https://docs.oracle.com/javase/tutorial/
package smartphone;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
import java.util.Scanner;
public class Smartphone extends Frame implements ActionListener {
static JButton webButton = new JButton(new ImageIcon("Firefox.png"));
static JButton phoButton = new JButton(new ImageIcon("Facebook.png"));
static JButton texButton = new JButton(new ImageIcon("Phone.png"));
static JButton setButton = new JButton(new ImageIcon("Settings.png"));
static JButton smsButton = new JButton(new ImageIcon("sms.png"));
Smartphone(){
webButton.addActionListener(this);
}
public static void main(String[] args) {
Smartphone container = new Smartphone();
container.setLayout(new BorderLayout());
Scanner daniel = new Scanner(System.in);
container.setTitle("Smartphone Interface!");
container.setSize(240, 340);
container.add(setButton, BorderLayout.CENTER);
container.add(webButton, BorderLayout.SOUTH);
container.add(texButton, BorderLayout.NORTH);
container.add(phoButton, BorderLayout.EAST);
container.add(smsButton, BorderLayout.WEST);
container.setVisible(true);
}
#Override
public void actionPerformed(ActionEvent e) {
System.out.println("This opens a Firefox Webbrowser.");
}
}
You need to add an ActionListener to the buttons INSIDE of the constructor: buttonName.addActionListener(this);.
Then you need to create the following method:
public void actionPerformed(ActionEvent event) {
Object control = event.getSource();
if (control == buttonName) {
//Run code...
}
}