3

我的第一个问题是 - 我们可以在 netbeans 中公开或保护摆动控件(如textfield或)吗?combobox当我们将组件拖放到框架中时,代码会自动生成,并且默认情况下组件是私有的。我们不能在其他类中使用它们。

第二个是 - 有没有其他方法可以在其他类中使用一个框架的组件?

例如-在一帧中,我们只有一帧textfieldbutton一帧label。我们希望将标签的文本设置为与单击按钮时在文本字段中写入的文本相同。但是代码必须写在其他类中。

package javaapplication10;

import javax.swing.JLabel;
public class NewJFrame extends javax.swing.JFrame {

    public NewJFrame() {
        initComponents();
    }


    private void initComponents() {

        jButton1 = new javax.swing.JButton();
        jTextField1 = new javax.swing.JTextField();
        label1 = new java.awt.Label();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        jButton1.setText("jButton1");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        label1.setText("label1");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                    .addGroup(layout.createSequentialGroup()
                        .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, 76, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                        .addGap(100, 100, 100)
                        .addComponent(label1, javax.swing.GroupLayout.PREFERRED_SIZE, 67, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 31, Short.MAX_VALUE)
                .addComponent(jButton1)
                .addGap(129, 129, 129))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(134, 134, 134)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jButton1))
                .addGap(56, 56, 56)
                .addComponent(label1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(67, Short.MAX_VALUE))
        );

        pack();
    }

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {

        label1.setText(jTextField1.getText());
    }
    public static void main(String args[]) {
        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(NewJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(NewJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(NewJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(NewJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }



        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new NewJFrame().setVisible(true);
            }
        });
    }

    private javax.swing.JButton jButton1;
    private javax.swing.JTextField jTextField1;
    private java.awt.Label label1;

}

我只写过

label1.setText(jTextField1.getText());

但这是在同一个班级。我希望这行写在其他类中。但是我们不能在其他类中引用它们textfieldlabellprivate

我怎样才能做到这一点?

我这样做是为了学习。

请帮忙。

4

3 回答 3

8

右键组件->属性->代码:

在此处输入图像描述

点击变量修饰符,选择你想要的。

Is there any other way by which we can use the components of one frame in other class?

是的,

  • 1) 类名.变量名
  • 2)创建类的对象,通过对象调用变量。
于 2013-03-30T11:33:51.620 回答
4

I must downvote the accepted answer because it doesn't actually answer the question posed in the title: "How to make swing controls in netbeans by default protected or public?" If the controls are by default protected, you would just drag a control into your form and it would be protected without any further action on your part. The accepted answer requires you to change each control to protected or public, which can be a big nuisance.

To make swing controls protected or public by default

  1. open the Design Navigator window,
  2. right click the topmost node (e.g. the Form node if you are creating a form),
  3. click Properties, and under Code Generation,
  4. change the Variables Modifier to protected or public.

To make this change for all future GUI forms,

  1. 转到工具 - 选项,

  2. 选择选项卡 Java,然后选择 GUI builder,

  3. 并更改变量修饰符。

于 2015-11-21T00:19:13.390 回答
2

只需将所需私有组件的 getter 添加到您的类中。

private JTextField text1 = new JTextField();
public JTextField getText1()
{ 
  return text1;
}
于 2013-03-30T10:31:36.767 回答