我试图让一个班级的 Jpanel 出现在另一个班级的 Jpanel 之上。我可以以迂回的方式做到这一点,但我知道从长远来看我会后悔的。我的框架类包含带有按钮的主 Jpanel,我想暂时在其顶部放置另一个带有按钮的 Jpanel。我试图简化我的代码。注意第一块代码下面的 ShopButton 方法和 Shop Class。这就是我想要关注的。我试图将 Shop JPanel 放在现有的“框架”(这就是我的班级)JPanel 上。
package sonomaroller;
//Bunch of imports i didnt include
import javax.swing.text.StyleConstants;
public class frame extends JPanel implements Runnable {
//buttons
private JButton Button4;
//images
private Image Sonoma;
private Image pic;
//booleans
private boolean loaded;
public boolean inTown = true;
public boolean isShopping = false;
//variables
//strings
//arrays
public Integer[] attributes = {0,1,2}; //attk - 0, def - 1 , magic - 2
//new jpanel
JTextPane field = new JTextPane();
JScrollPane sp = new JScrollPane(field);
//calling another class making it public
public Shop shopping = new Shop();
public void run(){
}
public frame(){
loadpics();
attackButton();
magicButton();
travelButton();
shopButton();
textField();
}
public void paint(Graphics g){
super.paint(g);
g.drawString(firstName +givenName+ secondName,225,40);
g.drawRect(100, 50, 350, 275);
if(loaded){
g.drawImage(Sonoma,101,51,350,275,null);
}
if(isShopping==true){
shopping.paint(g);
}
}
public void shopButton(){
//for shop button
Button4= new JButton("Shop");
Button4.addActionListener( new ActionListener() {
public void actionPerformed(ActionEvent Event) {
loaded=false;
isShopping=true;
repaint();
}
});
Button4.setBounds(130, 485, 90, 30);
add(Button4);
}
public void loadpics(){
Sonoma = new ImageIcon("C:\\Users\\Camtronius\\Documents\\NetBeansProjects\\SonomaRoller\\src\\sonomaroller\\Sonoma.png").getImage();
System.out.println("i was called?");
loaded = true;
repaint();
}
}
__ _ __ _ __ _ __ _ __ _ __ _ __ _ __ _ __ _ __ _ __ _ ___ _商店类
public class Shop extends JPanel {
public JButton CloseButton;
public Shop(){
CloseButton();
setVisible(true);
setLayout(null);
}
public void paint(Graphics g){
System.out.println("shop is working");
g.setColor(Color.WHITE);
g.fillRect(100, 50, 350, 275);
g.drawRect(100, 50, 350, 275);
}
public void CloseButton(){
//for
CloseButton= new JButton("Exit Shop");
CloseButton.addActionListener( new ActionListener() {
@Override
public void actionPerformed(ActionEvent Event) {
System.out.println("am i alive?");
}
});
CloseButton.setBounds(100, 100, 90, 30);
add(CloseButton);
}
}