默认情况下,如何在Netbeans中使Swing控件处于受保护或公开状态? [英] How to make swing controls in netbeans by default protected or public?

查看:112
本文介绍了默认情况下,如何在Netbeans中使Swing控件处于受保护或公开状态?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的第一个问题是-我们可以将挥杆控件(例如textfieldcombobox)设置为公共或在netbeans中进行保护吗?当我们将n组件拖放到框架中时,代码会自动生成,并且默认情况下,这些组件是私有的.我们不能在其他类中使用它们.

My first question is - can we make the swing controls (like textfield or combobox) public or protected in netbeans? When we drag n drop the components in a frame the code is autogenerated and the components are by default private. We cannot use them in other classes.

第二个是-还有其他方法可以使用另一类中一帧的组件吗?

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

例如-在一帧中,我们只有一个textfield一个button和一个label.我们想要将标签的文本设置为与单击按钮时在文本字段中写入的相同.但是代码必须用其他类编写.

For example - In one frame we have one textfield one button and one label only. We want to set the text of the label same as written in the textfield on clicking the button. but the code must be written in some other class.

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,我们无法在其他类中引用它们.

but this is in the same class. I want this line to be written in some other class. but as textfield and labell is private we can not refer to them in other class.

我该怎么做?

我这样做是出于学习目的.

I am doing it for the learning purpose.

请帮助.

推荐答案

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

Right click on the component->properties->code:

点击变量修饰符,选择所需的内容.

Click on Variable Modifiers,choose which ever you want.

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

是的

  • 1)classname.variablename
  • 2)创建类的对象,通过该对象调用变量.

这篇关于默认情况下,如何在Netbeans中使Swing控件处于受保护或公开状态?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆