小编gui*_*eak的帖子

Java Swing - JDialog默认焦点

在互联网上找到这样一个信息:

"当JDialog(或JFrame)可见时,默认情况下焦点放在第一个可聚焦组件上."

让我们考虑一下这样的代码:

public class MyDialog extends JDialog{
    // Dialog's components:
    private JLabel dialogLabel1 = new JLabel("Hello");
    private JLabel dialogLabel2 = new JLabel("Message");
    private JButton dialogBtn = new JButton("Sample Btn text");

    public MyDialog(JFrame parent, String title, ModalityType modality){
        super(parent, title, modality);
        dialogBtn.setName("Button");    //
        dialogLabel1.setName("Label1"); // - setting names
        dialogLabel2.setName("Label2"); //
        setTitle(title);
        setModalityType(modality);
        setSize(300, 100);
        setLocation(200, 200);
        // adding comps to contentPane
        getContentPane().add(dialogLabel1, BorderLayout.PAGE_START);
        getContentPane().add(dialogBtn, BorderLayout.CENTER);
        getContentPane().add(dialogLabel2, BorderLayout.PAGE_END);
        pack();
    }

    public void showDialog(){
        setVisible(true);
        listComps(rootPane.getComponents());
        setDefaultCloseOperation(DISPOSE_ON_CLOSE);
    } …
Run Code Online (Sandbox Code Playgroud)

java swing focus jdialog

4
推荐指数
1
解决办法
3015
查看次数

弱势和强势属性的例子

我开始使用Objective-C开发并尝试理解弱和强引用.我想我明白了,但我不确定...

让我们考虑一下代码:

@interface SomeClass {} 
@property (nonatomic, weak) NSString* propertyName;
@end
Run Code Online (Sandbox Code Playgroud)

现在,如果我在代码中的某个地方调用这样的东西:

NSString* s = someClassInstance.propertyName;

参考计数器不会递增.我的理解是否正确?

  • 疑问1:什么是参考计数器值propertyName

  • 怀疑2:那么......你能举例说明我什么时候可以获得对这个属性的强烈引用?我希望你知道我的意思或我不明白的东西......

我会得到弱参考.

reference-counting objective-c

2
推荐指数
1
解决办法
3197
查看次数

Objective-C对象在被释放后存在于内存中

我正在学习Objective-c内存管理(没有ARC),我遇到了一些常见的问题.这里曾提到过几次,所以我不是在问你"究竟是什么!?"而是"我能理解正确吗?"

问题:

文件说:

- (NSString *)fullName {

    NSString *string = [[[NSString alloc] initWithFormat:@"%@ %@",

                                          self.firstName, self.lastName] autorelease];

    return string;

}
Run Code Online (Sandbox Code Playgroud)

您拥有alloc返回的字符串.要遵守内存管理规则,您必须在丢失对该字符串的引用之前放弃该字符串的所有权.但是,如果使用release,则在返回之前将释放该字符串(并且该方法将返回无效对象).使用自动释放,表示您要放弃所有权,但允许方法的调用者在释放之前使用返回的字符串.

所以让我们试试:

// Method defined in one on my class:
-(NSString*) returnString {
    NSString* str = [[NSString alloc] initWithString:@"returned String"]; 
    NSLog(@"1) Address in method = %p", [str self]);
    [str release];
    NSLog(@"2) Address in method = %p", [str self]);
    return str; 
 }

// In main: 
        NSString* str = [myObject returnString];
        NSLog(@"String is: [%@]", str);
        NSLog(@"3) Address in main = …
Run Code Online (Sandbox Code Playgroud)

memory-management objective-c ios

2
推荐指数
1
解决办法
263
查看次数

Objective C - 示例Singleton实现

*我肯定需要休息......原因很简单 - 数组没有分配...感谢您的帮助.由于这个令人尴尬的错误,我标记了我的帖子以删除它.我觉得它对用户没用;)*

我刚刚尝试在iOS中创建一个单例类,但我可能犯了一个错误.代码(无需ARC):

#import "PeopleDatabase.h"
#import "Person.h"

#import <Foundation/Foundation.h>

@interface PeopleDatabase : NSObject{objetive
    NSMutableArray* _arrayOfPeople;
}

+(PeopleDatabase *) getInstance;

@property (nonatomic, retain) NSMutableArray* arrayOfPeople;


@end
Run Code Online (Sandbox Code Playgroud)

-

    @implementation PeopleDatabase
    @synthesize arrayOfPeople = _arrayOfPeople;

    static PeopleDatabase* instance = nil;

    -(id)init{
        if(self = [super init]) {
            Person* person = [[[Person alloc] initWithName:@"John" sname:@"Derovsky" descr:@"Some kind of description" iconName:@"johnphoto.png" title:Prof] retain];

            [_arrayOfPeople addObject:person];
            NSLog(@"array count = %d", [_arrayOfPeople count]); // <== array count = 0 
            [person release];
        }
        return self;
    }

    +(PeopleDatabase …
Run Code Online (Sandbox Code Playgroud)

singleton objective-c ios

1
推荐指数
1
解决办法
1万
查看次数