Netbeans Java SE GUI Builder:私有 initComponents() 问题
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2972181/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Netbeans Java SE GUI Builder: private initComponents() problem
提问by masnun
When I build a GUI for my Java SE app with Netbeans GUI builder, it puts all the codes in the initComponents() method which is private. I could not change it to public. So, all the components are accessible only to the class containing the UI.
当我使用 Netbeans GUI 构建器为我的 Java SE 应用程序构建 GUI 时,它将所有代码放在私有的 initComponents() 方法中。我无法将其更改为公开。因此,所有组件只能由包含 UI 的类访问。
I want to access those components from another class so that I can write custom event handlers and everything. Most importantly I want to separate my GUI code and non-GUI from each other.
我想从另一个类访问这些组件,以便我可以编写自定义事件处理程序和所有内容。最重要的是,我想将 GUI 代码和非 GUI 代码彼此分开。
I can copy paste the GUI code and later make them public by hand to achieve what I want. But thats a pain. I have to handcraft a portion whenever I need to re-design the UI.
我可以复制粘贴 GUI 代码,然后手动将它们公开以实现我想要的。但那是一种痛苦。每当我需要重新设计 UI 时,我都必须手工制作一部分。
What I tried to do:
我试图做的事情:
I used the variable identifier to make the text box public. Now how can I access the text box from the Main class? I think I need the component generated in a public method as well.
我使用变量标识符来公开文本框。现在如何从 Main 类访问文本框?我想我也需要在公共方法中生成的组件。
I am new to Java. Any helps?
我是 Java 新手。有帮助吗?
Here's the sample classes:
这是示例类:
The UI(uiFrame.java)
用户界面(uiFrame.java)
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
/*
* uiFrame.java
*
* Created on Jun 3, 2010, 9:33:15 PM
*/
package barcode;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JFileChooser;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import net.sourceforge.barbecue.output.OutputException;
/**
*
* @author masnun
*/
public class uiFrame extends javax.swing.JFrame {
/** Creates new form uiFrame */
public uiFrame() {
try {
try {
// Set cross-platform Java L&F (also called "Metal")
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException ex) {
Logger.getLogger(uiFrame.class.getName()).log(Level.SEVERE, null, ex);
} catch (InstantiationException ex) {
Logger.getLogger(uiFrame.class.getName()).log(Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
Logger.getLogger(uiFrame.class.getName()).log(Level.SEVERE, null, ex);
} catch (UnsupportedLookAndFeelException ex) {
Logger.getLogger(uiFrame.class.getName()).log(Level.SEVERE, null, ex);
}
} finally {
}
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() {
label1 = new javax.swing.JLabel();
textBox = new javax.swing.JTextField();
saveButton = new javax.swing.JButton();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
label1.setFont(label1.getFont().deriveFont(label1.getFont().getStyle() | java.awt.Font.BOLD, 13));
label1.setText("Type a text:");
label1.setName("label1"); // NOI18N
saveButton.setText("Save");
saveButton.addMouseListener(new java.awt.event.MouseAdapter() {
public void mousePressed(java.awt.event.MouseEvent evt) {
saveButtonMousePressed(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(56, 56, 56)
.addComponent(textBox, javax.swing.GroupLayout.PREFERRED_SIZE, 272, javax.swing.GroupLayout.PREFERRED_SIZE)
.addContainerGap(72, Short.MAX_VALUE))
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
.addContainerGap(154, Short.MAX_VALUE)
.addComponent(saveButton, javax.swing.GroupLayout.PREFERRED_SIZE, 102, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(144, 144, 144))
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
.addContainerGap(140, Short.MAX_VALUE)
.addComponent(label1, javax.swing.GroupLayout.PREFERRED_SIZE, 133, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(127, 127, 127))
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addComponent(label1, javax.swing.GroupLayout.PREFERRED_SIZE, 25, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(textBox, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addComponent(saveButton)
.addContainerGap(193, Short.MAX_VALUE))
);
pack();
}// </editor-fold>
@SuppressWarnings("static-access")
private void saveButtonMousePressed(java.awt.event.MouseEvent evt) {
JFileChooser file = new JFileChooser();
file.showSaveDialog(null);
String data = file.getSelectedFile().getAbsolutePath();
String text = textBox.getText();
BarcodeGenerator barcodeFactory = new BarcodeGenerator();
try {
barcodeFactory.generateBarcode(text, data);
} catch (OutputException ex) {
Logger.getLogger(uiFrame.class.getName()).log(Level.SEVERE, null, ex);
}
}
/**
* @param args the command line arguments
*/
// Variables declaration - do not modify
private javax.swing.JLabel label1;
private javax.swing.JButton saveButton;
public javax.swing.JTextField textBox;
// End of variables declaration
}
The Main Class(Main.java)
主类(Main.java)
package barcode;
import javax.swing.JFrame;
public class Main {
public static void main(String[] args) {
JFrame ui = new uiFrame();
ui.pack();
ui.show();
}
}
回答by den bardadym
When you click any component in GUI Builder you can change access level. Properties -> Code -> Variable Modifiers
当您单击 GUI Builder 中的任何组件时,您可以更改访问级别。属性 -> 代码 -> 变量修饰符
回答by Madabitjer
Look at the properties pane, you should see the following :
查看属性窗格,您应该看到以下内容:
Properties : Binding : Events : Code
属性:绑定:事件:代码
Click on code and then variable modifiers. You can set the level to what you require there.
单击代码,然后单击变量修饰符。您可以将级别设置为您需要的级别。
Good luck!
祝你好运!
回答by vkraemer
It looks like everyone is giving you the correct answer to the question you have asked... but no one is warning you that by using their answers, you are probably going to write code that violates one of your stated goals:
看起来每个人都为您提出的问题提供了正确答案……但没有人警告您,通过使用他们的答案,您可能会编写违反您既定目标之一的代码:
Most importantly I want to separate my GUI code and non-GUI from each other.
最重要的是,我想将 GUI 代码和非 GUI 代码彼此分开。
By making the UI components more visible, you are more likely to mix your UI and non-UI logic together in your program.
通过使 UI 组件更加可见,您更有可能在程序中将 UI 和非 UI 逻辑混合在一起。
You may want to search for 'java model view controller'and read through some of the articles before you proceed.
在继续之前,您可能想要搜索“java 模型视图控制器”并通读一些文章。
回答by masnun
Instead of instantiating a new uiFrame object I took a new JFrame. So it didn't have any public variable named textBox.
我没有实例化一个新的 uiFrame 对象,而是使用了一个新的 JFrame。所以它没有任何名为 textBox 的公共变量。

