O
Oxnard
I am trying to have 2 JPanels in one JFrame. The first JPanel (Panel1) has
two buttons. The second JPanel has a JTextField. The app works as expected
if I have only one button, but if I have a second button the second button
does not work. I found this example online and would like to expand it to be
able to use more than one button. What do I need to do to get the second
button to do a simple println? I have seen a number of posting but I am just
not quite getting it.
Thanks
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class Test extends JFrame{
public Test() {
super("hello");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(400,400);
Panel1 p1 = new Panel1();
Panel2 p2 = new Panel2();
getContentPane().setLayout(new BorderLayout());
getContentPane().add(p1, BorderLayout.SOUTH);
getContentPane().add(p2, BorderLayout.CENTER);
pack();
setVisible(true);
}
public static void main(String[] args){
setDefaultLookAndFeelDecorated(true);
new Test();
}
}
public class YourHandler implements ActionListener{
private static JButton theButton;
private static JTextField theField;
public YourHandler() {
}
public YourHandler(JButton b){
theButton = b;
}
public YourHandler(JTextField tf){
theField = tf;
}
public void actionPerformed(ActionEvent e) {
if(e.getSource() == theButton){
System.out.println(theField.getText());
}
}
}
public class Panel1 extends JPanel{
private JButton Show;
private JButton Clear;
public Panel1() {
Show = new JButton("Show");
Clear = new JButton("Clear");
Show.addActionListener(new YourHandler(Show));
Clear.addActionListener(new YourHandler(Clear));
this.add(Show);
this.add(Clear);
}
}
public class YourHandler implements ActionListener{
private static JButton theButton;
private static JTextField theField;
public YourHandler(JButton b){
theButton = b;
}
public YourHandler(JTextField tf){
theField = tf;
}
public void actionPerformed(ActionEvent e) {
if(e.getSource() == theButton){
System.out.println(theField.getText());
}
}
}
two buttons. The second JPanel has a JTextField. The app works as expected
if I have only one button, but if I have a second button the second button
does not work. I found this example online and would like to expand it to be
able to use more than one button. What do I need to do to get the second
button to do a simple println? I have seen a number of posting but I am just
not quite getting it.
Thanks
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class Test extends JFrame{
public Test() {
super("hello");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(400,400);
Panel1 p1 = new Panel1();
Panel2 p2 = new Panel2();
getContentPane().setLayout(new BorderLayout());
getContentPane().add(p1, BorderLayout.SOUTH);
getContentPane().add(p2, BorderLayout.CENTER);
pack();
setVisible(true);
}
public static void main(String[] args){
setDefaultLookAndFeelDecorated(true);
new Test();
}
}
public class YourHandler implements ActionListener{
private static JButton theButton;
private static JTextField theField;
public YourHandler() {
}
public YourHandler(JButton b){
theButton = b;
}
public YourHandler(JTextField tf){
theField = tf;
}
public void actionPerformed(ActionEvent e) {
if(e.getSource() == theButton){
System.out.println(theField.getText());
}
}
}
public class Panel1 extends JPanel{
private JButton Show;
private JButton Clear;
public Panel1() {
Show = new JButton("Show");
Clear = new JButton("Clear");
Show.addActionListener(new YourHandler(Show));
Clear.addActionListener(new YourHandler(Clear));
this.add(Show);
this.add(Clear);
}
}
public class YourHandler implements ActionListener{
private static JButton theButton;
private static JTextField theField;
public YourHandler(JButton b){
theButton = b;
}
public YourHandler(JTextField tf){
theField = tf;
}
public void actionPerformed(ActionEvent e) {
if(e.getSource() == theButton){
System.out.println(theField.getText());
}
}
}