关闭窗口(Java)

时间:2014-04-25 12:04:41

标签: java swing

Java:我有两个课程。第一个BaseForm(BaseForm.java)扩展JFrame,第二个ErrorM(ErrorM.java)扩展BaseForm。我想点击“确定”按钮关闭ErrorM

我知道我应该使用setVisible(false),但我不知道调用它的对象是什么。

课程:

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)); 
        }
    }

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);
                }
            }
        }
}

感谢您的回答

1 个答案:

答案 0 :(得分:0)

JOptionPane.showMessageDialog(null, "This is an error message!",
                             "This is the title!", JOptionPane.ERROR_MESSAGE);

来自内存,这是我用来显示错误消息的方法(非常简单明了)。

虽然如果你需要关闭对话框或框架,你可以在该对象上使用dispose()方法......

我希望这会有所帮助......