Not logged in? Join one of the bigest Law Forums on the Internet! Join Now!   Latest blog post: Research Law Professors Before Choosing Law Schools

Advertisments:




Sponsor Links:

Discount Legal Forms
Discounted Legal Texts


How can I add a action to a button in java. When I click on a button I want it to open a new frame.?

Dealing with a class action? Discuss it here

How can I add a action to a button in java. When I click on a button I want it to open a new frame.?

Postby cumhea » Sat Mar 31, 2012 3:36 am

package test;
public class Weight extends javax.swing.JFrame {
public Weight() {
initComponents();
}

private void initComponents() {

jLabel1 = new javax.swing.JLabel();
jLabel2 = new javax.swing.JLabel();
jLabel3 = new javax.swing.JLabel();
jLabel4 = new javax.swing.JLabel();
jLabel5 = new javax.swing.JLabel();
jTextField1 = new javax.swing.JTextField();
jTextField2 = new javax.swing.JTextField();
jTextField3 = new javax.swing.JTextField();
jTextField4 = new javax.swing.JTextField();
jButton1 = new javax.swing.JButton();
jButton2 = new javax.swing.JButton();
jButton3 = new javax.swing.JButton();
jButton4 = new javax.swing.JButton();
jButton5 = new javax.swing.JButton();
jButton6 = new javax.swing.JButton();
jButton7 = new javax.swing.JButton();
jButton8 = new javax.swing.JButton();
jButton9 = new javax.swing.JButton();

setDefaultCloseOperation(javax.swing.Win…

jLabel1.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
jLabel1.setText("Weight");

jLabel2.setText("Week 1");

jLabel3.setText("Week 2");

jLabel4.setText("Week 3");

jLabel5.setText("Week 4");

jButton1.setText("Search");

jButton2.setText("Patient");

jButton3.setText("Fluid");

jButton4.setText("Nutrition");

jButton5.setText("Weight");

jButton6.setText("Stool");

jButton7.setText("Discharge");

jButton8.setText("LogOut");

jButton9.setText("Save");
cumhea
 
Posts: 16
Joined: Thu Mar 31, 2011 2:51 pm
Top

How can I add a action to a button in java. When I click on a button I want it to open a new frame.?

Postby sayre » Sat Mar 31, 2012 3:37 am

First of all, you are making it a lot harder on yourself by not importing javax.swing.* and java.awt.*. If you do that, then you don't have to type javax.swing.nameOfClass when you want to use a class. You can just type the name. As to your question though, you need to implement java.awt.event.ActionListener. Firstly, though, you need to import java.awt.event.* (or you can use the full path whenever you need an event, but that's not wise). To do this, change your class declaration to this:

import java.awt.event.*;

public class Weight extends javax.swing.JFrame implements ActionListener{


No that that's done, you have to have a method called:

public void actionPerformed(ActionEvent ae)

In that method, you put the stuff that you want to happen when an action is performed (the button is clicked). To make this work, add this line to your code:

JButton button = new javax.swing.JButton();
button.addActionListener(this);

When button is clicked, the code in actionPerformed will be executed. If you want different things to happen when different buttons are clicked, you need to make multiple classes that implement ActionListener, each with their own actionPerformed methods. I hope this helps.
sayre
 
Posts: 12
Joined: Thu Mar 31, 2011 9:14 pm
Top


Return to Class Action

 


  • Related topics
    Replies
    Views
    Last post
cron