繁体   English   中英

JFrame:如何在单击按钮时隐藏主窗口?

[英]JFrame: How to hide main window when button is clicked?

我有一个简单的代码,首先执行的是带有按钮的框架,如果单击该按钮,则将显示一个消息对话框,当按下按钮时,如何将主框架的可见性设置为false,然后将其设置回当用户在消息对话框中单击“确定”时,可见性为true

这是代码:

package something;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;  //notice javax

public class Something extends JFrame implements ActionListener {

    JLabel answer = new JLabel("");
    JPanel pane = new JPanel();
    JButton somethingButton = new JButton("Something");

    Something() {
        super("Something");
        setBounds(100, 100, 300, 100);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        Container con = this.getContentPane(); // inherit main frame
        con.add(pane); // add the panel to frame
        pane.add(somethingButton);
        somethingButton.requestFocus();
        somethingButton.addActionListener(this);
        setVisible(true); // display this frame
    }

    @Override
    public void actionPerformed(ActionEvent event) {
        Object source = event.getSource();
        if (source == somethingButton) {
            answer.setText("Button pressed!");
            JOptionPane.showMessageDialog(null, "Something", "Message Dialog",
                    JOptionPane.PLAIN_MESSAGE);
            setVisible(true);  // show something
        }
    }

    public static void main(String args[]) {
        Something something = new Something();
    }
}
@Override
public void actionPerformed(ActionEvent event) {
    Object source = event.getSource();
    if (source == somethingButton) {
        answer.setText("Button pressed!");
        setVisible(false);  // hide something            
        JOptionPane.showMessageDialog(this, "Something", "Message Dialog",JOptionPane.PLAIN_MESSAGE);
        setVisible(true);  // show something 
    }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM