推进Java中的Swing框架
Java基础类把Swing框架作为它的一部分。Java 1.1版最初将Swing作为一个定义明确的库。后来,它被整合到了Java语言中。
由于框架可能是庞大而复杂的,所以框架可以增强问题。当我们回忆起小程序时,一个弹簧框架可能会带来比它所谓的改进的独特小工具更不可思议的困难。Swing有梦想,可以减少更大的框架应该做的任何苛刻的结果。
该框架的主要目标是提供Swing应用程序的内核,协助程序员迅速入门,并从与每个Swing框架相似的几个元素中获得最佳实践。
前提条件
要跟上这篇文章,读者必须具备以下条件。
- 熟悉Java编程语言。
- 熟悉java swing。
swing提供的可插拔的感觉和外观
Swing提供的可插拔的感觉和外观是使它被Java代码考虑的原因之一。Swing的控制使它创造了一种审美的感觉。这意味着Swing可以将一个组件的外观和感觉与组件的逻辑分开。
它可以为任何给定的组件 "插入 "一个新的外观和感觉,而不会在使用该组件的代码中产生任何副作用。可插拔的感觉和外观在整个过程中都有很大的好处。
建议在所有的平台上有一个统一的感觉和外观。假设一个应用程序只在MacOS环境中运行,那么定义MacOS的感觉和外观是合理的。也可以对惯例的感觉和外观进行建模。
总而言之,感觉和外观可以在运行时动态地改变。
MVC(Model-View-Controller)连接
Swing框架是理想的,因为它是由参考MVC创建的链接的想法和设计来支撑的。由于控制器和视图的分离造成了高度的差异,Swing组件出现了一些问题。
另一方面,Swing部署了一个修改过的MVC版本,它将视图和控制器绑定在一个被称为UI委托的专属逻辑对象中。可分离模型蓝图或模型-代表蓝图是Swing使用的两种技术。
尽管swing的框架的组件蓝图是基于MVC的,但传统的实现并没有被部署。Model-Delegate的蓝图调整了大小,实现了swing的可插拔感觉和外观。
事件处理
由于我们有一些关于swing框架的信息,我们可以在本节中讨论如何处理事件。Swing产生的交互是由响应用户输入以及事件的组件来管理的。当一个设定的定时器熄灭时,它就会触发一个事件。
所有基于Swing的应用程序中最重要的部分是事件处理。在大多数情况下,AWT中的事件也被用于swing中,这些事件的执行都包含在java.awt.event 。java.swing.event,只允许包装特定于swing的事件。
下面的代码说明的是一个事件生成的swing按钮PUSH 。
package eventdemo;
import java.awt.event.*;
import javax.swing.*;
class EventDemo {
JLabel lbJ;
EventDemo() {
JFrame frJ = new JFrame("EVENT HANDLER ILLUSTRATOR");
// We will be performing layout specification
frJ.setLayout(new FlowLayout());
// At this point we have to assign our frame a primary size.
frJ.setSize(400, 400);
// When the user closes the application, the program should stop.
frJ.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// You can create as many buttons but here we are going create only two.
JButton jbtnEvent1 = new JButton("Event1");
JButton jbtnEvent2 = new JButton("Event2");
// Implementing Event1 listener.
jbtnEvent1.addActionListener(new ActionListener() {
// Declaring ActionPerformed to be visible to all classes.
public void actionPerformed(ActionEvent ae) {
lbJ.setText("EVENT1 HAS BEEN HANDLED." + "\n");
}
});
// include listener for Event2.
jbtnEvent2.addActionListener(new ActionListener(){
lbJ.setText("EVENT2 HAS BEEN HANDLED." + "\n");
}
);
frJ.add(jbtnEvent1);
frJ.add(jbtnEvent2);
// text-based label will have to be created.
lbJ = new JLabel("Press any button.");
// Putting labels.
frJ.add(lbJ);
//Putting the frame on display.
frJ.setVisible(true);
}
// Declaration of the main method
public static void main(String args[]) {
//This metod uses Swing for its user interface.
SwingUtilities.invokeLater(new Runnable() //declaration of utility methods.
{
public void run() {
//Recall method (function) EventDemo
new EventDemo();
}
});
}
}
当你运行你的程序时,第一个输出将如下图所示。

当你点击第一个按钮时,你会得到如下的显示。

当你点击第二个按钮时,你会得到下图所示的输出显示。

执行摇摆画
为了实现绘画,我们需要了解以下几点。Swing框架是强大的,因为它恰好不限于使用Swing框架,因为它有允许用户直接输入到框架、面板或Swing的其他组件(如JLabel)的输出区域的功能。
此外,在一些情况下,Swing的使用不需要直接画到组件的显示。这个功能取决于这些应用。
为了直接输入组件中包含的演示文稿的给定输出,你将需要使用AWT库中的绘图方法和程序,像drawRect() 方法,甚至是drawLine() 方法。
// We will painting the panel lines.
package paintdemo;
import java.awt.*;
// Allows for use of input methods on swing at runtime
import javax.swing.*;
// allows for importing of java class or packages
import java.util.*;
// In the panel lines are plotted.
class PaintPanel extends JPanel {
Insets set; // The panel holder is Insets
Random rand; // Generation of random numbers
// Construction of the panel.
PaintPanel() {
//Placing a border around the panel gives yuor platform a neat view.
setBorder(
//Defining the border shade
BorderFactory.createLineBorder(Color.GREEN, 5));
//An object for random outputs is created.
rand = new Random();
}
// PaintComponent() function needs to be overriden.
@Override
protected void paintComponent(Graphics j) {
// Superclass method calling first has to be done.
super.paintComponent(j);
// Declaring of variables
int a, b, a2, b2;
// We are getting the dimensions.
int hgt = getHeight();
// Declaring Method for getting height and width.
int wdt = getWidth();
// Getting the insets.
ins = getInsets();
// Randomly produced endpoints of nine lines are drawn.
for(int i=0; i < 9; i++) {
// At this point we will be acquiring random coordinates.
a = rand.nextInt(wdt-ins.left);
b = rand.nextInt(hgt-ins.bottom);
a2 = rand.nextInt(wdt-ins.left);
b2 = rand.nextInt(hgt-ins.bottom);
// Drawing of the lines.
j.drawLine(a, b, a2, b2);
}
}
}
// Description of painting onto the plane.
public class PaintDemo {
// Creation of an object for JLabel.
JLabel lbJ;
// Creation of class PaintPanel.
PaintPanel jj;
PaintDemo() {
// JFrame repository is created.
JFrame frJ = new JFrame("Paint Demo");
// Defining the area to be displayed at execution.
frJ.setSize(400, 400);
// End the program on closing of the swing application.
frJ.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// Creation of the panel that will need painting.
jj = new PaintPanel();
frJ.add(jj);
// Showing the display .
frJ.setVisible(true);
}
// Declaring the main method.
public static void main(String args[]) {
// Thread event execution are created in relation to the frame.
SwingUtilities.invokeLater(() -> {
PaintDemo paintDemo = new PaintDemo();
});
}
}

paintComponent() 方法被PaintPanel重写,以管理绘画。它还能使PaintPanel在进行绘画时直接写入组件的输出显示中。
该程序有一个默认的边框格式,使该区域不被声明,并向中间添加面板。面板中的显示内容的大小要填满中间的位置。
总结
在这篇文章中,我们已经学会了如何使用swing框架来完成以下任务。
- 处理事件。
- 在面板中绘画。
- 了解swing如何支持可插拔的外观和感觉。
- 理解MVC的概念。
此外,你还掌握了完全处理swing框架的必要信息。你现在是一个swing geek。
享受Swing框架的设计吧!