|
Answer» I am just playing around with Java GUI. Here is a simple code, it has no main method because I haven't got to there yet but I am getting this ERROR:
txtFirstName cannot be resolved txtLastName cannot be resolved .... this is referenced to the ButtonHandler class at the bottom of the code in the ActionPerformed method.
And if anyone can explain to me what @Override is for, is that for override ActionPerformed method?
If anyone has any experience with GUI please help, I followed my teacher's example and that's what I got, not sure what's wrong. Thanks in advance guys.
import javax.swing.*; import java.awt.*; import java.awt.event.*;
public class Test extends JFrame { public Test() { JLabel lblFirstName, lblLastName, lblAddress, lblStudentNumber; JTextField txtFirstName, txtLastName, txtAddress, txtStudentNumber; JButton btnSend;
JPanel panel = NEW JPanel(); lblFirstName = new JLabel("First Name:"); lblLastName = new JLabel("Last Name"); lblAddress = new JLabel("Address:"); lblStudentNumber = new JLabel("Student Number");
txtFirstName = new JTextField(20); txtLastName = new JTextField(20); txtAddress = new JTextField(20); txtStudentNumber = new JTextField(20);
btnSend = new JButton("Send");
panel.add(lblFirstName); panel.add(txtFirstName);
panel.add(lblLastName); panel.add(txtLastName);
panel.add(lblAddress); panel.add(txtAddress);
panel.add(lblStudentNumber); panel.add(txtStudentNumber);
panel.add(btnSend);
btnSend.addActionListener(new ButtonHandler()); }
public class ButtonHandler implements ACTIONLISTENER { @Override public void actionPerformed(ActionEvent arg0) { JOptionPane.showMessageDialog(null, txtFirstName.getText() + "\n" + txtLastName.getText() + "\n" + txtAddress.getText() + "\n" + txtStudentNumber.getText()); } }
} You haven't specified a location for each controls. Thou JAVA should automatically perform this task but i think you are miss some code! READ this
import java.awt.BorderLayout; import java.awt.FlowLayout; import java.awt.event.ActionEvent; import java.awt.event.ActionListener;
import javax.swing.JButton; import javax.swing.JCheckBox; import javax.swing.JFrame; import javax.swing.JPanel; import javax.swing.JTextArea;
/** * A very simple java swing application. * Contains button and checkbox. RESPONDS * to manipulations with these controls by * changing text in the main text area. * * @author audriusa */ public class WikiHow extends JFrame implements ActionListener { /** * The button. */ JButton myButton = new JButton("Button");
/** * The the checkbox. */ JCheckBox myCheckBox = new JCheckBox("Check");
/** * The text area. */ JTextArea myText = new JTextArea("My text");
/** * The bottom panel which holds button. */ JPanel bottomPanel = new JPanel();
/** * The top level panel which holds all. */ JPanel holdAll = new JPanel();
/** * The constructor. */ public WikiHow() { bottomPanel.setLayout(new FlowLayout()); bottomPanel.add(myCheckBox); bottomPanel.add(myButton);
holdAll.setLayout(new BorderLayout()); holdAll.add(bottomPanel, BorderLayout.SOUTH); holdAll.add(myText, BorderLayout.CENTER);
getContentPane().add(holdAll, BorderLayout.CENTER);
myButton.addActionListener(this); myCheckBox.addActionListener(this);
setDefaultCloseOperation(DISPOSE_ON_CLOSE); }
/** * The program entry point, this method makes class executable. * * @param args the program start up parameters, not used. */ public static void main(String[] args) { WikiHow myApplication = new WikiHow();
// Specify where will it appear on the screen: myApplication.setLocation(10, 10); myApplication.setSize(300, 300);
// Show it! myApplication.setVisible(true); }
/** * Each non abstract class that implements the ActionListener * must have this method. * * @param e the action event. */ public void actionPerformed(ActionEvent e) { if (e.getSource() == myButton) myText.setText("A button click"); else if (e.getSource() == myCheckBox) myText.setText("The checkbox state changed to " + myCheckBox.isSelected()); else myText.setText("E ...?"); } }Quote And if anyone can explain to me what @Override is for, is that for override ActionPerformed method? It just points out to anyone reading the code that the next method is over-riding the original. It is not necessary for the code to work.
I think the problem is that txtFirstName etc are only declared within the scope of the constructor; ie. can only be accessed from the constructor. If you want them to be accessed from outdiee the constructor's scope (and you do, you want to access them in ButtonHandler), then put them as properties. Usually there is this structure: Code: [Select]public class MyClass.....{ //properties here ... ... ... //constructors here .. .. .. //methods and nested classes here ... ... .. } //end class
Hope this shed a little light,
Two-eyes %Sorry for not replying for a while the forum banned me for no reason.
Thank you Two-eyes for pointing that out. I've spent about four hours on Thursday trying to figure things out and now I have a general understanding of how to use GUI and laying it out using GridLayout.
Thank you guys.
|