Java 包 - 创建一个按钮并在需要时导入一个按钮

发布于 2024-08-28 20:57:31 字数 1548 浏览 4 评论 0 原文

这更像是打包/导入测试。我们将从 .../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

This is more like a package/import test. We'll start with my base folder at .../javaf/test.java

My goal is to create subcategory and create a class with a button that I can import to test.java when I need a button. I feel like I've done it right, I know that the button doesn't do anything as of now, but I just want to make the whole thing work and expand the code thereafter. So here goes - This is 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();
    }
}

And here is my .../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();
    }
}

I've compiled myButts.java with no errors. But then I compile test.java and it gives me the following error:

test.java:19: cannot find symbol
symbol : method add(paket.myButts)
location: class javax.swing.JPanel
myPanel.add(myButton);

Thanks for reading,
Z

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

凯凯我们等你回来 2024-09-04 20:57:31

我认为您想要:

public class myButts extends JButton {  

}

如果您想将类的实例直接添加到 Swing 控件,它必须扩展 Swing 或 AWT 类型(在本例中为 JButton)。

之前,您只是在构造函数中创建了一个本地 JButton,该 JButton 在其他地方未使用且无法访问。

请注意,最好尝试遵循 Java 样式指南,特别是命名约定。建议类混合大小写,例如 MyButton。

I think you want:

public class myButts extends JButton {  

}

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.

甜心 2024-09-04 20:57:31

错误是:

test.java:19: cannot find symbol symbol : method add(paket.myButts) location: class javax.swing.JPanel myPanel.add(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。我建议使用这些秋千课程。

一旦你这样做了,你的构造函数应该采用这种形式:

public class myButts extends JButton {  
  public myButts() {
    super();
    // here put any myButts-specific construction code, such as:
    setText("Press me!");
  }
}

对于使用 swing 组件的更一般的介绍,我强烈推荐优秀的 摆动教程

The error is:

test.java:19: cannot find symbol symbol : method add(paket.myButts) location: class javax.swing.JPanel myPanel.add(myButton);

It means that the class JPanel doesn't have a method add that takes a paket.myButts parameter. Let's look at the JPanel documentation, to see what add 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:

public class myButts extends JButton {  
  public myButts() {
    super();
    // here put any myButts-specific construction code, such as:
    setText("Press me!");
  }
}

For a more general introduction to using swing components, I strongly recommend the excellent swing tutorial.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文