我正在尝试创建一个MessageBox Creator。
我在消息框打开时试图隐藏创建者 然后在消息框关闭时显示。我使用Eclipse Neon的以下插件:
帮我创建程序。
类似的一个在这里,但没有帮助:Click Me
源代码在这里:
package org.us.me.****.messagebox.creator;
import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JTextField;
import javax.swing.JProgressBar;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
public class MessageBoxCreator {
private JFrame frmD;
private JTextField txtMessageGoesHere;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
MessageBoxCreator window = new MessageBoxCreator();
window.frmD.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the application.
*/
public MessageBoxCreator() {
initialize();
}
/**
* Initialize the contents of the frame.
*/
private void initialize() {
frmD = new JFrame();
frmD.setTitle("MessageBox: Creator");
frmD.setBounds(100, 100, 260, 113);
frmD.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frmD.getContentPane().setLayout(null);
JTextField MessageBox = new JTextField();
MessageBox.setText("Message goes here...");
MessageBox.setBounds(10, 11, 222, 20);
frmD.getContentPane().add(MessageBox);
MessageBox.setColumns(10);
JButton btnGenerate = new JButton("Generate");
btnGenerate.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
JOptionPane.showMessageDialog(null, MessageBox);
}
});
btnGenerate.setBounds(10, 42, 86, 23);
frmD.getContentPane().add(btnGenerate);
}
}
请帮助。
答案 0 :(得分:0)
好像你试图隐藏按钮上的框架,然后在弹出的按钮上单击,你想让初始框架恢复可见。
最简单直接的方法是创建自己的弹出框。
试试这个:
class CustomPopup extends JFrame
{
public CustomPopup()
{
frmD.setVisible(false);
this.setName("Popup");
this.setLayout(new BorderLayout());
JLabel l = new JLabel("Enter Message here");
JButton b = new JButton("Submit");
b.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
frmD.setVisible(true);
CustomPopup.this.setVisible(false);
CustomPopup.this.dispose();
}
});
this.add(l,BorderLayout.CENTER);
this.add(b,BorderLayout.SOUTH);
this.setSize(300, 150);
this.setResizable(false);
this.setDefaultCloseOperation(0);
this.setVisible(true);
}
}
然后在你的按钮动作监听器中执行以下操作:
btnGenerate.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
new CustomPopup();
}
});
注意:以上示例与您的代码有关。我在示例中使用了Border layout for popup,因为它是最简单的实现。我相信您可以自己定制自定义弹出窗口的外观。还有更多选项可以使用自定义设置创建弹出窗口。
有关详细信息,请参阅此处:http://docs.oracle.com/javase/tutorial/uiswing/components/dialog.html
答案 1 :(得分:0)
我认为设计并不好,但要使框架隐藏并显示变化 这一行:
JOptionPane.showMessageDialog(null, MessageBox);
要
frmD.setVisible(false);
JOptionPane.showMessageDialog(null, MessageBox);
frmD.setVisible(true);