gpt4 book ai didi

java - 控制JFrame其他无效吗?

转载 作者:行者123 更新时间:2023-12-02 12:44:15 25 4
gpt4 key购买 nike

我想从“PART3”控制 JFrame 有什么帮助吗?StackOverFlow 要求更多信息;)所以我的想法在第 3 部分 g.f 我知道这是错误的,所以任何帮助

import java.io.IOException;
import javax.swing.JFrame;

public class test {
public static void main(String[] args) throws IOException{
SetUp g =new SetUp();
g.PART2();
g.PART3();

}}

class SetUp {
void PART2()throws IOException{
JFrame f = new JFrame();

f.setTitle("Test");
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

// System.out.println(f);
}

void PART3()throws IOException{
SetUp g =new SetUp();
g.f.setSize(128,128);
g.f.setLocation(10,10);
g.f.setVisible(true);

// System.out.println(g.f);
}
}

所以我的想法在第 3 部分 g.f 我知道这是错误的所以任何帮助

最佳答案

f 需要是字段才能在其他方法中访问它。

class SetUp {

private JFrame f; // `f` is now an instance field of the SetUp class

void PART2()throws IOException {
f = new JFrame();
f.setTitle("Test");
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}

void PART3()throws IOException{
// SetUp g =new SetUp();
// you can directly access 'f' here, there's no need to create a new object

f.setSize(128,128);
f.setLocation(10,10);
f.setVisible(true);
}
}

关于java - 控制JFrame其他无效吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44824040/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com