白日梦想jun 2016-06-05 08:18 采纳率: 100%
浏览 1637
已采纳

Java Swing Jframe中左边组件固定,右边的组件刷新不了,很急,谢谢大神们帮我看看

package swingbg.test;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.GridLayout;
import java.awt.Image;
import java.awt.Insets;
import java.awt.Label;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.text.SimpleDateFormat;
import java.util.Date;

import javax.swing.BorderFactory;
import javax.swing.DefaultComboBoxModel;
import javax.swing.DefaultListModel;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFormattedTextField;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.JToolBar;
import javax.swing.ScrollPaneConstants;

public class SystemFunctions implements ActionListener {

private JFrame functionJf = new JFrame("My Diary");
private JPanel toolBar;
private JPanel panel;  //右边的每一个panel
private JButton btWriter = new JButton("写日记");
private JButton btFinder = new JButton("查找日记");
private JButton btSetter = new JButton("系统设置");
private JButton btExit = new JButton("退出");
private String userName;

public void show(){
    functionJf.setVisible(true);
    functionJf.setTitle(userName);
    functionJf.setSize(800, 800);
    functionJf.setLocationRelativeTo(null); // 设置窗体在屏幕中央显示
    functionJf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public SystemFunctions() {
    functionJf.add(buildToolBar(), BorderLayout.WEST);
    functionJf.add(panelWriter("欢迎"+userName+"登录"), BorderLayout.CENTER);

}

//左边工具类为一个Jpanel
public JPanel buildToolBar() {
    if (toolBar == null) {
        toolBar = new JPanel();
        toolBar.setLayout(new GridLayout(5, 1));

        toolBar.add(btWriter);
        toolBar.add(btFinder);
        toolBar.add(btSetter);
        toolBar.add(btExit);
        toolBar.setVisible(true);
        toolBar.setEnabled(false); // 设置toolBar的可移动性
        toolBar.setBackground(new Color(0, 255, 0));
        toolBar.setPreferredSize(new Dimension(150, 100));

        btWriter.setIcon(new ImageIcon("ico\\李白.gif"));
        btWriter.setBorderPainted(false);
        btWriter.setContentAreaFilled(false);
        btWriter.setFocusable(true);
        btWriter.setMargin(new Insets(3, 3, 3, 3));


        btFinder.setIcon(new ImageIcon("ico\\李白.gif"));
        btFinder.setBorderPainted(false);
        btFinder.setContentAreaFilled(false);
        btFinder.setFocusable(true);
        btFinder.setMargin(new Insets(3, 3, 3, 3));


        btSetter.setIcon(new ImageIcon("ico\\李白.gif"));
        btSetter.setBorderPainted(false);
        btSetter.setContentAreaFilled(false);
        btSetter.setFocusable(true);
        btSetter.setMargin(new Insets(3, 3, 3, 3));

        btExit.setIcon(new ImageIcon("ico\\李白.gif"));
        btExit.setBorderPainted(false);
        btExit.setContentAreaFilled(false);
        btExit.setFocusable(true);
        btExit.setMargin(new Insets(3, 3, 3, 3));
        btExit.addActionListener(this);

        btWriter.addActionListener(this);
        btWriter.requestFocus();                    //一开始就获得焦点
        btFinder.addActionListener(this);
        btSetter.addActionListener(this);
        btExit.addActionListener(this);
    }
    return toolBar;
}

//右边切换面板是每个Jpanel
public JPanel panelWriter(String name){
    if (panel == null) {
        this.panel = new JPanel(null);
        JButton btSure = new JButton("保存");
        JButton btCancel = new JButton("取消");
        //天气
        String weaNames[] = {"Sunny","Rainy","Cloudy"}; 
        JLabel weaLabel = new JLabel("天气:");
        DefaultComboBoxModel<String> weathModel = new DefaultComboBoxModel<>(weaNames);
        JComboBox<String> weather = new JComboBox<>(weathModel);
        //心情
        String FeelNames[] = {"Happy","Sad","Exiciting"}; 
        JLabel feelLabel = new JLabel("心情:");
        DefaultComboBoxModel<String> feelModel = new DefaultComboBoxModel<>(FeelNames);
        JComboBox<String> feel = new JComboBox<>(feelModel);
        // 日期
        JLabel dateLabel = new JLabel("日期:");
        JFormattedTextField date;
        SimpleDateFormat formatDate = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            // 要求采用严格的日期格式语法
        formatDate.setLenient(false);
        date = new JFormattedTextField(formatDate);
        date.setValue(new Date());
        //标题
        JLabel titleLabel = new JLabel("标题:");
        JTextField title = new JTextField(5);
        //日记内容
        JLabel contendLabel = new JLabel("内容:");
        JTextArea contend = new JTextArea(5, 5);


        weaLabel.setBounds(150, 80, 200, 35);
        weather.setBounds(250, 80, 200, 40);

        feelLabel.setBounds(150, 150, 200, 35);
        feel.setBounds(250, 150, 200, 40);

        dateLabel.setBounds(150, 220, 200, 35);
        date.setBounds(250, 220, 200, 40);

        titleLabel.setBounds(150, 290, 200, 35);
        title.setBounds(250, 290, 200, 40);

        contendLabel.setBounds(150, 360, 200, 35);
        contend.setBounds(250, 360, 300, 200);

        btSure.setBounds(250, 650, 80, 40);
        btCancel.setBounds(450, 650, 80, 40);

        btSure.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent arg0) {
                // TODO Auto-generated method stub
                //保存日记对象到数据库
                //清空所有列表框
            }
        });
        btCancel.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                // TODO Auto-generated method stub
                //清空清空所有列表框
            }
        });
        panel.add(weaLabel);
        panel.add(weather);
        panel.add(feelLabel);
        panel.add(feel);
        panel.add(dateLabel);
        panel.add(date);
        panel.add(titleLabel);
        panel.add(title);
        panel.add(contendLabel);
        panel.add(contend);
        panel.add(btSure);
        panel.add(btCancel);
        panel.setVisible(true);

        panel.setBorder(BorderFactory.createTitledBorder(name));
    }
    return panel;
}

public JPanel panelFinder(String name){
    if (panel == null) {
        panel = new JPanel(null);
        //日记列表->日记对象
        DefaultListModel<Object> listModel = new DefaultListModel<>();
        JList<Object> daList = new JList<>(listModel);
        JScrollPane sp = new JScrollPane(daList);
        //详情区
        //天气
        String weaNames[] = {"Sunny","Rainy","Cloudy"}; 
        JLabel weaLabel = new JLabel("天气:");
        DefaultComboBoxModel<String> weathModel = new DefaultComboBoxModel<>(weaNames);
        JComboBox<String> weather = new JComboBox<>(weathModel);
        //心情
        String FeelNames[] = {"Happy","Sad","Exiciting"}; 
        JLabel feelLabel = new JLabel("心情:");
        DefaultComboBoxModel<String> feelModel = new DefaultComboBoxModel<>(FeelNames);
        JComboBox<String> feel = new JComboBox<>(feelModel);
        // 日期
        JLabel dateLabel = new JLabel("日期:");
        JFormattedTextField date;
        SimpleDateFormat formatDate = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            // 要求采用严格的日期格式语法
        formatDate.setLenient(false);
        date = new JFormattedTextField(formatDate);
        date.setValue(new Date());
        //标题
        JLabel titleLabel = new JLabel("标题:");
        JTextField Ftitle = new JTextField(5);
        //日记内容
        JLabel contendLabel = new JLabel("内容:");
        JTextArea contend = new JTextArea(5, 5);

        sp.setBounds(20, 20, 100, 100);
        weaLabel.setBounds(150, 80, 200, 35);
        weather.setBounds(250, 80, 200, 40);

        feelLabel.setBounds(150, 150, 200, 35);
        feel.setBounds(250, 150, 200, 40);

        dateLabel.setBounds(150, 220, 200, 35);
        date.setBounds(250, 220, 200, 40);

        titleLabel.setBounds(150, 290, 200, 35);
        Ftitle.setBounds(250, 290, 200, 40);

        contendLabel.setBounds(150, 360, 200, 35);
        contend.setBounds(250, 360, 300, 200);


        panel.add(sp);
        panel.add(weaLabel);
        panel.add(weather);
        panel.add(feelLabel);
        panel.add(feel);
        panel.add(dateLabel);
        panel.add(date);
        panel.add(titleLabel);
        panel.add(Ftitle);
        panel.add(contendLabel);
        panel.add(contend);
        panel.setBorder(BorderFactory.createTitledBorder(name));
    }
    return panel;

}
public JPanel panelSetter(String name){
    return panel;
}


@Override
public void actionPerformed(ActionEvent e) {
    // TODO Auto-generated method stub

    if(e.getSource() == btWriter){
        SystemFunctions cp = SystemFunctions.this;
        cp.panel.removeAll();
        cp.panel = panelWriter(userName);
        cp.panel.validate();
    }
    if(e.getSource() == btFinder){
        SystemFunctions cp = SystemFunctions.this;
        cp.panel.removeAll();
        cp.panel = panelFinder(userName);     //这里切换不过来
        cp.panel.validate();
    }
    if(e.getSource() == btSetter){
        SystemFunctions cp = SystemFunctions.this;
        cp.panel.removeAll();
        cp.panel =panelSetter(userName);
        cp.panel.validate();
    }
    if(e.getSource() == btExit){
        new Start().show();
        SystemFunctions cp = SystemFunctions.this;
        cp.functionJf.setVisible(false);
    }
}

public static void main(String[] args) {
    SystemFunctions cut = new SystemFunctions();
    cut.show();
}

}


  • 写回答

1条回答 默认 最新

  • _1_1_7_ 2016-06-06 09:11
    关注

    private JPanel panel = new JPanel(null); //右边的每一个panel

    //右边切换面板是每个Jpanel
    public JPanel panelWriter(String name){
    // if (panel == null) {
    // this.panel = new JPanel(null);

    关键是这个多余的判断 if (panel == null) 导致后面代码不执行了,当然就变化不了了。
    后面if (panel == null) 也注释掉就可以了

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥50 安装pyaudiokits失败
  • ¥15 计组这些题应该咋做呀
  • ¥60 更换迈创SOL6M4AE卡的时候,驱动要重新装才能使用,怎么解决?
  • ¥15 让node服务器有自动加载文件的功能
  • ¥15 jmeter脚本回放有的是对的有的是错的
  • ¥15 r语言蛋白组学相关问题
  • ¥15 Python时间序列如何拟合疏系数模型
  • ¥15 求学软件的前人们指明方向🥺
  • ¥50 如何增强飞上天的树莓派的热点信号强度,以使得笔记本可以在地面实现远程桌面连接
  • ¥20 双层网络上信息-疾病传播