Swing 是一个为Java设计的GUI工具包,虽然现在基本上用的人已经不多了,但还是想把学习过程中实现需求的各种方法记录下来,希望能帮助到有需要的人。
Swing的简单用法可参照菜鸟教程:https://www.runoob.com/w3cnote/java-swing-demo-intro.html
下面是我个人满足项目需求的一点经验。
1.设置背景图片
对于应用程序来说,一个单调的窗口背景显然不够美观,为其添加一个背景图片就很有必要。下面是代码。
ImageIcon background = new ImageIcon("src/bg.jpg"); //加载背景图
JLabel bkLabel = new JLabel(background);
bkLabel.setBounds(0, 0,background.getIconWidth(), background.getIconHeight());
this.setSize(background.getIconWidth(), background.getIconHeight());
this.getLayeredPane().add(bkLabel,new Integer(Integer.MIN_VALUE));
JPanel ctPanel = (JPanel)this.getContentPane();
ctPanel.setOpaque(false);
因为我把这段代码写进了构造方法里面,所以直接用了this,可以根据需要进行修改。最好不要写进构造方法里面,而是为窗口写一个draw方法,将添加控件的内容写进draw方法里面。
2.鼠标移到按钮上改变图标
Cursor myCursor=new Cursor(Cursor.HAND_CURSOR);
button.setCursor(myCursor); //button是按钮变量名
3.点击按钮后隐藏当前窗口并显示新窗口
/*
*窗口1
*/
public class Window1 extends JFrame{
Window1(){
this.setTitle("title"); //窗口名称
this.setSize(1280,720); //窗口大小
this.setResizable(false); //窗口大小是否可以改变
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setLocationRelativeTo(null);
this.setAlwaysOnTop(true); //总在最前
this.setLayout(null);
JButton Button = new JButton("按钮");
this.add(Button);
ButtonListener listener = new ButtonListener();
Button.addActionListener(listener); //点击按钮后要执行的操作
BorrowButton.addActionListener(this::actionPerformed); //点击按钮后隐藏当前窗口
this.setVisible(true);
}
//隐藏当前窗口
private void actionPerformed(ActionEvent e) {
this.setVisible(false);
}
}
/*
*点击按钮后显示的窗口2
*/
class ButtonListener implements ActionListener{
@Override
public void actionPerformed(ActionEvent e){
JFrame newFrame =new JFrame();
newFrame.setTitle("窗口2");
newFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
newFrame.setSize(1280,720);
newFrame.setResizable(false);
newFrame.setLocationRelativeTo(null);
newFrame.setAlwaysOnTop(true);
newFrame.setLayout(null);
newFrame.setVisible(true);
}
4.点击按钮后退出
虽然可以直接按叉关掉程序,但还是设置一个退出按钮比较好。
class ExitButtonListener extends ButtonListener {
@Override
public void actionPerformed(ActionEvent e) {
System.exit(0);
}
}
5.添加可下拉的复选框及获取选择内容
//创建复选框
static String[] option = {"选项1","选项2","选项3","选项4"};
static JComboBox comboBox = new JComboBox(option);
comboBox.setBounds(300,293,120,30); //设置复选框位置及大小
newFrame.add(comboBox);
//获取所选择的内容
comboBox.getSelectIndex(); //获取下标,从0开始
comboBox.getSelectItem(); //直接获取所选择的内容
6.在文本域中追加显示内容及刷新文本域
//创建文本域
private static JTextArea display = new JTextArea();
public void draw(){
this.add(display);
}
//追加显示内容
public void appendJTextArea(String info) {
display.append(info+"\n");
display.paintImmediately(processInformation.getBounds());
}
//刷新文本域
public void refresh(){
display.setText("新内容");
}
追加显示内容用的是append方法,不会清除已显示的内容;刷新用的是setText方法,相当于重头开始显示内容。
通过这两个方法相结合便可以达到实时更新的效果,用setText来设置不变的内容,再用append显示更新后的内容。但是需要创建一个新的线程,否则有可能卡住不显示内容然后一下子显示一大片。创建新线程的代码如下。
public void appendResultJTextArea(String info) {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
display.append(info);
}
});
}
为了满足这些需求,我也是看了很多篇文章,甚至有时还找不到答案。所以在这里把自己实现的一些需求写出来,希望这篇文章能对需要用Java做界面的朋友起到一定的帮助。