一、序言
之前花了很多时间,找了很多方法,都没有把Swing插件给安装好,甚至几乎下载了NetBeans来进行尝试安装,利用其来进行Swing的练习。
经过一番努力,终于成功安装好了Swing插件,现在来分享一下我的安装经历。
二、安装Swing插件
1.在help中找到Eclipse marketplace
2.搜索Windowbuilder
3.直接安装即可
没错,就是这么简单,之前说需要找到对应的版本,官方文档上的说明也只有4.2版本之前的说明(可能没有更新,也可能新的这个插件很强,都兼容了)
我的Eclipse版本是4.16
最后,我尝试安装了一下。嗯,完全可以使用!
三、简单演示
1.检测有无安装成功
当你的建新窗口中,可以建新如下的文件的时候,就证明你安装成功了,可以使用Swing插件。
这样进行图形界面设计的时候,直接进行控件拖动就好了。
下面,以JFrame文件,OptionPaneTest进行测试。
2.对话框测试
代码如下
双击按钮控件编写按钮的代码,其余部分自动生成。
package class_2;
import java.awt.BorderLayout;
import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
public class OptionPaneTest extends JFrame {
private JPanel contentPane;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
OptionPaneTest frame = new OptionPaneTest();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the frame.
*/
public OptionPaneTest() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 599, 370);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(null);
JButton btnNewButton = new JButton("例12-2");
btnNewButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
int select;
select=JOptionPane.showConfirmDialog(null, "你喜欢水果吗?");
String msg="没有选择";
switch(select)
{
case JOptionPane.OK_OPTION:
msg="你喜欢水果。";
break;
case JOptionPane.CANCEL_OPTION:
msg="你没有选择哦";
break;
case JOptionPane.NO_OPTION:
msg="你不喜欢水果。";
break;
}
JOptionPane.showMessageDialog(null, msg);
}
});
btnNewButton.setBounds(43, 48, 97, 23);
contentPane.add(btnNewButton);
}
}
3.显示一个错误的信息框
package class_2;
import java.awt.BorderLayout;
import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
public class OptionPaneTest extends JFrame {
private JPanel contentPane;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
OptionPaneTest frame = new OptionPaneTest();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the frame.
*/
public OptionPaneTest() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 599, 370);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(null);
JButton btnNewButton = new JButton("例1");
btnNewButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
int select;
select=JOptionPane.showConfirmDialog(null, "你喜欢水果吗?");
String msg="没有选择";
switch(select)
{
case JOptionPane.OK_OPTION:
msg="你喜欢水果。";
break;
case JOptionPane.CANCEL_OPTION:
msg="你没有选择哦";
break;
case JOptionPane.NO_OPTION:
msg="你不喜欢水果。";
break;
}
JOptionPane.showMessageDialog(null, msg);
}
});
btnNewButton.setBounds(43, 48, 97, 23);
contentPane.add(btnNewButton);
JButton btnNewButton_1 = new JButton("例2");
btnNewButton_1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
JOptionPane.showMessageDialog(null, "不构成三角形","错误信息",JOptionPane.ERROR_MESSAGE);
}
});
btnNewButton_1.setBounds(191, 48, 97, 23);
contentPane.add(btnNewButton_1);
}
}
4.显示一个信息面板
package class_2;
import java.awt.BorderLayout;
import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
public class OptionPaneTest extends JFrame {
private JPanel contentPane;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
OptionPaneTest frame = new OptionPaneTest();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the frame.
*/
public OptionPaneTest() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 599, 370);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(null);
JButton btnNewButton = new JButton("例1");
btnNewButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
int select;
select=JOptionPane.showConfirmDialog(null, "你喜欢水果吗?");
String msg="没有选择";
switch(select)
{
case JOptionPane.OK_OPTION:
msg="你喜欢水果。";
break;
case JOptionPane.CANCEL_OPTION:
msg="你没有选择哦";
break;
case JOptionPane.NO_OPTION:
msg="你不喜欢水果。";
break;
}
JOptionPane.showMessageDialog(null, msg);
}
});
btnNewButton.setBounds(43, 48, 97, 23);
contentPane.add(btnNewButton);
JButton btnNewButton_1 = new JButton("例2");
btnNewButton_1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
JOptionPane.showMessageDialog(null, "不构成三角形","错误信息",JOptionPane.ERROR_MESSAGE);
}
});
btnNewButton_1.setBounds(191, 48, 97, 23);
contentPane.add(btnNewButton_1);
JButton btnNewButton_2 = new JButton("例3");
btnNewButton_2.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
JOptionPane.showConfirmDialog(null, "你是否喜欢编程","爱好选择",JOptionPane.YES_NO_OPTION);
}
});
btnNewButton_2.setBounds(353, 48, 97, 23);
contentPane.add(btnNewButton_2);
}
}
5.显示一个输入信息框
package class_2;
import java.awt.BorderLayout;
import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
public class OptionPaneTest extends JFrame {
private JPanel contentPane;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
OptionPaneTest frame = new OptionPaneTest();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the frame.
*/
public OptionPaneTest() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 599, 370);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(null);
JButton btnNewButton = new JButton("例1");
btnNewButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
int select;
select=JOptionPane.showConfirmDialog(null, "你喜欢水果吗?");
String msg="没有选择";
switch(select)
{
case JOptionPane.OK_OPTION:
msg="你喜欢水果。";
break;
case JOptionPane.CANCEL_OPTION:
msg="你没有选择哦";
break;
case JOptionPane.NO_OPTION:
msg="你不喜欢水果。";
break;
}
JOptionPane.showMessageDialog(null, msg);
}
});
btnNewButton.setBounds(43, 48, 97, 23);
contentPane.add(btnNewButton);
JButton btnNewButton_1 = new JButton("例2");
btnNewButton_1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
JOptionPane.showMessageDialog(null, "不构成三角形","错误信息",JOptionPane.ERROR_MESSAGE);
}
});
btnNewButton_1.setBounds(191, 48, 97, 23);
contentPane.add(btnNewButton_1);
JButton btnNewButton_2 = new JButton("例3");
btnNewButton_2.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
JOptionPane.showConfirmDialog(null, "你是否喜欢编程","爱好选择",JOptionPane.YES_NO_OPTION);
}
});
btnNewButton_2.setBounds(353, 48, 97, 23);
contentPane.add(btnNewButton_2);
JButton btnNewButton_3 = new JButton("例4");
btnNewButton_3.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String msg;
String result;
result=JOptionPane.showInputDialog(null, "你喜欢什么水果?");
msg="你喜欢的水果是: "+result+"哦";
if(result!=null && !result.trim().equals(""))
{
msg="你喜欢的水果是: "+result+"哦";
}
else
{
msg="你喜欢的水果没有选择哦";
}
JOptionPane.showMessageDialog(null, msg);
}
});
btnNewButton_3.setBounds(43, 122, 97, 23);
contentPane.add(btnNewButton_3);
}
}
6.显示警告对话框
package class_2;
import java.awt.BorderLayout;
import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
public class OptionPaneTest extends JFrame {
private JPanel contentPane;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
OptionPaneTest frame = new OptionPaneTest();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the frame.
*/
public OptionPaneTest() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 599, 370);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(null);
JButton btnNewButton = new JButton("例1");
btnNewButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
int select;
select=JOptionPane.showConfirmDialog(null, "你喜欢水果吗?");
String msg="没有选择";
switch(select)
{
case JOptionPane.OK_OPTION:
msg="你喜欢水果。";
break;
case JOptionPane.CANCEL_OPTION:
msg="你没有选择哦";
break;
case JOptionPane.NO_OPTION:
msg="你不喜欢水果。";
break;
}
JOptionPane.showMessageDialog(null, msg);
}
});
btnNewButton.setBounds(43, 48, 97, 23);
contentPane.add(btnNewButton);
JButton btnNewButton_1 = new JButton("例2");
btnNewButton_1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
JOptionPane.showMessageDialog(null, "不构成三角形","错误信息",JOptionPane.ERROR_MESSAGE);
}
});
btnNewButton_1.setBounds(191, 48, 97, 23);
contentPane.add(btnNewButton_1);
JButton btnNewButton_2 = new JButton("例3");
btnNewButton_2.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
JOptionPane.showConfirmDialog(null, "你是否喜欢编程","爱好选择",JOptionPane.YES_NO_OPTION);
}
});
btnNewButton_2.setBounds(353, 48, 97, 23);
contentPane.add(btnNewButton_2);
JButton btnNewButton_3 = new JButton("例4");
btnNewButton_3.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String msg;
String result;
result=JOptionPane.showInputDialog(null, "你喜欢什么水果?");
msg="你喜欢的水果是: "+result+"哦";
if(result!=null && !result.trim().equals(""))
{
msg="你喜欢的水果是: "+result+"哦";
}
else
{
msg="你喜欢的水果没有选择哦";
}
JOptionPane.showMessageDialog(null, msg);
}
});
btnNewButton_3.setBounds(43, 122, 97, 23);
contentPane.add(btnNewButton_3);
JButton btnNewButton_4 = new JButton("例5");
btnNewButton_4.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
Object[] options= {"是","取消"};
JOptionPane.showInternalOptionDialog(null, "单击是否继续", "注意", JOptionPane.DEFAULT_OPTION,
JOptionPane.WARNING_MESSAGE, null, options, options[0]);
}
});
btnNewButton_4.setBounds(191, 122, 97, 23);
contentPane.add(btnNewButton_4);
}
}
7.显示一个要求用户选择图形类别的对话框
package class_2;
import java.awt.BorderLayout;
import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
public class OptionPaneTest extends JFrame {
private JPanel contentPane;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
OptionPaneTest frame = new OptionPaneTest();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the frame.
*/
public OptionPaneTest() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 599, 370);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(null);
JButton btnNewButton = new JButton("例1");
btnNewButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
int select;
select=JOptionPane.showConfirmDialog(null, "你喜欢水果吗?");
String msg="没有选择";
switch(select)
{
case JOptionPane.OK_OPTION:
msg="你喜欢水果。";
break;
case JOptionPane.CANCEL_OPTION:
msg="你没有选择哦";
break;
case JOptionPane.NO_OPTION:
msg="你不喜欢水果。";
break;
}
JOptionPane.showMessageDialog(null, msg);
}
});
btnNewButton.setBounds(43, 48, 97, 23);
contentPane.add(btnNewButton);
JButton btnNewButton_1 = new JButton("例2");
btnNewButton_1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
JOptionPane.showMessageDialog(null, "不构成三角形","错误信息",JOptionPane.ERROR_MESSAGE);
}
});
btnNewButton_1.setBounds(191, 48, 97, 23);
contentPane.add(btnNewButton_1);
JButton btnNewButton_2 = new JButton("例3");
btnNewButton_2.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
JOptionPane.showConfirmDialog(null, "你是否喜欢编程","爱好选择",JOptionPane.YES_NO_OPTION);
}
});
btnNewButton_2.setBounds(353, 48, 97, 23);
contentPane.add(btnNewButton_2);
JButton btnNewButton_3 = new JButton("例4");
btnNewButton_3.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String msg;
String result;
result=JOptionPane.showInputDialog(null, "你喜欢什么水果?");
msg="你喜欢的水果是: "+result+"哦";
if(result!=null && !result.trim().equals(""))
{
msg="你喜欢的水果是: "+result+"哦";
}
else
{
msg="你喜欢的水果没有选择哦";
}
JOptionPane.showMessageDialog(null, msg);
}
});
btnNewButton_3.setBounds(43, 122, 97, 23);
contentPane.add(btnNewButton_3);
JButton btnNewButton_4 = new JButton("例5");
btnNewButton_4.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
Object[] options= {"是","取消"};
JOptionPane.showInternalOptionDialog(null, "单击是否继续", "注意", JOptionPane.DEFAULT_OPTION,
JOptionPane.WARNING_MESSAGE, null, options, options[0]);
}
});
btnNewButton_4.setBounds(191, 122, 97, 23);
contentPane.add(btnNewButton_4);
JButton btnNewButton_5 = new JButton("例6");
btnNewButton_5.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
Object[] possibleValues= {"圆形","三角形","矩形"};
Object selectedValue=JOptionPane.showInputDialog(null, "请选择图形类别:", "数据输入",
JOptionPane.INFORMATION_MESSAGE, null, possibleValues, possibleValues[0]);
}
});
btnNewButton_5.setBounds(353, 122, 97, 23);
contentPane.add(btnNewButton_5);
}
}
8.显示一个内部信息框
package class_2;
import java.awt.BorderLayout;
import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JInternalFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
public class OptionPaneTest extends JFrame {
private JPanel contentPane;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
OptionPaneTest frame = new OptionPaneTest();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the frame.
*/
public OptionPaneTest() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 599, 370);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(null);
JButton btnNewButton = new JButton("例1");
btnNewButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
int select;
select=JOptionPane.showConfirmDialog(null, "你喜欢水果吗?");
String msg="没有选择";
switch(select)
{
case JOptionPane.OK_OPTION:
msg="你喜欢水果。";
break;
case JOptionPane.CANCEL_OPTION:
msg="你没有选择哦";
break;
case JOptionPane.NO_OPTION:
msg="你不喜欢水果。";
break;
}
JOptionPane.showMessageDialog(null, msg);
}
});
btnNewButton.setBounds(43, 48, 97, 23);
contentPane.add(btnNewButton);
JButton btnNewButton_1 = new JButton("例2");
btnNewButton_1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
JOptionPane.showMessageDialog(null, "不构成三角形","错误信息",JOptionPane.ERROR_MESSAGE);
}
});
btnNewButton_1.setBounds(191, 48, 97, 23);
contentPane.add(btnNewButton_1);
JButton btnNewButton_2 = new JButton("例3");
btnNewButton_2.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
JOptionPane.showConfirmDialog(null, "你是否喜欢编程","爱好选择",JOptionPane.YES_NO_OPTION);
}
});
btnNewButton_2.setBounds(353, 48, 97, 23);
contentPane.add(btnNewButton_2);
JButton btnNewButton_3 = new JButton("例4");
btnNewButton_3.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String msg;
String result;
result=JOptionPane.showInputDialog(null, "你喜欢什么水果?");
msg="你喜欢的水果是: "+result+"哦";
if(result!=null && !result.trim().equals(""))
{
msg="你喜欢的水果是: "+result+"哦";
}
else
{
msg="你喜欢的水果没有选择哦";
}
JOptionPane.showMessageDialog(null, msg);
}
});
btnNewButton_3.setBounds(43, 122, 97, 23);
contentPane.add(btnNewButton_3);
JButton btnNewButton_4 = new JButton("例5");
btnNewButton_4.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
Object[] options= {"是","取消"};
JOptionPane.showInternalOptionDialog(null, "单击是否继续", "注意", JOptionPane.DEFAULT_OPTION,
JOptionPane.WARNING_MESSAGE, null, options, options[0]);
}
});
btnNewButton_4.setBounds(191, 122, 97, 23);
contentPane.add(btnNewButton_4);
JButton btnNewButton_5 = new JButton("例6");
btnNewButton_5.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
Object[] possibleValues= {"圆形","三角形","矩形"};
Object selectedValue=JOptionPane.showInputDialog(null, "请选择图形类别:", "数据输入",
JOptionPane.INFORMATION_MESSAGE, null, possibleValues, possibleValues[0]);
}
});
btnNewButton_5.setBounds(353, 122, 97, 23);
contentPane.add(btnNewButton_5);
JButton btnNewButton_6 = new JButton("例7");
btnNewButton_6.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
JFrame frame=new JFrame();
frame.setSize(300, 400);
frame.setVisible(true);
JInternalFrame jf=new JInternalFrame();
frame.add(jf);
jf.setVisible(true);
JOptionPane.showInternalMessageDialog(jf, "你是否爱唱歌","爱好选择",JOptionPane.INFORMATION_MESSAGE);
}
});
btnNewButton_6.setBounds(43, 193, 97, 23);
contentPane.add(btnNewButton_6);
}
}