JAVA——组件重绘

来源:互联网 发布:百事淘宝跟淘宝的区别 编辑:程序博客网 时间:2024/06/06 02:06

在编写Java GUI程序时,一般会用到组件的重绘,下面简单讲解组件的两种重绘方法。

第一种:JFrame上组件的重绘

该方法主要利用的是JFrame的自由组件布局管理。
在特定位置上进行组件的重绘。

如下图所示,在重绘前,有一个按钮和一个下拉列表框

这里写图片描述

在点击重绘按钮后,将下拉列表框重绘为另一个按钮

这里写图片描述

代码如下:

package test1;import java.awt.BorderLayout;import java.awt.event.ActionEvent;import java.awt.event.ActionListener;import javax.swing.JButton;import javax.swing.JComboBox;import javax.swing.JFrame;import javax.swing.JOptionPane;public class RepaintDemo {    public static void main(String[] args) {        JFrame frame = new JFrame();        frame.setLayout(null);        JComboBox<String> jComboBox = new JComboBox<String>();        jComboBox.setBounds(0, 30, 120, 30);        frame.add(jComboBox,BorderLayout.CENTER);        JButton button = new JButton("重绘");        button.setBounds(0, 0, 120, 30);        frame.add(button,BorderLayout.NORTH);        button.addActionListener(new ActionListener() {            @Override            public void actionPerformed(ActionEvent e) {                frame.remove(jComboBox);                JButton button_R = new JButton("重绘后");                button_R.setBounds(0, 30, 120, 30);                frame.add(button_R);                button_R.repaint(0, 0, 120, 30);                button_R.addActionListener(new ActionListener() {                    @Override                    public void actionPerformed(ActionEvent e) {                        JOptionPane.showMessageDialog(null, "由之前的下拉列表重绘为按钮");                    }                });            }        });        frame.setBounds(100, 100, 100, 100);        frame.setVisible(true);    }}

最主要的重绘代码为:

                //首先,去除要取代的组件                frame.remove(jComboBox);                //其次,创建或添加要重绘的组件                JButton button_R = new JButton("重绘后");                //在窗体上确定要重绘的组件的位置                button_R.setBounds(0, 30, 120, 30);                //将要重绘的组件添加到窗体上                frame.add(button_R);                //最后,重绘组件的指定区域(这一步至关重要)                button_R.repaint(0, 0, 120, 30);

第二种:JPanel上的组件

下面是重回前后的效果:

这里写图片描述

这里写图片描述

package test1;import java.awt.event.ActionEvent;import java.awt.event.ActionListener;import javax.swing.JButton;import javax.swing.JFrame;import javax.swing.JPanel;public class RepaintDemo2 {    public static void main(String[] args) {        JFrame frame = new JFrame();        frame.setLayout(null);        JButton button = new JButton("重绘");        button.setBounds(0, 0, 170, 30);        JPanel panel = new JPanel();        panel.setBounds(0, 30, 160, 30);        JButton demo = new JButton("样本");        panel.add(demo);        JButton button2 = new JButton("重绘前");        panel.add(button2);        frame.add(button);        frame.add(panel);        button.addActionListener(new ActionListener() {            @Override            public void actionPerformed(ActionEvent e) {                panel.remove(button2);                JButton button3 = new JButton("重绘后");                panel.add(button3,1);                panel.updateUI();            }        });        frame.setBounds(100, 100, 175, 100);        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);        frame.setVisible(true);    }}

最主要的重绘代码为:

                //首先,去除要取代的组件                panel.remove(1);//使用组件的索引(索引从0开始)                //panel.remove(button2);//或者直接使用组件对象                //然后,创建或添加要重绘的组件                JButton button3 = new JButton("重绘后");                //将要重绘的组件添加到面板的指定位置                panel.add(button3,1);                //将UI属性重置为当前外观的值(这一步至关重要)                panel.updateUI();

由于面板默认的布局管理器时流式布局,所以上述方案可以顺利解决。
如果想要在面板中使用自由式的布局管理器,就需要在主要代码上加上一句:

               button3.setBounds(x, y, width, heigth);
原创粉丝点击