我正试图在NetBeans中使用Matisse来创建一个简单的游戏。。。比如Myst。我只想让它显示一个框架,它在右边有一个文本字段和按钮/清单。我能做到。然后我创建了一个Jpanel,显示在Frame上。我已经输入了一些图片,并创建了一套卡片,以便在需要时显示和不显示。每个卡片/场景都需要有针对每个场景不同的按钮。基本上,我想让用户按下透明的按钮来做事情(即移动到下一张图片/场景,找到一个键,等等)。我可以在每张图片中创建不同的按钮,但我似乎无法将它们移动到任何地方。我怀疑是因为马蒂斯,但我不知道。
以下是一些代码,我相信它很粗糙,因为这是我第一次用Java实现任何东西。在要显示的场景的最底部,我添加了一个按钮,它将显示。我现在几乎知道,由于位置的工作方式,setBounds不会工作。。。我该怎么把它放在什么地方呢。现在它只显示在图片的中上角。
场景框架:
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package main;
import scene.SecondPanel;
import scene.FirstPanel;
import scene.Scene;
import javax.swing.JButton;
public class SceneFrame extends javax.swing.JFrame {
private FirstPanel sceneP;
private SecondPanel sceneSP;
/**
* Creates new form SceneFrame
*/
public SceneFrame() {
initComponents();
addCards();
}
public void addCards() {
sceneP = new FirstPanel();
SceneManager.add("SecondPanel", sceneP);
sceneSP = new SecondPanel();
SceneManager.add("FirstPanel", sceneSP);
sceneP.setVisible(false);
}
/**
* This method is called from within the constructor to initialize the form.
* WARNING: Do NOT modify this code. The content of this method is always
* regenerated by the Form Editor.
*/
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
SceneManager = new scene.ScenePanel();
jButton1 = new javax.swing.JButton();
jButton2 = new javax.swing.JButton();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
javax.swing.GroupLayout SceneManagerLayout = new javax.swing.GroupLayout(SceneManager);
SceneManager.setLayout(SceneManagerLayout);
SceneManagerLayout.setHorizontalGroup(
SceneManagerLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGap(0, 517, Short.MAX_VALUE)
);
SceneManagerLayout.setVerticalGroup(
SceneManagerLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGap(0, 421, Short.MAX_VALUE)
);
jButton1.setText("jButton1");
jButton1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton1ActionPerformed(evt);
}
});
jButton2.setText("jButton2");
jButton2.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton2ActionPerformed(evt);
}
});
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addComponent(SceneManager, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jButton2, javax.swing.GroupLayout.Alignment.TRAILING)
.addComponent(jButton1, javax.swing.GroupLayout.Alignment.TRAILING))
.addContainerGap())
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(139, 139, 139)
.addComponent(jButton1)
.addGap(18, 18, 18)
.addComponent(jButton2)
.addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
.addGroup(layout.createSequentialGroup()
.addComponent(SceneManager, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(0, 97, Short.MAX_VALUE))
);
pack();
}// </editor-fold>
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
sceneP.setVisible(false);
//SceneManager.showScene("SecondPanel");
sceneSP.setVisible(true);
}
private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {
sceneSP.setVisible(false);
//SceneManager.showScene("FirstPanel");
sceneP.setVisible(true);
}
/**
* @param args the command line arguments
*/
public static void main(String args[]) {
/* Set the Nimbus look and feel */
//<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
/* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
* For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
*/
try {
for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
if ("Nimbus".equals(info.getName())) {
javax.swing.UIManager.setLookAndFeel(info.getClassName());
break;
}
}
} catch (ClassNotFoundException ex) {
java.util.logging.Logger.getLogger(SceneFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(SceneFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(SceneFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(SceneFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
}
//</editor-fold>
/* Create and display the form */
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new SceneFrame().setVisible(true);
}
});
}
// Variables declaration - do not modify
private javax.swing.JPanel SceneManager;
private javax.swing.JButton jButton1;
private javax.swing.JButton jButton2;
// End of variables declaration
}
场景:
package scene;
import java.awt.Color;
import javax.swing.JPanel;
import java.awt.Image;
import javax.swing.ImageIcon;
public abstract class Scene extends JPanel {
public Image image;
private String sceneName;
public Scene(String cn) {
setName(cn);
this.setSize(600, 600);
this.setBackground(Color.WHITE);
}
public String getCardName() {
return sceneName;
}
public abstract void addControlButtons();
// public abstract void implementControlButtons();
}
显示面板:
/**
* Creates a new ForestScene Object.
*/
public SecondPanel() {
super("SecondPanel");
JButton Button2 = new JButton("SwagCity");
try {
image = (new ImageIcon(getClass().getResource("/resources/ForestPath.jpg"))).getImage();
} catch (Exception e) {/*How to handle?*/
}
this.addControlButtons();
}
@Override
public void addControlButtons(){
JButton bButton = new JButton("bButton");
//bButton.setBounds(200, 300, 500, 600); //How to display where I want?
bButton.setAlignmentX(1000);
this.add(bButton);
}
public void paintComponent(Graphics g) {
super.paintComponent(g);
//Graphics2D g2 = (Graphics2D) g;
if (image != null) {
boolean val = g.drawImage(image, 0, 0, 800, 600, this);
} else {
System.out.println("Image not found");
}
}
}
您需要添加JFrame.frame.setLayout(null),以便在框架上调整字段(按钮、文本字段)。我也遇到了类似的问题
我发现一定是layoutManager混淆了什么。如果我设置:this.setLayout(null);
,我就可以设置按钮的边界,它会显示我想要的位置和方式。如果这个问题没有足够的信息,我很抱歉,但我比我告诉你的还不了解自己。如果有人对我的所作所为有任何补充,我很乐意选择这一点作为我的答案。