重新初始化jframe

时间:2015-02-08 23:55:30

标签: java swing initialization jpanel observer-pattern

我的主框架上有一个带有一些按钮的侧面板,中央面板用于显示从侧面板及其子面板上的按钮生成的表格和数据 在开始时我的中央面板是空白的,我希望它在生成任何数据之前每次单击按钮后始终返回其初始状态(空白)

我使用了某种观察者模式(我没有那么有经验)但我的问题是中心面板必须在点击面板上的某些按钮后显示数据,这些按钮也需要点击生成之前的侧面板

我试图在下面的类上创建一个可执行的例子,我的真实应用程序在中央面板上显示一些表格,我通过观察者的更新方法发送模型

希望它对你有用,我希望你能真正帮助我

1 - 主框架:

package tests;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import javax.swing.JFrame; 
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;


public class MainFrame extends JFrame implements MyObserver{

private SidePanel sidePanel;
private JPanel centralPanel;
private JFrame frame;
private JLabel title;

public MainFrame(){
    frame = new JFrame("TEST");
    frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
    frame.setLayout(new BorderLayout());

    sidePanel = new SidePanel();
    sidePanel.addObserver(this);
    centralPanel = new JPanel();
    title = new JLabel();

    initialise(0);

    frame.pack();
    frame.setVisible(true);
    frame.setLocationRelativeTo(null);
}

private void initialise(int i) {
    if( i == 0){
        centralPanel.setPreferredSize(new Dimension(400,300));
        centralPanel.setBackground(Color.green);
        title.setText("GREEN");

        centralPanel.add(title, BorderLayout.CENTER);

        frame.add(sidePanel, BorderLayout.WEST);
        frame.add(centralPanel, BorderLayout.CENTER);
    }
}

public static void main(String[] args) {
    SwingUtilities.invokeLater(new Runnable() {

        @Override
        public void run() {
            new MainFrame();
        }
    });
}

@Override
public void update(int color) {
    if(color == 0){
        centralPanel.setBackground(Color.yellow);
        title.setText("YELLOW");
    }else{
        centralPanel.setBackground(Color.pink);
        title.setText("PINK");
    }
}
}

2 - 侧面板

package tests;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JPanel;

public class SidePanel extends JPanel implements MyObserver,MyObservable{
private JPanel panel;
private JButton test;
private MyObserver observer;
private ButtonPanel buttonPanel;

public SidePanel(){
    panel = new JPanel();
    panel.setPreferredSize(new Dimension(140, 300));
    panel.setBackground(Color.blue);
    panel.setLayout(new BoxLayout(panel, 0));


    test = new JButton("Lunch buttons");
    test.setPreferredSize(new Dimension(80,30));
    buttonPanel = new ButtonPanel();
    buttonPanel.addObserver(this);
    test.addActionListener(new ActionListener() {

        @Override
        public void actionPerformed(ActionEvent arg0) {
            buttonPanel.setVisible(true);

        }
    });

    panel.add(Box.createVerticalGlue());
    panel.add(test);
    panel.add(Box.createVerticalGlue());

    panel.setVisible(true);
    this.add(panel, BorderLayout.CENTER);
}

@Override
public void addObserver(MyObserver obs) {
    this.observer = obs;
}

@Override
public void updateObserver(MyObserver obs, int color) {
    obs.update(color);
}

@Override
public void update(int color) {
    updateObserver(observer, color);
}


}

3 - 按钮面板,通常是要在中央面板上显示的任何数据的来源

package tests;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JPanel;

public class ButtonPanel extends JDialog implements MyObservable{
private JButton yellow;
private JButton orange;
private JPanel panel;
private MyObserver observer;

public ButtonPanel(){
    panel = new JPanel();
    panel.setPreferredSize(new Dimension(300, 40));
    panel.setLayout(new FlowLayout());
    this.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
    this.setContentPane(panel);

    yellow = new JButton("YELLOW");
    yellow.setPreferredSize(new Dimension(100,30));
    yellow.addActionListener(new ActionListener() {

        @Override
        public void actionPerformed(ActionEvent arg0) {
            updateObserver(observer, 0);
        }
    });

    orange = new JButton("ORANGE");
    orange.setPreferredSize(new Dimension(100,30));
    orange.addActionListener(new ActionListener() {

        @Override
        public void actionPerformed(ActionEvent e) {
            updateObserver(observer, 1);
        }
    });

    panel.add(yellow);
    panel.add(orange);

    pack();
    setLocationRelativeTo(null);
}

@Override
public void addObserver(MyObserver obs) {
    this.observer = obs;
}

@Override
public void updateObserver(MyObserver obs, int color) {
    obs.update(color);
}
}

最后,定制的观察者和可观察的界面,请注意在真正的应用程序中我使用的是表格模型而不仅仅是一个整体 - 我不确定这是一个好方法 -

package tests;

public interface MyObservable {
public void addObserver(MyObserver obs);
public void updateObserver(MyObserver obs, int color);
} 

package tests;

public interface MyObserver {
public void update(int color);
}

1 个答案:

答案 0 :(得分:0)

已更改答案:

SidePanel.java添加:

private MainFrame frame;

然后让构造函数将MyFrame对象作为参数。这样做:

public SidePanel(MainFrame frame){
    this.frame = frame;
    //rest not changed
    //
}

actionPerformed()按钮的test更改为:

test.addActionListener(new ActionListener() {
    @Override
    public void actionPerformed(ActionEvent arg0) {
        buttonPanel.setVisible(true);
        frame.initialise(0);    // this line is added
    }
});

MainFrame.java

sidePanel = new SidePanel();更改为sidePanel = new SidePanel(this);

private void initialise(int i)更改为public void initialise(int i)

这就是你想要实现的目标。