有没有人知道如何在java中打开一个新的JPanel GUI按钮?它不在谷歌上。它显示一个关于面板。谢谢你的帮助!
答案 0 :(得分:6)
我猜你需要JDialog 。
详情请见:How to Make Dialogs
以下是一个示例:
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
public class CreateDialogFromOptionPane {
public static void main(final String[] args) {
JFrame parent = new JFrame();
JButton button = new JButton();
button.setText("Click me to show dialog!");
parent.add(button);
parent.pack();
parent.setVisible(true);
button.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
JOptionPane optionPane = new JOptionPane("Is this what you need?", JOptionPane.QUESTION_MESSAGE,JOptionPane.YES_NO_OPTION);
JDialog dialog = optionPane.createDialog("Dialog");
dialog.setVisible(true);
}
});
}
}
答案 1 :(得分:2)
我认为通过实现CardLayout你可以解决这个问题
答案 2 :(得分:2)
JOptionPane.showMessageDialog(frame, "This is my message");
会创建一条警告信息,您也可以创建自己的服装对话框,我建议您阅读this
答案 3 :(得分:1)
您可以使用未修饰的JDialog显示面板
public static void main(String args[])
{
final JDialog bwin = new JDialog();
bwin.addWindowFocusListener(new WindowFocusListener()
{
@Override
public void windowLostFocus(WindowEvent e)
{
bwin.setVisible(false);
bwin.dispose();
}
@Override
public void windowGainedFocus(WindowEvent e)
{
}
});
bwin.setUndecorated(true);
JLabel label = new JLabel("About");
JPanel panel = new JPanel();
panel.setLayout(new BorderLayout());
panel.add(label);
panel.setPreferredSize(new Dimension(200,200));
bwin.add(panel);
bwin.pack();
bwin.setVisible(true);
}