Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/401.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/eclipse/8.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 打开新主机架时如何关闭主机架_Java_Eclipse_Swing - Fatal编程技术网

Java 打开新主机架时如何关闭主机架

Java 打开新主机架时如何关闭主机架,java,eclipse,swing,Java,Eclipse,Swing,我创建了两个框架我的主框架是Home,第二个是Selectie 在主页上有一个按钮可以打开框架选择器,但我想当我点击这个按钮时,主框架主页将消失,只有选择器才会显示。我在另一个包中制作的按钮代码,我不希望它与我的主(主)按钮在同一个类中 家庭代码: package View; import java.awt.BorderLayout; import java.awt.Color; import java.awt.Container; import java.awt.Dimension; imp

我创建了两个框架我的主框架是Home,第二个是Selectie 在主页上有一个按钮可以打开框架选择器,但我想当我点击这个按钮时,主框架主页将消失,只有选择器才会显示。我在另一个包中制作的按钮代码,我不希望它与我的主(主)按钮在同一个类中

家庭代码:

package View;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.Toolkit;
import java.awt.event.ActionListener;
import java.io.File;


import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JLabel;

import javax.swing.JPanel;

import Controller.HomeController;


import music.PlaySound;

public class Home extends JFrame {

    private JLabel label, label1, label2;
    private JPanel panel;
    private JButton logo, logo1, logo2, logo3, logo4, logo5, selectie;
    private Container window = getContentPane();
    private HomeController Controller;

    public Home (){
        initGUI();
        Controller = new HomeController();
    }
    public void addHomeListener(ActionListener a){
        selectie.addActionListener(a);
    }
    public void initGUI(){

        setLayout(null);
        setTitle("");
        setPreferredSize(new Dimension(800,600));
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        label = new JLabel();       
        label.setBounds(0, 0, 266, 800);
        label.setBackground(Color.WHITE);
        label.setOpaque(true);
        window.add(label);

        label1 = new JLabel();
        label1.setBounds(267, 0, 266, 800);
        label1.setBackground(Color.RED);
        label1.setOpaque(true);
        window.add(label1);

        label2 = new JLabel();
        label2.setBounds(533, 0, 266, 800);
        label2.setBackground(Color.WHITE);
        label2.setOpaque(true);
        window.add(label2);

        logo = new JButton(new ImageIcon("../Ajax/src/img/logotje.gif"));
        logo.setBorderPainted(false);
        logo.setBounds(40, 150, 188, 188);
        label1.add(logo);

        logo1 = new JButton(new ImageIcon("../Ajax/src/img/Ster.png"));
        logo1.setBorderPainted(false);
        logo1.setBounds(10, 50, 82, 82);
        label1.add(logo1);

        logo2 = new JButton(new ImageIcon("../Ajax/src/img/Ster.png"));
        logo2.setBorderPainted(false);
        logo2.setBounds(92, 20, 82, 82);
        label1.add(logo2);

        logo3 = new JButton(new ImageIcon("../Ajax/src/img/Ster.png"));
        logo3.setBorderPainted(false);
        logo3.setBounds(174, 50, 82, 82);
        label1.add(logo3);

        logo4 = new JButton(new ImageIcon("../Ajax/src/img/shirt.png"));
        logo4.setBorderPainted(false);
        logo4.setBounds(50, 50, 135, 182);
        label.add(logo4);

        logo5 = new JButton(new ImageIcon("../Ajax/src/img/uitshirt.png"));
        logo5.setBorderPainted(false);
        logo5.setBounds(65, 50, 138, 190);
        label2.add(logo5);

        selectie = new JButton("Selectie");
        selectie.setBounds(60, 500, 99, 25);
        selectie.setActionCommand("selectie");
        label.add(selectie);

        pack();

        addHomeListener(new HomeController());
    }

}
按钮中的代码:

package Controller;

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

import View.Home;
import View.Selectie;

public class HomeController implements ActionListener {

    public void actionPerformed (ActionEvent e){
            Selectie selectie = new Selectie();
            selectie.setVisible(true);
    }
}
我建议使用,最简单,非常舒适的(+1)你的代码张贴在这里

永远不要创建,重新创建一堆另一个
JFrames
,只有在你有非常重要的原因的情况下,然后使用
JDialog
JFrames

的父对象,我建议你使用最简单、最容易理解的方法来发布你的代码


永远不要创建,重新创建一堆另一个
JFrames
,只有在你有非常重要的原因的情况下,然后使用
JDialog
,与
JFrames

的父对象一起使用,请注意@kleopatra和@mKorbel必须说的话,他们非常正确地向你们指出了这一点,以使事情变得更容易

在这里,我在代码中添加了一些注释,请检查以下内容:

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.Toolkit;
import java.awt.event.ActionListener;
import java.io.File;

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


import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JLabel;

import javax.swing.JPanel;

//import Controller.HomeController;


//import music.PlaySound;

public class Home extends JFrame {

    private JLabel label, label1, label2;
    private JPanel panel;
    private JButton logo, logo1, logo2, logo3, logo4, logo5, selectie;
    private Container window = getContentPane();
    private HomeController Controller;

    public Home (){
        initGUI();
    }
    public void addHomeListener(ActionListener a){
        selectie.addActionListener(a);
    }
    public void initGUI(){

        setLayout(null);
        setTitle("");
        setPreferredSize(new Dimension(800,600));
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        label = new JLabel();       
        label.setBounds(0, 0, 266, 800);
        label.setBackground(Color.WHITE);
        label.setOpaque(true);
        window.add(label);

        label1 = new JLabel();
        label1.setBounds(267, 0, 266, 800);
        label1.setBackground(Color.RED);
        label1.setOpaque(true);
        window.add(label1);

        label2 = new JLabel();
        label2.setBounds(533, 0, 266, 800);
        label2.setBackground(Color.WHITE);
        label2.setOpaque(true);
        window.add(label2);

        logo = new JButton(new ImageIcon("../Ajax/src/img/logotje.gif"));
        logo.setBorderPainted(false);
        logo.setBounds(40, 150, 188, 188);
        label1.add(logo);

        logo1 = new JButton(new ImageIcon("../Ajax/src/img/Ster.png"));
        logo1.setBorderPainted(false);
        logo1.setBounds(10, 50, 82, 82);
        label1.add(logo1);

        logo2 = new JButton(new ImageIcon("../Ajax/src/img/Ster.png"));
        logo2.setBorderPainted(false);
        logo2.setBounds(92, 20, 82, 82);
        label1.add(logo2);

        logo3 = new JButton(new ImageIcon("../Ajax/src/img/Ster.png"));
        logo3.setBorderPainted(false);
        logo3.setBounds(174, 50, 82, 82);
        label1.add(logo3);

        logo4 = new JButton(new ImageIcon("../Ajax/src/img/shirt.png"));
        logo4.setBorderPainted(false);
        logo4.setBounds(50, 50, 135, 182);
        label.add(logo4);

        logo5 = new JButton(new ImageIcon("../Ajax/src/img/uitshirt.png"));
        logo5.setBorderPainted(false);
        logo5.setBounds(65, 50, 138, 190);
        label2.add(logo5);

        selectie = new JButton("Selectie");
        selectie.setBounds(60, 500, 99, 25);
        selectie.setActionCommand("selectie");
        label.add(selectie);

        pack();

        /*
         * You are making a new object again,
         * when you already had declared it as 
         * an instance Variable. So I used the 
         * one declared as instance variable..
         * To this we will send the object of Home
         * class, means the object of this class..
         * And as we know that object of the 
         * class we are in is by default known 
         * as this, so passing this to HomeController class.
         */
        Controller = new HomeController(this);
        addHomeListener(Controller);

        setVisible(true);
    }

    public static void main(String... args)
    {
        javax.swing.SwingUtilities.invokeLater(new Runnable()
        {
            public void run()
            {
                new Home();
            }
        });
    }
}

class HomeController implements ActionListener {

    /*
     * Here we declared a Home class's variable,
     * that we will use to dispose that JFrame.
     */
    private Home home;

    public HomeController(Home home)
    {
        this.home = home;
    }

    public void actionPerformed (ActionEvent e){

            home.dispose();
            Selectie selectie = new Selectie();
            selectie.setVisible(true);
    }
}

class Selectie extends JFrame
{
    public Selectie()
    {
        initGUI();
    }

    public void initGUI()
    {
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        setLocationByPlatform(true);

        setSize(300, 300);
    }
}

请务必注意@kleopatra和@mKorbel所说的话,他们非常正确地指出这一点,让事情变得更容易

在这里,我在代码中添加了一些注释,请检查以下内容:

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.Toolkit;
import java.awt.event.ActionListener;
import java.io.File;

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


import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JLabel;

import javax.swing.JPanel;

//import Controller.HomeController;


//import music.PlaySound;

public class Home extends JFrame {

    private JLabel label, label1, label2;
    private JPanel panel;
    private JButton logo, logo1, logo2, logo3, logo4, logo5, selectie;
    private Container window = getContentPane();
    private HomeController Controller;

    public Home (){
        initGUI();
    }
    public void addHomeListener(ActionListener a){
        selectie.addActionListener(a);
    }
    public void initGUI(){

        setLayout(null);
        setTitle("");
        setPreferredSize(new Dimension(800,600));
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        label = new JLabel();       
        label.setBounds(0, 0, 266, 800);
        label.setBackground(Color.WHITE);
        label.setOpaque(true);
        window.add(label);

        label1 = new JLabel();
        label1.setBounds(267, 0, 266, 800);
        label1.setBackground(Color.RED);
        label1.setOpaque(true);
        window.add(label1);

        label2 = new JLabel();
        label2.setBounds(533, 0, 266, 800);
        label2.setBackground(Color.WHITE);
        label2.setOpaque(true);
        window.add(label2);

        logo = new JButton(new ImageIcon("../Ajax/src/img/logotje.gif"));
        logo.setBorderPainted(false);
        logo.setBounds(40, 150, 188, 188);
        label1.add(logo);

        logo1 = new JButton(new ImageIcon("../Ajax/src/img/Ster.png"));
        logo1.setBorderPainted(false);
        logo1.setBounds(10, 50, 82, 82);
        label1.add(logo1);

        logo2 = new JButton(new ImageIcon("../Ajax/src/img/Ster.png"));
        logo2.setBorderPainted(false);
        logo2.setBounds(92, 20, 82, 82);
        label1.add(logo2);

        logo3 = new JButton(new ImageIcon("../Ajax/src/img/Ster.png"));
        logo3.setBorderPainted(false);
        logo3.setBounds(174, 50, 82, 82);
        label1.add(logo3);

        logo4 = new JButton(new ImageIcon("../Ajax/src/img/shirt.png"));
        logo4.setBorderPainted(false);
        logo4.setBounds(50, 50, 135, 182);
        label.add(logo4);

        logo5 = new JButton(new ImageIcon("../Ajax/src/img/uitshirt.png"));
        logo5.setBorderPainted(false);
        logo5.setBounds(65, 50, 138, 190);
        label2.add(logo5);

        selectie = new JButton("Selectie");
        selectie.setBounds(60, 500, 99, 25);
        selectie.setActionCommand("selectie");
        label.add(selectie);

        pack();

        /*
         * You are making a new object again,
         * when you already had declared it as 
         * an instance Variable. So I used the 
         * one declared as instance variable..
         * To this we will send the object of Home
         * class, means the object of this class..
         * And as we know that object of the 
         * class we are in is by default known 
         * as this, so passing this to HomeController class.
         */
        Controller = new HomeController(this);
        addHomeListener(Controller);

        setVisible(true);
    }

    public static void main(String... args)
    {
        javax.swing.SwingUtilities.invokeLater(new Runnable()
        {
            public void run()
            {
                new Home();
            }
        });
    }
}

class HomeController implements ActionListener {

    /*
     * Here we declared a Home class's variable,
     * that we will use to dispose that JFrame.
     */
    private Home home;

    public HomeController(Home home)
    {
        this.home = home;
    }

    public void actionPerformed (ActionEvent e){

            home.dispose();
            Selectie selectie = new Selectie();
            selectie.setVisible(true);
    }
}

class Selectie extends JFrame
{
    public Selectie()
    {
        initGUI();
    }

    public void initGUI()
    {
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        setLocationByPlatform(true);

        setSize(300, 300);
    }
}

可能重复:与您的问题无关:永远不要手动使用定位和大小组件,而是找到并使用适当的LayoutManager来实现您需要的布局和大小调整行为可能重复:与您的问题无关:永远不要手动使用定位和大小组件,相反,找到并使用适当的LayoutManager来实现所需的布局和大小调整行为+1,但我认为如果存在
setSize()
简单的容器布局,则
pack()
可能没有用twice@mKorbel:不,实际上OP没有提供Selectie类,所以我制作了一个虚构的Selectie类,因此,为了在屏幕上显示它,我正在使用
setSize()
pack()
仍然在Home类中。+1但我认为如果存在
setSize()
简单的容器布局,那么
pack()
可能是无用的twice@mKorbel:不,实际上OP没有提供Selectie类,所以我制作了一个虚构的Selectie类,因此为了在屏幕上显示它,我使用了
setSize()
,而
pack()
仍然在Home类中。