【问题标题】:How do I get my JButton to both switch the JPanel and close the JFrame?如何让我的 JButton 同时切换 JPanel 并关闭 JFrame?
【发布时间】:2013-09-16 08:57:35
【问题描述】:

我已经设法让类数据库中的 JButton buttonOne 来切换面板,但同时它正在打开一个新的 JFrame 来执行此操作。如何更改它,使其只更改 JFrame 面板而无需打开另一个 JFrame?我有一种感觉,如果我可以同时返回 JPanel 和 JFrame,我可以做到这一点,但我不知道该怎么做。感谢您的帮助:

第一类,JFrame 和 JPanel 在点击“buttonOne”时被切换:

import java.awt.BorderLayout;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

import javax.imageio.ImageIO;
import javax.swing.BorderFactory;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JDatabaseFarme;
import javax.swing.JLabel;

public class Database{

    //Running the GUI
    public static void main(String[] args) throws IOException {

        Database gui2 = new Database();
        gui2.mainPanel();
    }


     JDatabaseFarme mainPanel() throws IOException {

        // GridBagLayout/Constraint
        GridBagConstraints gbc = new GridBagConstraints();
        gbc.insets = new Insets(15,15,15,15);

        final JDatabaseFarme DatabaseFarme = new JDatabaseFarme("Lohn Jocke and the Quest for Qualia"); //Had to set DatabaseFarme to final for the action listener
        //JPanel panel = new JPanel(new GridBagLayout()); (??)

        final JComponent panel = new JLabel(new ImageIcon(ImageIO.read(new File("res/FinalBG.png")))); //Had to set panel to final for the action listener
        panel.setLayout(new GridBagLayout());

        ////// Creating JButtons/Icons for the buttons ////
        BufferedImage buttonIcon = ImageIO.read(new File("res/PlayGame.png"));
        JButton button = new JButton ("", new ImageIcon(buttonIcon));

        BufferedImage buttonIcon2 = ImageIO.read(new File("res/Scoreboard.png"));
        JButton buttonTwo = new JButton ("", new ImageIcon(buttonIcon2));

        BufferedImage buttonIcon3 = ImageIO.read(new File("res/SQLs.png"));
        JButton buttonThree = new JButton ("",new ImageIcon(buttonIcon3));

        // Scoreboard button ActionListener
        buttonThree.addActionListener(new ActionListener(){
                    @Override
                    public void actionPerformed(ActionEvent e){
                        //removed some code from here 
                        try {
                            panel.setVisible(false);
                            SQLsPanel a = new SQLsPanel(); 
                            JComponent SQLsPanel = a.SQLsPanel();
                            DatabaseFarme.add(SQLsPanel);
                            DatabaseFarme.getContentPane().add(BorderLayout.CENTER, SQLsPanel);
                            SQLsPanel.setVisible(true);

                        } catch (IOException e1) {
                            e1.printStackTrace();
                        }
                                }

                });

        ////// Creating/adding button rollover images /////

        BufferedImage buttonIcon1b = ImageIO.read(new File("res/PlayGameHigh.png"));
        button.setRolloverIcon(new ImageIcon(buttonIcon1b));

        BufferedImage buttonIcon2b = ImageIO.read(new File("res/ScoreboardHigh.png"));
        buttonTwo.setRolloverIcon(new ImageIcon(buttonIcon2b));

        BufferedImage buttonIcon3b = ImageIO.read(new File("res/SQLsHigh.png"));
        buttonThree.setRolloverIcon(new ImageIcon(buttonIcon3b));

        // Setting up GridBagConstraints for each JButton
        gbc.weightx=1;
        gbc.weighty=0;
        gbc.gridx=0;
        gbc.gridy=0;
        gbc.anchor = GridBagConstraints.CENTER;

        panel.add(button, gbc); //PLAY GAME

        gbc.weightx=1;
        gbc.weighty=0;
        gbc.anchor = GridBagConstraints.CENTER;
        gbc.gridx=0;
        gbc.gridy=1;

        panel.add(buttonTwo,gbc); //SCOREBOARD

        gbc.weightx=1;
        gbc.weighty=0;
        gbc.anchor = GridBagConstraints.CENTER;
        gbc.gridx=0;
        gbc.gridy=2;

        panel.add(buttonThree,gbc); //SQLS

        // JDatabaseFarme settings
        DatabaseFarme.add(panel);
        DatabaseFarme.getContentPane().add(BorderLayout.CENTER, panel);
        DatabaseFarme.setSize(860,500);
        DatabaseFarme.setLocationRelativeTo(null);
        DatabaseFarme.setDefaultCloseOperation(JDatabaseFarme.EXIT_ON_CLOSE);
        DatabaseFarme.setResizable(false);
        DatabaseFarme.setVisible(true);

        // JButton icon details 
        button.setBorder(BorderFactory.createEmptyBorder());
        button.setContentAreaFilled(false);


        buttonTwo.setBorder(BorderFactory.createEmptyBorder());
        buttonTwo.setContentAreaFilled(false);


        buttonThree.setBorder(BorderFactory.createEmptyBorder());
        buttonThree.setContentAreaFilled(false);

        return DatabaseFarme;
        }
}

我的第二个类,它包含需要更改面板并关闭 JDatabaseFarme 的 JButton:

import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

import javax.imageio.ImageIO;
import javax.swing.BorderFactory;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JDatabaseFarme;
import javax.swing.JLabel;

public class SQLsPanel {



 JComponent SQLsPanel() throws IOException {

        final JComponent SQLsPanel = new JLabel(new ImageIcon(ImageIO.read(new File("res/HowToPlayBG.png"))));  

        SQLsPanel.setLayout(new GridBagLayout());

        GridBagConstraints gbc = new GridBagConstraints();

        BufferedImage buttonOneIcon = ImageIO.read(new File("res/Database.png"));
        JButton buttonOne = new JButton("",new ImageIcon(buttonOneIcon));

        BufferedImage buttonOneIconB = ImageIO.read(new File("res/DatabaseHigh.png"));
        buttonOne.setRolloverIcon(new ImageIcon(buttonOneIconB));

        buttonOne.addActionListener(new ActionListener(){
            @Override
            public void actionPerformed(ActionEvent e){

                SQLsPanel.setVisible(false);
                try {

                    Database passme = new Database();
                    JDatabaseFarme DatabaseFarmeA = passme.mainPanel();
                    DatabaseFarmeA.add(SQLsPanel);

                } catch (IOException e1) {
                    e1.printStackTrace();
                }
                        }

        });

        gbc = new GridBagConstraints();
        gbc.insets = new Insets(15,15,15,15);
        gbc.weighty = 1;
        gbc.weightx = 1;
        gbc.gridx = 1;
        gbc.gridy = 3;
        gbc.anchor = GridBagConstraints.PAGE_END;
        SQLsPanel.add(buttonOne, gbc);

        buttonOne.setBorder(BorderFactory.createEmptyBorder());
        buttonOne.setContentAreaFilled(false);

        return SQLsPanel;   


    }
}

【问题讨论】:

    标签: java swing


    【解决方案1】:

    尝试以下方法:

        final JFrame frame = new JFrame("Lohn Jocke and the Quest for Qualia"); //Had to set frame to     final for the action listener
        final String name = frame.getName();
    

    在声明 JFrame 的下方添加变量“名称”。

          // JFrame settings
        frame.add(panel);
        frame.getContentPane().add(BorderLayout.CENTER, panel);
        frame.setSize(860,500);
        frame.setLocationRelativeTo(null);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setResizable(false);
        frame.setVisible(true);
    
    
        if(Frame.getFrames().length > 1){
            Frame[] f = Frame.getFrames();
                    for(Frame frames : f){
                if(!frames.getName().equals(name)){
                    frames.dispose();
                }
                }
        } 
    

    在你设置 JFrame 设置的地方添加这个。

    当你调用它时会发生什么

       MainMenu passme = new MainMenu();
    

    在 InstructionsPanel 中,除了您已经拥有的当前框架之外,您还从类构造函数创建了一个新的 JFrame(因此最终得到 2 个)。我试图做的是获取您正在创建的新框架的名称(使用 name 变量),然后使用底部的循环擦除所有其他框架,让您使用新框架来显示您的面板。( Frame.getFrames() 返回所有帧的列表,然后我遍历删除所有不需要的帧)

    如果您将来添加更多 JFrame,您可能需要对其进行调整,但希望这至少对您现在有所帮助。(我已尝试使用虚拟图像复制您的代码以解决此“修复”的问题' 如果我在这里误解了这个问题,请道歉)

    希望这会有所帮助。

    【讨论】:

      猜你喜欢
      • 2014-02-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-09-06
      • 1970-01-01
      • 1970-01-01
      • 2013-06-27
      相关资源
      最近更新 更多