Ok, here is another newbie in java. I want to create an Applet program. When the button “Show” is clicked, the information typed in the two JTextFields (if any), will be displayed on the JLabel. On the other hand, when the button “Clear” is clicked, the information typed in the two JTextField (if any), will be cleared.
the complicated code(for newbie me) is right here:
JLabel lblName = new JLabel("Name:");
JLabel lblAge = new JLabel("Age:");
textField = new JTextField();
textField.setColumns(10);
textField_1 = new JTextField();
textField_1.setColumns(10);
JButton btnShow = new JButton("Show");
btnShow.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
textField.setText("Max");
textField_1.setText("25");
}
});
JButton btnClear = new JButton("Clear");
btnClear.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
textField.setText("");
textField_1.setText("");
}
Here my whole code using Eclipse:
package applet.oum;
import java.awt.BorderLayout;
import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;
import javax.swing.border.TitledBorder;
import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.LayoutStyle.ComponentPlacement;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.Toolkit;
import javax.swing.JMenuBar;
import javax.swing.JMenu;
import javax.swing.JMenuItem;
import javax.swing.ImageIcon;
public class Applet extends JFrame {
private JPanel contentPane;
private JTextField textField;
private JTextField textField_1;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
Applet frame = new Applet();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the frame.
*/
public Applet() {
setIconImage(Toolkit.getDefaultToolkit().getImage( Applet.class.getResource("/buttons/img/KK.png")));
setTitle("Applet for Me");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 450, 300);
JMenuBar menuBar = new JMenuBar();
setJMenuBar(menuBar);
JMenu mnFile = new JMenu("File");
menuBar.add(mnFile);
JMenuItem mntmExit = new JMenuItem("Exit");
mntmExit.setIcon(new ImageIcon(Applet.class.getResource("/buttons/img/Snap4-16.png")));
mntmExit.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
dispose();
}
});
mnFile.add(mntmExit);
JMenu mnAbout = new JMenu("About");
menuBar.add(mnAbout);
JMenuItem mntmAboutThis = new JMenuItem("About This");
mntmAboutThis.setIcon(new ImageIcon(Applet.class.getResource("/buttons/img/Snap6-16.png")));
mnAbout.add(mntmAboutThis);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
JPanel panelApplet = new JPanel();
panelApplet.setBorder(new TitledBorder(null, "Applet", TitledBorder.LEADING, TitledBorder.TOP, null, null));
GroupLayout gl_contentPane = new GroupLayout(contentPane);
gl_contentPane.setHorizontalGroup(
gl_contentPane.createParallelGroup(Alignment.LEADI NG)
.addComponent(panelApplet, GroupLayout.DEFAULT_SIZE, 424, Short.MAX_VALUE)
);
gl_contentPane.setVerticalGroup(
gl_contentPane.createParallelGroup(Alignment.LEADI NG)
.addComponent(panelApplet, GroupLayout.DEFAULT_SIZE, 252, Short.MAX_VALUE)
);
JLabel lblName = new JLabel("Name:");
JLabel lblAge = new JLabel("Age:");
textField = new JTextField();
textField.setColumns(10);
textField_1 = new JTextField();
textField_1.setColumns(10);
JButton btnShow = new JButton("Show");
btnShow.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
textField.setText("Max");
textField_1.setText("25");
}
});
JButton btnClear = new JButton("Clear");
btnClear.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
textField.setText("");
textField_1.setText("");
}
});
GroupLayout gl_panelApplet = new GroupLayout(panelApplet);
gl_panelApplet.setHorizontalGroup(
gl_panelApplet.createParallelGroup(Alignment.TRAIL ING)
.addGroup(Alignment.LEADING, gl_panelApplet.createSequentialGroup()
.addGap(74)
.addGroup(gl_panelApplet.createParallelGroup(Align ment.TRAILING)
.addComponent(lblName)
.addComponent(lblAge))
.addGap(18)
.addGroup(gl_panelApplet.createParallelGroup(Align ment.LEADING, false)
.addComponent(textField_1)
.addComponent(textField, GroupLayout.DEFAULT_SIZE, 220, Short.MAX_VALUE))
.addGap(69))
.addGroup(gl_panelApplet.createSequentialGroup()
.addContainerGap(112, Short.MAX_VALUE)
.addComponent(btnShow)
.addGap(62)
.addComponent(btnClear)
.addGap(122))
);
gl_panelApplet.setVerticalGroup(
gl_panelApplet.createParallelGroup(Alignment.LEADI NG)
.addGroup(gl_panelApplet.createSequentialGroup()
.addGap(19)
.addGroup(gl_panelApplet.createParallelGroup(Align ment.BASELINE)
.addComponent(textField, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
.addComponent(lblName))
.addGap(18)
.addGroup(gl_panelApplet.createParallelGroup(Align ment.BASELINE)
.addComponent(textField_1, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
.addComponent(lblAge))
.addPreferredGap(ComponentPlacement.RELATED, 91, Short.MAX_VALUE)
.addGroup(gl_panelApplet.createParallelGroup(Align ment.BASELINE)
.addComponent(btnClear)
.addComponent(btnShow))
.addGap(38))
);
panelApplet.setLayout(gl_panelApplet);
contentPane.setLayout(gl_contentPane);
}
}
Snap1.jpg
any help would b appreciate.. TQ..
There is no error about this code, it just that i want it to be like, when the button “Show” is clicked, the information typed in the two JTextFields (if any), will be displayed on the JLabel. On the other hand, when the button “Clear” is clicked, the information typed in the two JTextField (if any), will be cleared. The problem is, what ever names i enter will output name: Max, and age: 25. I want it to display different names and ages.