protected ActionListener createNewFileActionListener() {
//加触发程序
return new ActionListener() {
public void actionPerformed(ActionEvent e) {
javax.swing.JPanel contentpanel= new javax.swing.JPanel();
MainFrame.this.setContentPane(contentpanel);
}
};
}这是一个"新建"菜单的事件监听,目的是做一个新panel.现在点击它,不出现新建,反而像死机一样.Debug总是显示下面这句有问题
javax.swing.JPanel contentpanel= new javax.swing.JPanel();
请教这是什么问题呢?
//加触发程序
return new ActionListener() {
public void actionPerformed(ActionEvent e) {
javax.swing.JPanel contentpanel= new javax.swing.JPanel();
MainFrame.this.setContentPane(contentpanel);
}
};
}这是一个"新建"菜单的事件监听,目的是做一个新panel.现在点击它,不出现新建,反而像死机一样.Debug总是显示下面这句有问题
javax.swing.JPanel contentpanel= new javax.swing.JPanel();
请教这是什么问题呢?
import javax.swing.*;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
public class ContentPane extends JFrame{
private JPanel pane1;
private JButton clickButton;
public ContentPane (){
jbInit();
} public void jbInit(){
pane1=new JPanel();
clickButton=new JButton("click");
pane1.add(clickButton);
this.getContentPane().add(pane1);
clickButton.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
JPanel pane2=new JPanel();
pane2.add(new JButton("hello"));
ContentPane.this.setContentPane(pane2);
}
});
} public static void main(String[] args){
ContentPane c=new ContentPane(); c.setLocation(200,200);
c.setSize(200,200);
c.show();
}
}
这是一个button事件的例子,可以运行,不过有点慢。
没有报错
public class GraghView extends JPanel
{
public static GraghView getInstance()
{
if(instance == null)
{
instance = new GraghView();
}
return instance;
}
GraghView在一个面板上,
在另一个类 MainFrame.this.buildContentPane();//初始化该面板,不能显示GraghView里的内容.现在加了 GraghView.getInstance();仍然显示不了,请教高手了
能否再指点一下?
import testpane.GraghView;import javax.swing.*;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
public class ContentPane extends JFrame{
private JPanel pane1;
private JButton clickButton;
public ContentPane (){
jbInit();
} public void jbInit(){
pane1=new JPanel();
clickButton=new JButton("click");
pane1.add(clickButton);
this.getContentPane().add(pane1);
clickButton.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
JPanel pane2=GraghView.getInstatnce();
ContentPane.this.setContentPane(pane2);
ContentPane.this.validate();//这是新加上的一行,可能更新之后要激活窗体。
}
});
} public static void main(String[] args){
ContentPane c=new ContentPane(); c.setLocation(200,200);
c.setSize(200,200);
c.show();
}
}
package testpane;
import javax.swing.*;public class GraghView extends JPanel{
private static GraghView instance=null;
private JButton againButton;
public GraghView() {
panelInit();
}
public void panelInit(){
againButton=new JButton("show the button");
this.add(againButton);
}
public static GraghView getInstatnce(){
if(instance==null){
instance=new GraghView();
}
return instance;
}
}不知道你所说的是不是这样的?