如何使用自动生成的网豆JFrame表单?



我想开发一个迷你游戏,但我很难为这个应用程序创建一个菜单。菜单由 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 生成器初始化前端 GUI 使用的所有 Java 摆动组件对象。我看到您已经在使用 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 设计时,这些摆动组件都会在上述方法中自动生成。

您不应在此方法中更改上述代码的任何方面,因为此方法与前端 NetBeans GUI 构建器密不可分。如果您更改代码,您的前端也将更改。但是,如果您想更改代码,则由您来处理它。

默认情况下,NetBeans 在构造函数内部调用initComponents();方法。默认情况下,它是private.在Constructor内运行此initComponents();方法将在应用程序启动后立即显示前端。

public Sterge() {
initComponents();
}

但是你可以在任何你喜欢的地方调用initComponents();方法(构造函数或其他方法(。对于Java,它就像任何其他方法一样。

当我来到你的菜单问题时,我没有看到任何JMenuBar和JMenuItems被使用。如果您需要它们,可以从此处阅读有关如何使用菜单的信息。但是我看到有 3 个 JButton,正如您所说,菜单中有三个按钮。所以我假设这些是你所说的 3 个按钮。

查看类中的以下代码。

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后在控制台中print 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 BuilderActionListener生成的代码将是:

jButton3.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton3ActionPerformed(evt);
}
});

所以我将从这里停止写作,因为我无法涵盖其中的所有要点。以下链接提供了非常好的 NetBeans GUI 构建器基本要素的快速入门指南。

  • 图形用户界面构建简介
  • 在 NetBeans 中设计 Swing GUI

希望这对你有帮助。祝您编码愉快!

最新更新