Java 包 - 创建一个按钮并在需要时导入一个按钮
这更像是打包/导入测试。我们将从 .../javaf/test.java 的基本文件夹开始。
我的目标是创建子类别并创建一个带有按钮的类,当我需要按钮时,我可以将其导入到 test.java 中。我觉得我做得对,我知道按钮现在没有做任何事情,但我只想让整个事情正常工作并随后扩展代码。所以这里是 - 这是 test.java
import paket.*; // importing classes from subcategory paket!
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.*;
public class test {
public test() {
JFrame myFrame;
JPanel myPanel;
myFrame = new JFrame("Hello FramWorld");
myPanel = new JPanel();
// Here I want to add the object created in paket/myButts.java
// The problem is how to make these two lines work.
myButts myButton = new myButts();
myPanel.add(myButton);
myFrame.setVisible(true);
myFrame.getContentPane().add(myPanel, BorderLayout.CENTER);
myFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
myFrame.pack();
}
public static void main(String args[]) {
new test();
}
}
这是我的 .../javaf/paket/myButts.java
package paket; // Here is the package function (ought to work like a link)
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
// This class should only create a button.
public class myButts {
public myButts() {
JButton myButt = new JButton();
}
}
我已经编译了 myButts.java 没有错误。但后来我编译 test.java 并给出以下错误:
test.java:19: 找不到符号 符号:方法add(paket.myButts) 位置:类 javax.swing.JPanel myPanel.add(myButton);
感谢您的阅读, Z
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我认为您想要:
如果您想将类的实例直接添加到 Swing 控件,它必须扩展 Swing 或 AWT 类型(在本例中为 JButton)。
之前,您只是在构造函数中创建了一个本地 JButton,该 JButton 在其他地方未使用且无法访问。
请注意,最好尝试遵循 Java 样式指南,特别是命名约定。建议类混合大小写,例如 MyButton。
I think you want:
If you want to add instances of your class directly to Swing controls, it must extend a Swing or AWT type (in this case JButton).
Before, you just created a local JButton in the constructor, which was unused and inaccessible everywhere else.
As a note, it's best to try to follow the Java style guide, in particular naming conventions. Classes are recommended to be mixed case, e.g. MyButton.
错误是:
这意味着 JPanel 类没有采用
paket.myButts
参数的add
方法。让我们看一下 JPanel 文档,查看它有哪些add
方法。有 5 个从 Container 继承的
add
方法,以及 1 个从 Component 继承的方法。 这个方法,像大多数方法一样,要求您传递一个 Component 对象。这需要您传递一个 PopupMenu 对象。因此,您的 paket.myButts 对象似乎不是 Component 或 PopupMenu,但需要是。让您的 myButts 扩展组件可能很诱人。因此,请查看组件文档,看看你会得到什么。请注意,已经提供了几个子类:
Button、Canvas、Checkbox、Choice、Container、Label、List、Scrollbar、TextComponent
现在,由于您实际上需要一个按钮,您可能会猜测 myButts 扩展 Button 是有意义的。那会起作用,你会得到一个 awt.Button。但是,如果您进一步探索,Container 的一些子类是 swing 类,包括 JButton。我建议使用这些秋千课程。
一旦你这样做了,你的构造函数应该采用这种形式:
对于使用 swing 组件的更一般的介绍,我强烈推荐优秀的 摆动教程。
The error is:
It means that the class JPanel doesn't have a method
add
that takes apaket.myButts
parameter. Let's look at the JPanel documentation, to see whatadd
methods it has.There are 5
add
methods inherited from Container, and one from Component. This method, like most of them, requires you to pass a Component object. This one requires you to pass a PopupMenu object.So it seems that your paket.myButts object isn't a Component or a PopupMenu, and needs to be. It might be tempting to make your myButts extend Component. So take a look at the Component documentation, to see what you'll be getting. Notice that there are several subclasses that have already been provided:
Button, Canvas, Checkbox, Choice, Container, Label, List, Scrollbar, TextComponent
Now since you actually want a button, you might guess it would make sense for myButts to extend Button. That would work, and you'd get an awt.Button. However, if you explore further, some of the subclasses of Container are the swing classes, including JButton. I would recommend using these swing classes.
Once you do that, your constructor should take this form:
For a more general introduction to using swing components, I strongly recommend the excellent swing tutorial.