Java 如何关闭程序中间的JFrFrm
如何关闭此框架,以便我的JOptionPane消息对话框可以在程序中继续,而无需完全退出程序 编辑:我尝试了.dispose(),但得到以下结果:Java 如何关闭程序中间的JFrFrm,java,dispose,jframe,joptionpane,Java,Dispose,Jframe,Joptionpane,如何关闭此框架,以便我的JOptionPane消息对话框可以在程序中继续,而无需完全退出程序 编辑:我尝试了.dispose(),但得到以下结果: public class JFrameWithPanel extends JFrame implements ActionListener, ItemListener { int packageIndex; double price; double[] prices = {49.99, 39.99, 34.99, 99.99}
public class JFrameWithPanel extends JFrame implements ActionListener, ItemListener
{
int packageIndex;
double price;
double[] prices = {49.99, 39.99, 34.99, 99.99};
DecimalFormat money = new DecimalFormat("$0.00");
JLabel priceLabel = new JLabel("Total Price: "+price);
JButton button = new JButton("Check Price");
JComboBox packageChoice = new JComboBox();
JPanel pane = new JPanel();
TextField text = new TextField(5);
JButton accept = new JButton("Accept");
JButton decline = new JButton("Decline");
JCheckBox serviceTerms = new JCheckBox("I Agree to the Terms of Service.", false);
JTextArea termsOfService = new JTextArea("This is a text area", 5, 10);
public JFrameWithPanel()
{
super("JFrame with Panel");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
pane.add(packageChoice);
setContentPane(pane);
setSize(250,250);
setVisible(true);
packageChoice.addItem("A+ Certification");
packageChoice.addItem("Network+ Certification ");
packageChoice.addItem("Security+ Certifictation");
packageChoice.addItem("CIT Full Test Package");
pane.add(button);
button.addActionListener(this);
pane.add(text);
text.setEditable(false);
text.setBackground(Color.WHITE);
text.addActionListener(this);
pane.add(termsOfService);
termsOfService.setEditable(false);
termsOfService.setBackground(Color.lightGray);
pane.add(serviceTerms);
serviceTerms.addItemListener(this);
pane.add(accept);
accept.addActionListener(this);
pane.add(decline);
decline.addActionListener(this);
}
public void actionPerformed(ActionEvent e)
{
packageIndex = packageChoice.getSelectedIndex();
price = prices[packageIndex];
text.setText("$"+price);
Object source = e.getSource();
if(source == accept)
{
if(serviceTerms.isSelected() == false)
{
JOptionPane.showMessageDialog(null,"Please accept the terms of service.", "Terms of Service", JOptionPane.ERROR_MESSAGE);
}
else
{
JOptionPane.showMessageDialog(null,"Thank you. We will now move on to registering your product.");
pane.dispose();
}
}
else if(source == decline)
{
System.exit(0);
}
}
public void itemStateChanged(ItemEvent e)
{
int select = e.getStateChange();
}
public static void main(String[] args)
{
String value1;
int constant = 1, invalidNum = 0, answerParse, packNum, packPrice;
JOptionPane.showMessageDialog(null,"Hello!"+"\nWelcome to the CIT Test Program.");
JOptionPane.showMessageDialog(null,"IT WORKS!");
}
}//class
请尝试:this.dispose()
JPanel
没有该方法,但是JFrame
有
编辑
在你的主体中,你不是在呼唤你的框架:
cannot find symbol
symbol : method dispose()
location: class javax.swing.JPanel
pane.dispose();
^
尝试添加它并查看差异:
public static void main(String[] args) {
String value1;
int constant = 1, invalidNum = 0, answerParse, packNum, packPrice;
JOptionPane.showMessageDialog(null,"Hello!"+"\nWelcome to the CIT Test Program.");
JOptionPane.showMessageDialog(null,"IT WORKS!");
}
结果:
接
编辑2
试试下面的代码,它会显示一个框架,当你点击“关闭”按钮时,它会显示一个对话框,这就是你想要的吗
if(serviceTerms.isSelected() == false) {
JOptionPane.showMessageDialog(null,"Please accept the terms of service.", "Terms of Service", JOptionPane.ERROR_MESSAGE);
} else {
this.dispose();
JOptionPane.showMessageDialog(null,"Thank you. We will now move on to registering your product.");
}
我知道这可能是一个愚蠢的答案,但有时最明显的事情是造成问题。我没有看到您在代码中导入javax.swing。。。你那样做了吗?那完全不同。在main方法中,您甚至没有调用
JFrameWithPanel
尝试添加:newjFrameWithPanel()
在main
方法的最后一行中,查看差异是的,这正是所做的,请参阅我的更新。单击“接受”显示“谢谢”对话框。前两个?欢迎来到CIT,它能工作吗?可能你刚刚删除了它们。我希望程序在帧被释放后继续显示JOptionPane,我该怎么做,这是我的主要问题,让帧关闭,程序继续打开JOptionPanes。嗯?是否要先处理框架,然后显示对话框?还是要显示对话框,然后显示框架并保持对话框可见?我不明白你想做什么。看看“接受”逻辑中的“f/else”部分。首先显示对话框,然后处理框架,可能需要另一种方式(参见上面的答案),这可能应该是一个注释
if(serviceTerms.isSelected() == false) {
JOptionPane.showMessageDialog(null,"Please accept the terms of service.", "Terms of Service", JOptionPane.ERROR_MESSAGE);
} else {
this.dispose();
JOptionPane.showMessageDialog(null,"Thank you. We will now move on to registering your product.");
}
import javax.swing.*;
import java.awt.event.*;
class FrameDemo {
public static void main( String [] args ) {
final JFrame frame = new JFrame("Main frame");
frame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
frame.add( new JPanel(){{
add( new JLabel("This is the main content"));
add( new JButton("Close"){{
addActionListener( new ActionListener(){
public void actionPerformed(ActionEvent e ) {
frame.dispose();
JOptionPane.showMessageDialog(frame,"IT WORKS!");
}
});
}});
}});
frame.pack();
frame.setLocationRelativeTo( null );
frame.setVisible( true );
}
}