2 zy10601 zy10601 于 2015.06.07 23:07 提问

Java GUI怎样实现刷新或切换页面的效果

请教一下大概思路。如果有例子就更好啦
具体就是更改数据后刷新页面或者点击按钮跳转到其他页面

3个回答

tongyi55555
tongyi55555   2015.06.08 09:38

你是做网页的么?跳转到其他页面???

zy10601
zy10601 不是做网页,就是一个软件界面,点击不同的按钮可以显示不同页面内容那种
2 年多之前 回复
tongyi55555
tongyi55555   2015.06.08 13:26

这样吗?

import java.awt.BorderLayout;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextPane;

public class ChangeJPanelTest extends JFrame {

    public ChangeJPanelTest() {
        GridBagLayout gridBagLayout = new GridBagLayout();
        gridBagLayout.columnWidths = new int[] { 0, 0, 0 };
        gridBagLayout.rowHeights = new int[] { 0, 0, 0 };
        gridBagLayout.columnWeights = new double[] { 0.5, 0.5, Double.MIN_VALUE };
        gridBagLayout.rowWeights = new double[] { 1.0, 0.0, Double.MIN_VALUE };
        getContentPane().setLayout(gridBagLayout);

        final GridBagConstraints gbc_panel = new GridBagConstraints();
        gbc_panel.gridwidth = 2;
        gbc_panel.fill = GridBagConstraints.BOTH;
        gbc_panel.insets = new Insets(0, 0, 5, 0);
        gbc_panel.gridx = 0;
        gbc_panel.gridy = 0;

        JButton btn_Panel1 = new JButton("显示杜甫");
        btn_Panel1.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                getContentPane().add(new PanelDuFu(), gbc_panel);
                getContentPane().validate();
            }
        });
        GridBagConstraints gbc_btn_Panel1 = new GridBagConstraints();
        gbc_btn_Panel1.insets = new Insets(5, 0, 5, 0);
        gbc_btn_Panel1.gridx = 0;
        gbc_btn_Panel1.gridy = 1;
        getContentPane().add(btn_Panel1, gbc_btn_Panel1);

        JButton btn_Panel2 = new JButton("显示李白");
        btn_Panel2.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                getContentPane().add(new PanelLiBai(), gbc_panel);
                getContentPane().validate();
            }
        });
        GridBagConstraints gbc_btn_Panel2 = new GridBagConstraints();
        gbc_btn_Panel2.insets = new Insets(5, 0, 5, 0);
        gbc_btn_Panel2.gridx = 1;
        gbc_btn_Panel2.gridy = 1;
        getContentPane().add(btn_Panel2, gbc_btn_Panel2);

    }

    public static void main(String[] args) {
        ChangeJPanelTest frm = new ChangeJPanelTest();
        frm.setSize(500, 500);
        frm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frm.setLocationRelativeTo(null);
        frm.setVisible(true);
    }

    private class PanelDuFu extends JPanel {
        public PanelDuFu() {
            this.setLayout(new BorderLayout(0, 0));

            JTextPane textPane = new JTextPane();
            this.add(textPane);
            JLabel lbl = new JLabel("杜甫");
            this.add(lbl, BorderLayout.SOUTH);
            textPane.setText("春夜喜雨\r\n好雨知时节,\r\n当春乃发生。\r\n随风潜入夜,\r\n润物细无声。\r\n野径云俱黑,\r\n江船火独明。\r\n晓看红湿处,\r\n花重锦官城。\r\n");
        }
    }

    private class PanelLiBai extends JPanel {
        public PanelLiBai() {
            this.setLayout(new BorderLayout(0, 0));

            JTextPane textPane = new JTextPane();
            this.add(textPane);
            JLabel lbl = new JLabel("李白");
            this.add(lbl, BorderLayout.NORTH);
            textPane.setText("望庐山瀑布\r\n日照香炉生紫烟,\r\n遥看瀑布挂前川。\r\n飞流直下三千尺,\r\n疑是银河落九天。\r\n");
        }
    }
}

zy10601
zy10601 对,就是这样。非常感谢
2 年多之前 回复
wangyangzhizhou
wangyangzhizhou   2015.06.09 12:46

建议你去读一下优秀项目的源码,例如国内做的很好的yoyo播放器

Csdn user default icon
上传中...
上传图片
插入图片