构造对话框的问题
来源: 互联网 发布时间:2017-04-25
本文导语: import javax.swing.*; import java.awt.*; import java.awt.event.*; class DialogTest { public static void main(String[] args) { DialogFrame frame = new DialogFrame(); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.show(); } } class DialogFrame ext...
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
class DialogTest
{
public static void main(String[] args)
{
DialogFrame frame = new DialogFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.show();
}
}
class DialogFrame extends JFrame
{
public DialogFrame()
{
setSize(300, 200);
setTitle("About");
DialogPanel panel = new DialogPanel();
Container contentpane = this.getContentPane();
contentpane.add(panel);
}
}
class DialogPanel extends JPanel
{
public DialogPanel()
{
dialog = new GameOverDialog(DialogFrame.this);//
dialog.show();
}
GameOverDialog dialog;
private class GameOverDialog extends JDialog
{
public GameOverDialog(JFrame owner)
{
super(owner, "GameOverDialog", true);
Container contentpane = getContentPane();
contentpane.add(new Label(
"hello"));
JButton button_ok = new JButton("OK");
button_ok.addActionListener(new
ActionListener()
{
public void actionPerformed(ActionEvent event)
{}
});
JButton button_cancel = new JButton("CANCEL");
button_cancel.addActionListener(new
ActionListener()
{
public void actionPerformed(ActionEvent event)
{
System.exit(0);
}
});
contentpane.add(button_ok,BorderLayout.CENTER);
contentpane.add(button_cancel, BorderLayout.SOUTH);
setSize(120, 100);
}
}
}
我在JPanel中构造一个对话框实例,编译错误:不是一个闭合类:DialogFrame
怎么解决;在DialogFrame中构造对话框实例没有问题
import java.awt.*;
import java.awt.event.*;
class DialogTest
{
public static void main(String[] args)
{
DialogFrame frame = new DialogFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.show();
}
}
class DialogFrame extends JFrame
{
public DialogFrame()
{
setSize(300, 200);
setTitle("About");
DialogPanel panel = new DialogPanel();
Container contentpane = this.getContentPane();
contentpane.add(panel);
}
}
class DialogPanel extends JPanel
{
public DialogPanel()
{
dialog = new GameOverDialog(DialogFrame.this);//
dialog.show();
}
GameOverDialog dialog;
private class GameOverDialog extends JDialog
{
public GameOverDialog(JFrame owner)
{
super(owner, "GameOverDialog", true);
Container contentpane = getContentPane();
contentpane.add(new Label(
"hello"));
JButton button_ok = new JButton("OK");
button_ok.addActionListener(new
ActionListener()
{
public void actionPerformed(ActionEvent event)
{}
});
JButton button_cancel = new JButton("CANCEL");
button_cancel.addActionListener(new
ActionListener()
{
public void actionPerformed(ActionEvent event)
{
System.exit(0);
}
});
contentpane.add(button_ok,BorderLayout.CENTER);
contentpane.add(button_cancel, BorderLayout.SOUTH);
setSize(120, 100);
}
}
}
我在JPanel中构造一个对话框实例,编译错误:不是一个闭合类:DialogFrame
怎么解决;在DialogFrame中构造对话框实例没有问题
|
dialog = new GameOverDialog(DialogFrame.this);
//因为类GameOverDialog 不是类 DialogFrame 的inner class,所以你不可以这样引用outer class DialogFrame 的reference
solution:
class DialogFrame extends JFrame{
...
class DialogPanel extends JPanel{
...
}
}
//因为类GameOverDialog 不是类 DialogFrame 的inner class,所以你不可以这样引用outer class DialogFrame 的reference
solution:
class DialogFrame extends JFrame{
...
class DialogPanel extends JPanel{
...
}
}
|
我有个实例你看一看:
public class MessageDialog extends JDialog implements ActionListener {
public MessageDialog(Frame parent,String title,String message) {
super(parent,title,true);
if(parent!=null){
Dimension parentSize=parent.getSize();
Point p=parent.getLocation();
setLocation(p.x+parentSize.width/4,p.y+parentSize.height/4);
}
JPanel messagePane=new JPanel();
messagePane.add(new JLabel(message));
getContentPane().add(messagePane);
JPanel buttonPane=new JPanel();
JButton button=new JButton("OK");
buttonPane.add(button);
button.addActionListener(this);
getContentPane().add(buttonPane,BorderLayout.SOUTH);
setDefaultCloseOperation(DISPOSE_ON_CLOSE);
pack();
setVisible(true);
}
public void actionPerformed(ActionEvent e){
setVisible(false);
dispose();
}
}
public class MessageDialog extends JDialog implements ActionListener {
public MessageDialog(Frame parent,String title,String message) {
super(parent,title,true);
if(parent!=null){
Dimension parentSize=parent.getSize();
Point p=parent.getLocation();
setLocation(p.x+parentSize.width/4,p.y+parentSize.height/4);
}
JPanel messagePane=new JPanel();
messagePane.add(new JLabel(message));
getContentPane().add(messagePane);
JPanel buttonPane=new JPanel();
JButton button=new JButton("OK");
buttonPane.add(button);
button.addActionListener(this);
getContentPane().add(buttonPane,BorderLayout.SOUTH);
setDefaultCloseOperation(DISPOSE_ON_CLOSE);
pack();
setVisible(true);
}
public void actionPerformed(ActionEvent e){
setVisible(false);
dispose();
}
}
本站(WWW.)旨在分享和传播互联网科技相关的资讯和技术,将尽最大努力为读者提供更好的信息聚合和浏览方式。
本站(WWW.)站内文章除注明原创外,均为转载、整理或搜集自网络。欢迎任何形式的转载,转载请注明出处。
本站(WWW.)站内文章除注明原创外,均为转载、整理或搜集自网络。欢迎任何形式的转载,转载请注明出处。