首页 文章

在JFrame中的多个JPanel之间切换

提问于
浏览
1

http://imgur.com/VT3JHH8这就是我想创建的基本上是为了开始我的基于菜单的游戏 . 我想知道我的方法是否良好 . 我创建了一个如下所示的框架类"SonomaRoller" . 在该类中,它添加"frame" my "panel1",如图所示 . 截至目前,我也在该类中绘制了我的图像,显示为"panel2" . 我希望用户能够使用panel1中的按钮在面板之间切换 . 面板2的一些面板也将具有它们自己的按钮 . 对此最好的方法是什么?我应该为面板创建单独的类并将它们添加到JFrame中吗?我应该使用JFrame在面板之间切换,因为它添加了第一个面板吗?我已将下面的代码包含在我的两个课程中 . 我还在面板下面有一个Jtext窗格 . 提前致谢

**** ___________________ 我的框架 ___________________

package sonomaroller;

import javax.swing.*;
import java.awt.*;
import static javax.swing.JFrame.*;

public class SonomaRoller extends JFrame {

    public static Dimension size = new Dimension(550,550); //Dimension of Frame
    public static String title = "Sonoma Roller v0.00" ;
    //Creates new object f to create the window

    //boolean
    public boolean addShop=false;

    public SonomaRoller(){

      setTitle(title);
        setSize(size);
        setResizable(false);
        setLocationRelativeTo(null); // null centers window on screen
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
        init(addShop);
    }
    public void init(boolean addShop){
       frame panel=new frame();

       panel.setLayout(null);
       add(panel);
       setVisible(true);

    }
   public static void main(String[] args) {

       SonomaRoller object1=new SonomaRoller();  

    }
}

**** ___________________ 我的面板带按钮 ___________________

package sonomaroller;
import javax.swing.*;
import java.awt.*; 
import java.awt.event.*;
import javax.swing.text.BadLocationException;
import javax.swing.text.DefaultCaret;

import javax.swing.text.StyledDocument;
import javax.swing.text.Style;
import javax.swing.text.StyleConstants;

public class frame extends JPanel implements Runnable {


    public void run(){

    }

    public frame(){

        loadpics();
        attackButton();
        magicButton();
        travelButton();
        shopButton();
        textField(); 

    }
    public void paintComponent(Graphics g){


    }

   }
    public void textField(){


}
    public void attackButton(){

    }
    public void magicButton(){

    }
    public void travelButton(){

    }
    public void shopButton(){

    }

     public void loadpics(){
        Sonoma = new ImageIcon("C:\\Users\\Camtronius\\Documents\\NetBeansProjects\\SonomaRoller\\src\\sonomaroller\\testImage.jpg").getImage();
        System.out.println("image loaded");  
        loaded = true;
        repaint();
    }

}

1 回答

  • 1

    您是否可以通过按下按钮使用ActionListener将面板设置为可见或不可见 . 例如:

    panel_1Button.addActionListener(new ActionListener(){
            public void actionPerformed(ActionEvent e){
                panel1.setVisible(false)
                panel2.setVisible(true);
            }
        });
    

相关问题