Java:对另一个类中构造函数的引用

huangapple go评论83阅读模式
英文:

Java: Reference to constructor in another class

问题

我正在尝试制作一个程序,可以自动从一个 .json 文件中提取链接。我对编程还很陌生,正在尝试整理代码,以便其他人能更容易理解。

我有一个名为 Gui 的构造函数,在其中添加了一个关闭按钮和一个带有 awt 的文件浏览器。为了组织项目,我想创建另一个类来提取链接,但我无法弄清楚如何在 Gui 类的构造函数中引用带有文件路径的 TextField。

我需要在另一个类中从 fe 获取文本。

我在网上搜索了几个小时,但找不到适用于我的任何方法。

public class Gui extends Frame {

	public Gui() {
		Frame gui = new Frame(Strings.name);
		
		// 添加“关闭”按钮
		Button cls = new Button(Strings.close);
		cls.setBounds(30, 30, 100, 30);
		gui.add(cls);
		cls.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				System.exit(0);
				
			}
		});
		
		// 文件浏览器
	    TextField fe = new TextField(Strings.file);  
	    fe.setBounds(50,100, 200,30);  
	    fe.setLocation(75, 75);
	    gui.add(fe);
	    fe.addMouseListener(new MouseListener() {
			
			public void mouseReleased(MouseEvent e) {}
			
			public void mousePressed(MouseEvent e) {}
			
			public void mouseExited(MouseEvent e) {}

			public void mouseEntered(MouseEvent e) {}
			
			@Override
			public void mouseClicked(MouseEvent e) {
				FileDialog fd = new FileDialog(gui, Strings.cfile, FileDialog.LOAD);
                fd.setVisible(true);
                fe.setText(fd.getDirectory());
			}
		});
	    
	    // 使应用程序工作
		gui.addWindowListener(new WindowAdapter(){  
	           public void windowClosing(WindowEvent e) {  
	               System.exit(0);
	           }  
	    }); 
		
		gui.setSize(1200, 900);
		gui.setLayout(null);
		gui.setVisible(true);
	}
	
}
英文:

I am trying to make a program, that automaticly extracts a link from a .json file. I'm new to programming, and I'm trying to organize the code, so other people will be able to understand it more easily.

I have a constructor called Gui, where it adds a close button, and a file explorer with awt. To organize the project, I want to make another class to extract the link, but I can't figure out, how I can refer to the TextField with the file path, in the Gui class's constructor.

I need to get the text from fe in another class.

I have searched the web for hours, but I can't find anything that works for me.

public class Gui extends Frame {
public Gui() {
Frame gui = new Frame(Strings.name);
// add "close" button
Button cls = new Button(Strings.close);
cls.setBounds(30, 30, 100, 30);
gui.add(cls);
cls.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
System.exit(0);
}
});
// file explorer
TextField fe = new TextField(Strings.file);  
fe.setBounds(50,100, 200,30);  
fe.setLocation(75, 75);
gui.add(fe);
fe.addMouseListener(new MouseListener() {
public void mouseReleased(MouseEvent e) {}
public void mousePressed(MouseEvent e) {}
public void mouseExited(MouseEvent e) {}
public void mouseEntered(MouseEvent e) {}
@Override
public void mouseClicked(MouseEvent e) {
FileDialog fd = new FileDialog(gui, Strings.cfile, FileDialog.LOAD);
fd.setVisible(true);
fe.setText(fd.getDirectory());
}
});
// make application work
gui.addWindowListener(new WindowAdapter(){  
public void windowClosing(WindowEvent e) {  
System.exit(0);
}  
}); 
gui.setSize(1200, 900);
gui.setLayout(null);
gui.setVisible(true);
}
}
</details>
# 答案1
**得分**: 0
```java
尝试使用以下更新:
public class Gui extends Frame {
// 创建内部类
class MyProcess {
MyProcess(String s) {
System.out.println("使用 s 进行一些操作:" + s);
}
}
public static void main(String args[]) {
new Gui();
}
...
@Override
public void mouseClicked(MouseEvent e) {
FileDialog fd = new FileDialog(gui, "文件", FileDialog.LOAD);
fd.setVisible(true);
fe.setText(fd.getDirectory());
// 根据需要使用内部类
new MyProcess(fe.getText());
}
<br>可能的控制台输出
`使用 s 进行一些操作:D:\some_path\`
<br><br>请注意,使用内部类并不是强制的,也可以是外部类。这完全取决于您的设计。也许在未来的图形界面方面,您可以对以下内容进行一些研究:`JavaFx`、`Swing`,甚至是 `Eclipse Graphics Library`。`Awt` 库是最古老的库之一。
英文:

Could try with the following updates

public class Gui extends Frame {
//create an inner class
class MyProcess
{
MyProcess(String s)
{
System.out.println(&quot;do something with s=&quot;+s);
}
}
public static void main(String args[])
{
new Gui();
}

...

@Override
public void mouseClicked(MouseEvent e) {
FileDialog fd = new FileDialog(gui, &quot;file&quot;, FileDialog.LOAD);
fd.setVisible(true);
fe.setText(fd.getDirectory());
//use the inner class as needed
new MyProcess(fe.getText());
}

<br>Possible output on console
do something with s=D:\some_path\
<br><br>Kindly note that is not mandatory an inner class, could be also an external one. That depends only of your design. Maybe also in the future for graphic interfaces you could do some researches on : JavaFx,Swing maybe Eclipse Graphics Library. Awt library is the most ancient one.

答案2

得分: 0

你应该将对类的引用放在构造函数之外,例如文本:

public class Gui extends Frame {
    // 你的引用
    private TextField text;
    Gui() {
        // 现在实例化你的文本框
        text = new TextField();
    }
    // 返回你的文本框的getter方法
    public TextField getTextField() {
        return text;
    }
}

然后你可以从你的界面获取文本:

Gui gui = new Gui();
TextField field = gui.getTextField();

我建议你先学习Java编程的基础,然后为自己设定更大的项目。

英文:

you should put your references to your classes e.g. text outside of your constructor for example,

public class Gui extends Frame {
// your reference
private TextField text;
Gui() {
// now instantiate your textField
text = new TextField();
}
// getter method that returns your textField
public TextField getTextField() {
return text;
}
}

then you can get your text from your gui.

Gui gui = new Gui();
TextField field =gui.getTextField();

I recommend that learn the basics of java programming and then define bigger project for your self.

huangapple
  • 本文由 发表于 2020年4月6日 18:57:02
  • 转载请务必保留本文链接:https://go.coder-hub.com/61058191.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定