我想开发一款迷你游戏,但我很难为这个应用程序创建菜单。菜单由 3 个按钮(退出、规则、播放)组成,我想让 netbeans JFrame 表单设计器来处理它(你知道,我只是放置按钮,分配它们的功能和样式,然后 IDE 创建类代码为我)。 现在的问题是我不知道如何实现这段代码。它说“从构造函数内部调用此方法来初始化表单。”,所以我猜我只需要这样做:
public Menu() {
create_and_show_menu();
}
private void create_and_show_menu() {
JFrame f = new JFrame("Menu");
new Sterge();
f.setExtendedState(JFrame.MAXIMIZED_BOTH);
f.setLayout(null);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setLocationRelativeTo(null);
f.setVisible(true);
}
...但没有成功。请告诉我如何使用那个怪异的 JFrame 表单,因为我在网上没有找到任何相关内容。
自动生成的类代码:
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
/**
*
* @author stoic
*/
public class Sterge extends javax.swing.JFrame {
/**
* Creates new form Sterge
*/
public Sterge() {
initComponents();
}
/**
* 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() {
jButton1 = new javax.swing.JButton();
jButton2 = new javax.swing.JButton();
jButton3 = new javax.swing.JButton();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
jButton1.setText("jButton1");
jButton1.addMouseListener(new java.awt.event.MouseAdapter() {
public void mousePressed(java.awt.event.MouseEvent evt) {
jButton1MousePressed(evt);
}
});
jButton2.setText("jButton1");
jButton2.addMouseListener(new java.awt.event.MouseAdapter() {
public void mousePressed(java.awt.event.MouseEvent evt) {
jButton2MousePressed(evt);
}
});
jButton3.setText("jButton1");
jButton3.addMouseListener(new java.awt.event.MouseAdapter() {
public void mousePressed(java.awt.event.MouseEvent evt) {
jButton3MousePressed(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()
.addGap(138, 138, 138)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jButton1)
.addComponent(jButton2)
.addComponent(jButton3))
.addContainerGap(189, Short.MAX_VALUE))
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(75, 75, 75)
.addComponent(jButton1)
.addGap(42, 42, 42)
.addComponent(jButton2)
.addGap(46, 46, 46)
.addComponent(jButton3)
.addContainerGap(73, Short.MAX_VALUE))
);
pack();
}// </editor-fold>
private void jButton1MousePressed(java.awt.event.MouseEvent evt) {
// TODO add your handling code here:
}
private void jButton2MousePressed(java.awt.event.MouseEvent evt) {
// TODO add your handling code here:
}
private void jButton3MousePressed(java.awt.event.MouseEvent evt) {
// TODO add your handling code here:
}
/**
* @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(Sterge.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(Sterge.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(Sterge.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(Sterge.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 Sterge().setVisible(true);
}
});
}
// Variables declaration - do not modify
private javax.swing.JButton jButton1;
private javax.swing.JButton jButton2;
private javax.swing.JButton jButton3;
// End of variables declaration
}
最佳答案
initcomponents()
方法使用 NetBeans GUI Builder 初始化前端 GUI 使用的所有 Java swing 组件对象。我看到您已经在使用 NetBeans GUI Builder,并且 IDE 在您设计时生成了代码,
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
jButton1 = new javax.swing.JButton();
jButton2 = new javax.swing.JButton();
jButton3 = new javax.swing.JButton();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
jButton1.setText("jButton1");
jButton1.addMouseListener(new java.awt.event.MouseAdapter() {
public void mousePressed(java.awt.event.MouseEvent evt) {
jButton1MousePressed(evt);
}
});
jButton2.setText("jButton1");
jButton2.addMouseListener(new java.awt.event.MouseAdapter() {
public void mousePressed(java.awt.event.MouseEvent evt) {
jButton2MousePressed(evt);
}
});
jButton3.setText("jButton1");
jButton3.addMouseListener(new java.awt.event.MouseAdapter() {
public void mousePressed(java.awt.event.MouseEvent evt) {
jButton3MousePressed(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()
.addGap(138, 138, 138)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jButton1)
.addComponent(jButton2)
.addComponent(jButton3))
.addContainerGap(189, Short.MAX_VALUE))
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(75, 75, 75)
.addComponent(jButton1)
.addGap(42, 42, 42)
.addComponent(jButton2)
.addGap(46, 46, 46)
.addComponent(jButton3)
.addContainerGap(73, Short.MAX_VALUE))
);
pack();
}// </editor-fold>
这就是 NetBeans IDE 帮助您开发应用程序的方式,无需键入更多代码,只需进行设计即可。很简单吧:)
每当您使用 GUI 构建器更改 GUI 的设计时,这些 swing 组件都会在上述方法中自动生成。
您不应更改此方法中上述代码的任何方面,因为此方法与前端 NetBeans GUI 构建器密不可分。如果您更改代码,您的前端也会更改。但如果您想更改代码,则由您自行处理。
默认情况下,NetBeans 在构造函数内调用 initComponents();
方法。默认情况下它是私有(private)
。在构造函数
内运行此initComponents();
方法将在应用程序启动后立即显示前端。
public Sterge() {
initComponents();
}
但是您可以在任意位置(构造函数或其他方法内部)调用 initComponents();
方法。对于 Java,它就像任何其他方法一样。
当我回答你的菜单问题时,我没有看到任何 JMenuBar和 JMenuItems被使用。如果您需要它们,您可以从 here 阅读有关如何使用菜单的信息。 。但我看到有 3 JButtons正如你所说,菜单中有三个按钮。所以我假设这就是您所说的 3 个按钮。
查看您类(class)中的以下代码。
private void jButton1MousePressed(java.awt.event.MouseEvent evt) {
// TODO add your handling code here:
}
private void jButton2MousePressed(java.awt.event.MouseEvent evt) {
// TODO add your handling code here:
}
private void jButton3MousePressed(java.awt.event.MouseEvent evt) {
// TODO add your handling code here:
}
假设您需要在单击jButton1
后在控制台中打印Hello World!
。这是您编写代码的地方。在以下方法中。
private void jButton1MousePressed(java.awt.event.MouseEvent evt) {
System.out.println("Hello World!");
}
一旦您点击上面的按钮,MouseEvent
将被触发,您将在控制台上看到 Hello World!
文本。根据您的详细信息,有 3 个按钮用于 3 个功能(退出、规则、播放)。编写代码由您决定。 :)
private void jButton1MousePressed(java.awt.event.MouseEvent evt) {
// Code for exit the game writes here
}
private void jButton2MousePressed(java.awt.event.MouseEvent evt) {
// Code for show rules writes here
}
private void jButton3MousePressed(java.awt.event.MouseEvent evt) {
// Code for play the game writes here
}
如果您仔细查看 NetBeans 生成的代码,您会发现在 initComponents();
方法中有这样的代码,
jButton3.addMouseListener(new java.awt.event.MouseAdapter() {
public void mousePressed(java.awt.event.MouseEvent evt) {
jButton3MousePressed(evt);
}
});
NetBeans 使用此结构来防止对标准代码进行编辑,从而导致错误的事件处理,从而允许您同时编写在该事件发生时需要执行的代码。
正如您所使用的 MouseListener我建议不要使用 MouseListener。 JButton 设计为与 ActionListener 一起使用。 .
public void jButton3ActionPerformed(ActionEvent e) {
// Your code here
}
NetBeans GUI Builder 生成的代码 ActionListener
将是,
jButton3.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton3ActionPerformed(evt);
}
});
所以我将停止从这里开始写作,因为我无法涵盖其中的所有要点。以下链接提供了有关 NetBeans GUI 构建器基本知识的非常好的快速入门指南。
希望这对您有帮助。快乐编码!
关于java - 如何使用自动生成的netbeans JFrame 表单?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58032348/