Swing 在JFrame内的多个JPanel之间切换
这就是我试图创建的,基本上是为了开始我的基于菜单的游戏。我想知道我的方法是否好。我创建了一个框架类“SonomaRoller”,如下所示。在该类中,它添加了“frame”我的“panel1”,如图所示。到目前为止,我也有我在该类中绘制的图像,显示为“panel2”。我希望用户能够使用panel1中的按钮在面板之间切换。面板2所在的一些面板也有自己的按钮。最好的方法是什么?我应该为面板创建单独的类并将它们添加到JFrame中吗?我是否应该使用JFrame在面板之间切换,因为它添加了第一个面板?我已经在下面为我的两个类添加了代码。我还有一个Jtext窗格,位于面板的下方。 提前谢谢 我的框架我的框架Swing 在JFrame内的多个JPanel之间切换,swing,jframe,jpanel,paintcomponent,Swing,Jframe,Jpanel,Paintcomponent,这就是我试图创建的,基本上是为了开始我的基于菜单的游戏。我想知道我的方法是否好。我创建了一个框架类“SonomaRoller”,如下所示。在该类中,它添加了“frame”我的“panel1”,如图所示。到目前为止,我也有我在该类中绘制的图像,显示为“panel2”。我希望用户能够使用panel1中的按钮在面板之间切换。面板2所在的一些面板也有自己的按钮。最好的方法是什么?我应该为面板创建单独的类并将它们添加到JFrame中吗?我是否应该使用JFrame在面板之间切换,因为它添加了第一个面板?我已
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();
}
}
\uuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuu我的面板带按钮
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();
}
}
不能使用ActionListener通过按按钮将面板设置为可见或不可见。例如:
panel_1Button.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
panel1.setVisible(false)
panel2.setVisible(true);
}
});
不能使用ActionListener通过按按钮将面板设置为可见或不可见。例如:
panel_1Button.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
panel1.setVisible(false)
panel2.setVisible(true);
}
});
在继续之前,先停下来,不要使用,读第一段,关于使用这种方法的陷阱。考虑一下这样的任务。一个相关:-)此外,您在项目中处理图像的方式值得怀疑。如果您将此项目交给您的朋友进行测试,那么此人必须首先在您的
loadpics()
方法中写入的相同位置提供图像,如果此人没有Drive C
,该怎么办?请看一下,查看NetBeans
标题,了解与此相关的信息:-)在继续之前,请立即停止,不要使用,请阅读第一段,其中与使用这种方法的陷阱相关。考虑一下这样的任务。一个相关:-)此外,您在项目中处理图像的方式值得怀疑。如果您将此项目交给您的朋友进行测试,那么此人必须首先在您的loadpics()
方法中写入的相同位置提供图像,如果此人没有Drive C
,该怎么办?请看一下,查看NetBeans
标题,了解与此内容相关的信息:-)