Java: I have two classes. The first BaseForm
(BaseForm.java) extends JFrame
, the second ErrorM
(ErrorM.java) extends BaseForm
. I want to close ErrorM
by a click on OK button.
I know I am supposed to use the setVisible(false)
but I do not know what is the object to invoke it.
The classes:
package interfaceClasses;
import java.awt.GraphicsConfiguration;
import java.awt.HeadlessException;
import java.awt.Dimension;
import javax.swing.JFrame;
import java.awt.Color;
public class BaseForm extends JFrame {
public BaseForm() throws HeadlessException {
Dimension d = new Dimension(1100, 850);
setTitle("Employee Payroll System");
setSize(d);
setLocationRelativeTo(null);
getContentPane().setBackground(new Color(107, 142, 35));
getContentPane().setLayout(null);
setBackground(new Color(107, 142, 35));
}
}
and
package interfaceClasses;
import java.awt.BorderLayout;
import java.awt.FlowLayout;
import javax.swing.JButton;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import java.awt.Color;
import javax.swing.JLabel;
import java.awt.Font;
import javax.swing.SwingConstants;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
public class ErrorM extends BaseForm{
private final JPanel contentPanel = new JPanel();
public ErrorM(String errT, String errM) {
setTitle("Error");
setLocationRelativeTo(null);
setBounds(100, 100, 450, 300);
getContentPane().setLayout(new BorderLayout());
contentPanel.setBackground(new Color(107, 142, 35));
contentPanel.setBorder(new EmptyBorder(5, 5, 5, 5));
getContentPane().add(contentPanel, BorderLayout.CENTER);
contentPanel.setLayout(null);
{
JLabel errorText = new JLabel("Error Text 1");
errorText.setBounds(6, 6, 438, 53);
errorText.setHorizontalAlignment(SwingConstants.CENTER);
errorText.setForeground(Color.WHITE);
errorText.setFont(new Font("Helvetica Neue", Font.PLAIN, 20));
errorText.setText(errT);
contentPanel.add(errorText);
}
{
JLabel errorMessage = new JLabel("Error Text 2");
errorMessage.setBounds(6, 67, 438, 166);
errorMessage.setHorizontalAlignment(SwingConstants.CENTER);
errorMessage.setForeground(Color.WHITE);
errorMessage.setFont(new Font("Helvetica Neue", Font.BOLD, 15));
errorMessage.setText(errM);
contentPanel.add(errorMessage);
}
{
JPanel buttonPane = new JPanel();
buttonPane.setLayout(new FlowLayout(FlowLayout.RIGHT));
getContentPane().add(buttonPane, BorderLayout.SOUTH);
{
JButton okButton = new JButton("OK");
okButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
}
});
okButton.setFont(new Font("Helvetica Neue", Font.BOLD, 15));
okButton.setForeground(new Color(85, 107, 47));
okButton.setActionCommand("OK");
buttonPane.add(okButton);
getRootPane().setDefaultButton(okButton);
}
}
}
}
Thanks for your answers