简体   繁体   English

关于Java中的GUI?

[英]About GUI in Java?

I'm trying to learn GUI in Java, but I'm a little bit confused and wondering whats the difference between this code to add components in a window? 我正在尝试用Java学习GUI,但我有点困惑,并想知道这个代码在窗口中添加组件的区别是什么? When I view code examples it varies a lot, some examples use JPanel and just add by writing: panel.add(something); 当我查看代码示例时,它变化很大,一些示例使用JPanel,只需通过编写:panel.add(something); Some code just use add(something); 有些代码只使用add(something); or contentPane.add(something); 或contentPane.add(某事物); I'm just curious. 我只是好奇。 Thanks! 谢谢!

JList text;

JPanel panel = new JPanel();
frame.add(panel);
panel.add(text);

- -

setLayout(new FlowLayout);
add(text);

- -

Container contentPane;
contentPane = getContentPane();
contentpane.setLayout(new FlowLayout);
contentPane.add(text);

The differences you are seeing have more to do with the Java language, and less to do with the actual operations being performed. 您所看到的差异更多地与Java语言有关,而与正在执行的实际操作关系不大。

If you call a method without specifying a particular object, the Java language assumes that you are calling the method on the this object, or the object you are currently working in. 如果在未指定特定对象的情况下调用方法,则Java语言假定您正在调用this对象上的方法或您当前正在使用的对象。

public class MyClass {

  public void doThis() {
  }

  public void doThat() {
    // the following line will call this.doThis()
    doThis();
    // this is exactly the same as the line above, except it is explicitly stated
    this.doThis();
  }

}

Sometimes a method wishes to call a method on a different object. 有时,方法希望在不同的对象上调用方法。 In that case, you have a variable hold a reference to the "other" object, and when you call the method on that object, you must dereference the name reference, one example might look like: 在这种情况下,您有一个变量持有对“other”对象的引用,当您在该对象上调用该方法时,您必须取消引用该名称引用,一个示例可能如下所示:

public Class MyOtherClass {

  public void doTheOtherThing(MyClass myClass) {
    myClass.doThis();
  }

}

Different examples do Swing programming differently. Swing编程的不同示例不同。 Some examples tend to be more object-oriented than others. 一些示例往往比其他示例更面向对象。 To illustrate, if you need a "special" button, one example might subclass the button and configure it appropriately within the subclass, while another example might construct a non-subclassed button and configure it from outside of the 'JButton' class. 为了说明,如果你需要一个“特殊”按钮,一个例子可能是子类的按钮并在子类中适当地配置它,而另一个例子可能构造一个非子类按钮并从'JButton'类的外部配置它。

Good object oriented programming favors subclassing as a solution over external configuration. 良好的面向对象编程有利于子类化作为外部配置的解决方案。 That is because the code to configure the button becomes part of the new button subclass, and so when one moves the Button into different places architecturally through the program, the configuration code cannot be accidentally separated from the Button. 这是因为配置按钮的代码成为新按钮子类的一部分,因此当通过程序将Button移动到不同的位置时,配置代码不会意外地与Button分离。 Sometimes analyzing the best object oriented structure can be costly, in which case one might release code with lots of "object-external" influences. 有时分析最佳的面向对象结构可能代价高昂,在这种情况下,人们可能会发布具有大量“对象外部”影响的代码。

The difference lies in where the code is located. 不同之处在于代码所在的位置。 The second section of code would only make sense inside a method of a class that had setLayout and add methods; 代码的第二部分只在具有setLayoutadd方法的类的方法中有意义; most likely this is because the class is a custom GUI component that inherits from JPanel or another Container . 这很可能是因为该类是从JPanel或其他Container继承的自定义GUI组件。

The third section calls getContentPane , which is a method of JFrame , so it most likely inherits from that class. 第三部分调用getContentPane ,它是JFrame一种方法,因此它很可能从该类继承。

If you edit your question to post the surrounding context of the example code (or links to it), I (or someone more experienced with Swing than I) might be able to give a more detailed explanation of how it works. 如果你编辑你的问题以发布示例代码的周围环境(或链接到它),我(或者比我更有经验的Swing)可能能够更详细地解释它是如何工作的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM