简体   繁体   English

我可以阅读,但不能编辑主课内容

[英]I can read but Can't edit Main class contents

I'm using netbeans to program something with a user interface... I hava a main class that named " NewJFrame.java "( A ) and one more class that named " NewClass.java "( B ). 我正在使用netbeans通过用户界面进行编程...我有一个名为“ NewJFrame.java ”( A )的主类,还有一个名为“ NewClass.java ”( B )的类。 Class A is extended to class B like this: A延伸到类B这样的:

public class NewClass extends NewJFrame{
   ...
}

Contents of ClassA are public static like this: ClassA内容是公共静态的,如下所示:

public static javax.swing.JTextField TextBox1;

I also has a button in classA .So when I click the button, it will call a function from the classB and that function needs to edit TextBox1 's text... 我在classA也有一个按钮, classA当我单击该按钮时,它将从classB调用一个函数,该函数需要编辑TextBox1的文本...

Here is whats going on when I click the button: 这是我单击按钮时发生的情况:

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

    String Str1;
    NewClass nc = new NewClass();
    Str1=nc.call();

}

Here is the funcion in ClassB : 这是ClassB函数:

public String call()
{

    String Str;
    Str = TextBox1.getText();
    TextBox1.setText(Str + "1");  //This part isn't work.
    JOptionPane.showConfirmDialog(null,Str,"22222222",JOptionPane.PLAIN_MESSAGE);
    return Str;
}

So I can read the text of TextBox1 and show it in a messagebox but cannot edit his text. 因此,我可以阅读TextBox1的文本并将其显示在消息框中,但无法编辑其文本。 If I put this code in main class it works perfectly but in another class it doesn't work. 如果我将此代码放在主类中,则效果很好,但在另一类中,则不起作用。 Can someone help me to reslove this problem? 有人可以帮我解决这个问题吗?

(I'm using netbeans 6.9.1) (我正在使用netbeans 6.9.1)


I Just Trying to use some another class to add my code because I dont want all the codes stay in same file this is not usefull... Come on someone needs to know how to do that you can't be writing all the codes in a *.java file right? 我只是想使用另一个类来添加我的代码,因为我不希望所有代码都保存在同一文件中,所以这没有用。。。来吧,有人需要知道怎么做,你不能在其中编写所有代码。 * .java文件对吗?

The problem you are facing has nothing to do with NetBeans IDE, you will face the same problem with any IDE for this code. 您面临的问题与NetBeans IDE无关,对于此代码,任何IDE都将面临相同的问题。

One way of achieving this is by aggregating the NewJFrame class in the NewClass instead of extending it: 实现此目的的一种方法是在NewClass中聚合NewJFrame类,而不是对其进行扩展:

Let me exlplain with some code: 让我用一些代码来说明:

public class NewClass {
    private NewJFrame frame = null;

    public NewClass(NewJFrame frame) {
        this.frame = frame;
    }

    public String call()
    {

        String text;
        text = frame.TextBox1.getText();
        frame.TextBox1.setText(text + "1");  //This will work now.
        JOptionPane.showConfirmDialog(null,text,"22222222",JOptionPane.PLAIN_MESSAGE);
        return text;
    }

}

Here we will receive a reference to the calling JFrame class and will use fields defined in that class. 在这里,我们将收到对调用JFrame类的引用,并将使用该类中定义的字段。

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

    String Str1;
    NewClass nc = new NewClass(this); // see the parameter we are passing here
    Str1=nc.call();

}

When we create an object of class NewClass we will pass the reference of the currently calling NewJFrame object 当我们创建类NewClass的对象时,我们将传递当前调用的NewJFrame对象的引用

This will work check it. 这将检查它。

Now coming to why your code is not working. 现在介绍为什么您的代码无法正常工作。 When NewClass is extending NewJFrame and when you create a new object of NewClass class it contains a separate copy of the NewJFrame which is different from the calling NewJFrame reference hence the field is getting set in another JFrame and not what you wanted. 当NewClass扩展NewJFrame并创建NewClass类的新对象时,它包含NewJFrame的单独副本,该副本不同于调用NewJFrame引用,因此该字段将在另一个JFrame中设置,而不是您想要的。

with regards 带着敬意
Tushar Joshi, Nagpur 那格浦尔Tushar Joshi

AFAIK Netbeans阻止您通过手工GUI进行编辑,并且根据不同的问题(例如您遇到的问题)进行不同的处理……但这是几个月前的事,我不知道当前的版本是否还那么烂。

I really don't understand why you are forcing yourself to use a new class for this? 我真的不明白您为什么要强迫自己为此使用新类? Even if you NEED to, I don't understand why NewClass extends NewJFrame since you are only creating an instance to call a method that has nothing to do with GUI. 即使您需要,我也不明白为什么NewClass 扩展 NewJFrame,因为您只是创建一个实例来调用与GUI无关的方法。

I think creating NewClass isn't necessary. 我认为没有必要创建NewClass。 Writing all the code in one class isn't bad by itself. 在一个类中编写所有代码本身并不坏。 This really depends on MANY factors: how much is "all the code"? 这实际上取决于许多因素:“所有代码”多少钱? Does it make sense to separate responsibilities? 分开职责是否有意义? Etc, etc... 等等...

So make the JTextField and JButton NOT static and NOT public, and simply do everything in there: 因此,使JTextField和JButton不是静态的并且不是公共的,只需在其中执行所有操作即可:

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

    String str = TextBox1.getText();
    TextBox1.setText(str + "1");  //This part isn't work.
    JOptionPane.showConfirmDialog(null,Str,"22222222",JOptionPane.PLAIN_MESSAGE);
}

PS: variable names are start in lowercase: String str , not String Str . PS:变量名称以小写字母开头:String str ,而不是String Str

I Found a solution. 我找到了解决方案。 I'm throwing the contents whereever I'll use. 我会将内容扔到我将要使用的任何地方。 Here is an Example: 这是一个例子:

Main class: 主班:

private void formWindowOpened(WindowEvent evt) {                                  
    Tab1Codes tc1 = new Tab1Codes();
    if(!tc1.LockAll(TabMenu1))
        System.exit(1);
    tc1.dispose();
}   

Another class where I added some of my codes: 我在其中添加了一些代码的另一个类:

public boolean LockAll(javax.swing.JTabbedPane TabMenu){
    try
    {
        TabMenu.setEnabledAt(1, false);
        TabMenu.setEnabledAt(2, false);
        TabMenu.setEnabledAt(3, false);
        TabMenu.setEnabledAt(4, false);
    }catch(Exception e)
    {
        JOptionPane.showConfirmDialog(null, "I can't Lock the tabs!",
                "Locking tabs...",
                JOptionPane.PLAIN_MESSAGE,
                JOptionPane.ERROR_MESSAGE);
        return false;
    }
    return true;
}

So, I can edit the contents in another class but it's little useless to send every content I want to read and edit. 因此,我可以在另一个类中编辑内容,但是发送要阅读和编辑的每个内容都没什么用。 If someone knows any short way please write here. 如果有人知道任何捷径,请在这里写。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM