[英]Windows or components do not show up in Java Swing
I am getting started with Java Swing using Eclipse on MacOS. 我正在MacOS上使用Eclipse的Java Swing入门。 I read some tutorials;
我读了一些教程; however, I failed to reproduce the results: whenever I copy-paste a sample code, the result is different from what I am supposed to get.
但是,我无法重现结果:每当我复制粘贴示例代码时,结果都与我应该得到的结果不同。
For example, with this I get the empty window, but not the buttons and other stuff. 例如,用这个我得到一个空的窗口,但是没有按钮和其他东西。
Another example is code from https://stackoverflow.com/a/14673412/4189299 : when I run it, I observe only this (instead of a button and two text fields): 另一个示例是来自https://stackoverflow.com/a/14673412/4189299的代码:运行该代码时,我仅观察到此内容(而不是一个按钮和两个文本字段):
Do I miss something or should I change some Eclipse settings? 我会错过某些东西还是应该更改某些Eclipse设置?
You can copy paste the same code from below , once you pasted it inside your IDE . 将代码粘贴到IDE中后,就可以从下面复制粘贴相同的代码。 write one statement public static void main(Strings []abc) By yourself.
自己编写一条语句public static void main(Strings [] abc) 。
import javax.swing.*;
public class HelloWorld extends JFrame{
public static void main(String ard[]) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
new HelloWorld().setVisible(true);
}
});
}
public HelloWorld()
{
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setTitle("Hello World Button App");
panel pan= new panel();
add(pan.panel);
pack();
setVisible(true);
}
}
class panel {
private JButton btn1 = new JButton("Klick!");
private JTextField txt1 = new JTextField(10);
private JTextField txt2 = new JTextField(10);
JPanel panel;
public panel() {
panel = new JPanel();
panel.add(btn1);
panel.add(txt1);
panel.add(txt2);
}
}
In the first example you should only get simple frame with no buttons at all. 在第一个示例中,您应该只获得根本没有按钮的简单框架。 Just a frame with a title.
只是一个带有标题的框架。 Here is a Simple Example .
这是一个简单的例子 。
On the other case in the second example I tried and example is fine. 在第二个示例中,我尝试了另一种情况,示例很好。 Maybe you should check that you copied all code?
也许您应该检查一下是否复制了所有代码?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.