【问题标题】:Jframe something went bad[i try to open window1 but open window2]Jframe 出了问题[我尝试打开窗口但打开了窗口 2]
【发布时间】:2013-12-14 13:02:58
【问题描述】:

我不知道为什么我的代码不能正常工作。当我试图打开 window(BazaDanych) 时,只出现 window2(ZmienBaze),我不知道为什么。窗口(BazaDanych)根本没有打开。也许你能帮帮我。

class GlowneMenu extends JFrame implements ActionListener  
{  

  public GlowneMenu()  
  {  
    setTitle("Menu");  
    setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);  
    ...<not important just Menu stuff>
    JMenu info = new JMenu("Info");
    ...<not important just JMenu and label stuff>

  }  

  public void actionPerformed(ActionEvent e){  

    new BazaDanych().setVisible(true);  
    this.dispose(); 

    new ZmienBaza().setVisible(true);  
    this.dispose();

  }  

  public static void main( String[] args){new GlowneMenu().setVisible(true);}  
}  
class BazaDanych extends JFrame{ 

  public BazaDanych()  
  {  
    setTitle("Baza danych");  
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);  
    setLocation(400,100);  
    ...<not importent just this window stuff>
  }  

} 
class ZmienBaza extends JFrame{ 

      public ZmienBaza()  
      {  
        setTitle("Zmiana Bazy");  
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);  
        setLocation(400,100);  
        ...<not importent just this window stuff>

      }  
}

我发现当我删除这个 :* 我的代码将正常工作,但只适用于一个窗口。如何通过这些方式添加更多窗口?

 *new ZmienBaza().setVisible(true);
            this.dispose();

【问题讨论】:

标签: java swing jframe


【解决方案1】:

以下代码应该可以完成这项工作:

import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;

public class GlowneMenu extends JFrame{  

    public GlowneMenu(){  
        JButton button = new JButton("Show");
        button.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                actionMethod();
            }
        });
        setTitle("Menu");  
        setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        getContentPane().add(button);
        setSize(new Dimension(300,200));
        setPreferredSize(new Dimension(300,200));
    }  


    protected void actionMethod(){
        new BazaDanych().setVisible(true);
        //this.dispose();
        new ZmienBaza().setVisible(true);
        this.dispose();
    }  

    public static void main( String[] args){
        new GlowneMenu().setVisible(true);
    }

    class BazaDanych extends JFrame{
        public BazaDanych(){  
            setTitle("Baza danych");  
            setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);  
            setLocation(400,100);  
            setSize(new Dimension(300,200));
            setPreferredSize(new Dimension(300,200));
        }  

    } 
     class ZmienBaza extends JFrame{
        public ZmienBaza(){  
            setTitle("Zmiana Bazy");  
            setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);  
            setLocation(100,400);  
            setSize(new Dimension(300,200));
        }  
    }

}  

【讨论】:

  • 好的,谢谢。嗯,我宁愿认为我有 2 个窗口和 2 个路径,当我单击窗口路径时出现 window2(不是 window1),当我单击窗口路径时出现 window2。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-05-18
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多