⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 mainframe.java

📁 应付作业+练习OOP做的东西
💻 JAVA
字号:
package userinterface;

import java.awt.BorderLayout;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;

import javax.swing.DefaultListModel;
import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.ListSelectionModel;

import model.DataManager;
import dataprocess.Result;
import datasource.FileSource;
import java.awt.Dimension;
import javax.swing.JMenuBar;
import javax.swing.JMenu;
import java.awt.event.KeyEvent;
import javax.swing.JMenuItem;

public class MainFrame extends JFrame implements DistributionDisplayer {
    
	private static final long serialVersionUID = 1L;

	private JPanel jContentPane = null;

	private JList datalist = null;

	private JButton filebutton = null;

	private JButton manualbutton = null;

	private JButton paintbutton = null;

	private JPanel databuttonpanel = null;

	private JButton delete = null;

	private JButton modify = null;

	private JButton exit = null;
	public void setManager(DataManager manager){
		this.manager=manager;
		manager.setDisplayer(this);
	}
	public DataManager getManager(){
		return this.manager;
	}
	

	public void drawDistribution(Result result) {
		ResultPainter resultPainter=new ResultPainter(this);
		resultPainter.setSize(new Dimension(46, 33));
		resultPainter.drawDistribution(result);
		

	}

	public void renewData(double[] newdata) {
		Double[] data=new Double[newdata.length];
		for(int i=0;i<newdata.length;i++){
			data[i]=new Double(newdata[i]);
		}
		datalist.setListData(data);
		datalist.revalidate();

	}

	/**
	 * This is the default constructor
	 */
	public MainFrame() {
		super();
		initialize();
	}

	/**
	 * This method initializes this
	 * 
	 * @return void
	 */
	private void initialize() {
		this.setSize(377, 228);
		this.setJMenuBar(getJJMenuBar());
		this.setResizable(false);
		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		this.setContentPane(getJContentPane());
		this.setTitle("分布数据处理器0.1beta");
		this.setVisible(true);
		this.about=new AboutDialog(this);
		this.help=new HelpDialog(this);
		
		
	}

	/**
	 * This method initializes jContentPane
	 * 
	 * @return javax.swing.JPanel
	 */
	private JPanel getJContentPane() {
		if (jContentPane == null) {
			jContentPane = new JPanel();
			jContentPane.setLayout(new BorderLayout());
			jContentPane.add(getDatabuttonpanel(), BorderLayout.EAST);
			jContentPane.add(getScrollPane(), BorderLayout.CENTER);
		}
		return jContentPane;
	}

	/**
	 * This method initializes datalist	
	 * 	
	 * @return javax.swing.JList	
	 */
	private JList getDatalist() {
		if (datalist == null) {
			datalist = new JList();
			
			datalist.setModel(new DefaultListModel());
			
		
			datalist.setSelectionMode(ListSelectionModel.SINGLE_INTERVAL_SELECTION);
			
			
		}
		return datalist;
	}

	/**
	 * This method initializes filebutton	
	 * 	
	 * @return javax.swing.JButton	
	 */
	private JButton getFilebutton() {
		if (filebutton == null) {
			filebutton = new JButton();
			filebutton.setText("从文件添加数据");
			filebutton.addActionListener(new ActionListener(){

				public void actionPerformed(ActionEvent arg0) {
					if(manager==null)return;//什么都不做
					JFileChooser filechooser=new JFileChooser();
					int val=filechooser.showOpenDialog(getFilebutton());
					if(val == JFileChooser.APPROVE_OPTION){
						File target=filechooser.getSelectedFile();
						FileSource source=new FileSource(target);
						source.sendData(manager);
						
					}
				}
				
			});
		}
		return filebutton;
	}

	/**
	 * This method initializes manualbutton	
	 * 	
	 * @return javax.swing.JButton	
	 */
	private JButton getManualbutton() {
		if (manualbutton == null) {
			manualbutton = new JButton();
			manualbutton.setText("手动输入数据");
			manualbutton.addActionListener(new ActionListener(){

				public void actionPerformed(ActionEvent arg0) {
					ManualDataSource src=new ManualDataSource(MainFrame.this);
					src.sendData(manager);
					
					
				}
				
			});
		}
		return manualbutton;
	}

	/**
	 * This method initializes paintbutton	
	 * 	
	 * @return javax.swing.JButton	
	 */
	private JButton getPaintbutton() {
		if (paintbutton == null) {
			paintbutton = new JButton();
			paintbutton.setText("绘制图形");
			paintbutton.addActionListener(new ActionListener(){

				public void actionPerformed(ActionEvent arg0) {
					manager.process();
					
				}
				
			});
		}
		return paintbutton;
	}

	/**
	 * This method initializes databuttonpanel	
	 * 	
	 * @return javax.swing.JPanel	
	 */
	private JPanel getDatabuttonpanel() {
		if (databuttonpanel == null) {
			GridBagConstraints gridBagConstraints5 = new GridBagConstraints();
			gridBagConstraints5.gridx = 0;
			gridBagConstraints5.fill = GridBagConstraints.HORIZONTAL;
			gridBagConstraints5.gridy = 5;
			GridBagConstraints gridBagConstraints4 = new GridBagConstraints();
			gridBagConstraints4.gridy = 4;
			gridBagConstraints4.fill = GridBagConstraints.HORIZONTAL;
			gridBagConstraints4.gridx = 0;
			GridBagConstraints gridBagConstraints3 = new GridBagConstraints();
			gridBagConstraints3.gridy = 3;
			gridBagConstraints3.fill = GridBagConstraints.HORIZONTAL;
			gridBagConstraints3.gridx = 0;
			GridBagConstraints gridBagConstraints2 = new GridBagConstraints();
			gridBagConstraints2.gridx = 0;
			gridBagConstraints2.fill = GridBagConstraints.HORIZONTAL;
			gridBagConstraints2.gridy = 2;
			GridBagConstraints gridBagConstraints1 = new GridBagConstraints();
			gridBagConstraints1.gridy = 1;
			gridBagConstraints1.fill = GridBagConstraints.HORIZONTAL;
			gridBagConstraints1.gridx = 0;
			GridBagConstraints gridBagConstraints = new GridBagConstraints();
			gridBagConstraints.gridy = 0;
			gridBagConstraints.fill = GridBagConstraints.HORIZONTAL;
			gridBagConstraints.gridx = 0;
			databuttonpanel = new JPanel();
			databuttonpanel.setLayout(new GridBagLayout());
			databuttonpanel.add(getModify(), gridBagConstraints);
			databuttonpanel.add(getDelete(), gridBagConstraints1);
			databuttonpanel.add(getFilebutton(), gridBagConstraints2);
			databuttonpanel.add(getManualbutton(), gridBagConstraints3);
			databuttonpanel.add(getPaintbutton(), gridBagConstraints4);
			databuttonpanel.add(getExit(), gridBagConstraints5);
		}
		return databuttonpanel;
	}

	/**
	 * This method initializes delete	
	 * 	
	 * @return javax.swing.JButton	
	 */
	private JButton getDelete() {
		if (delete == null) {
			delete = new JButton();
			delete.setText("删除选定的数据");
			delete.setName("delete");
			delete.addActionListener(new ActionListener(){

				public void actionPerformed(ActionEvent arg0) {
					if(datalist.isSelectionEmpty())return;
					Double value=(Double)datalist.getSelectedValue();
					manager.deleteData(value.doubleValue());
					
				}
				
			});
		}
		return delete;
	}

	/**
	 * This method initializes modify	
	 * 	
	 * @return javax.swing.JButton	
	 */
	private JButton getModify() {
		if (modify == null) {
			modify = new JButton();
			modify.setText("清除数据");
			modify.setName("modify");
			modify.addActionListener(new ActionListener(){

				public void actionPerformed(ActionEvent arg0) {
					manager.deleteAll();
				}
				
			});
		}
		return modify;
	}

	/**
	 * This method initializes exit	
	 * 	
	 * @return javax.swing.JButton	
	 */
	private JButton getExit() {
		if (exit == null) {
			exit = new JButton();
			exit.setText("退出");
			exit.addActionListener(new ActionListener(){

				public void actionPerformed(ActionEvent arg0) {
					System.exit(0);
					
				}
				
			});
			
		}
		return exit;
	}
	
	private DataManager manager;  //  @jve:decl-index=0:

	private JScrollPane scrollPane = null;

	private JMenuBar jJMenuBar = null;

	private JMenu fileMenu = null;

	private JMenu jHelp = null;

	private JMenuItem ExitItem = null;

	private JMenuItem helpitem = null;

	private JMenuItem aboutitem = null;
	/**
	 * This method initializes scrollPane	
	 * 	
	 * @return javax.swing.JScrollPane	
	 */
	private JScrollPane getScrollPane() {
		if (scrollPane == null) {
			scrollPane = new JScrollPane();
			scrollPane.setViewportView(getDatalist());
		}
		return scrollPane;
	}
	/**
	 * This method initializes jJMenuBar	
	 * 	
	 * @return javax.swing.JMenuBar	
	 */
	private JMenuBar getJJMenuBar() {
		if (jJMenuBar == null) {
			jJMenuBar = new JMenuBar();
			jJMenuBar.add(getFileMenu());
			jJMenuBar.add(getJHelp());
		}
		return jJMenuBar;
	}
	/**
	 * This method initializes fileMenu	
	 * 	
	 * @return javax.swing.JMenu	
	 */
	private JMenu getFileMenu() {
		if (fileMenu == null) {
			fileMenu = new JMenu();
			fileMenu.setText("文件(F)");
			fileMenu.setMnemonic(KeyEvent.VK_F);
			fileMenu.add(getExitItem());
		}
		return fileMenu;
	}
	/**
	 * This method initializes jHelp	
	 * 	
	 * @return javax.swing.JMenu	
	 */
	private JMenu getJHelp() {
		if (jHelp == null) {
			jHelp = new JMenu();
			jHelp.setText("帮助(H)");
			jHelp.setMnemonic(KeyEvent.VK_H);
			jHelp.add(getHelpitem());
			jHelp.add(getAboutitem());
		}
		return jHelp;
	}
	/**
	 * This method initializes ExitItem	
	 * 	
	 * @return javax.swing.JMenuItem	
	 */
	private JMenuItem getExitItem() {
		if (ExitItem == null) {
			ExitItem = new JMenuItem();
			ExitItem.setText("退出(X)");
			ExitItem.setMnemonic(KeyEvent.VK_X);
			ExitItem.addActionListener(new java.awt.event.ActionListener() {
				public void actionPerformed(java.awt.event.ActionEvent e) {
					System.exit(0);
				}
			});
		}
		return ExitItem;
	}
	/**
	 * This method initializes helpitem	
	 * 	
	 * @return javax.swing.JMenuItem	
	 */
	private JMenuItem getHelpitem() {
		if (helpitem == null) {
			helpitem = new JMenuItem();
			helpitem.setText("使用帮助(H)");
			helpitem.setMnemonic(KeyEvent.VK_H);
			helpitem.addActionListener(new ActionListener(){

				public void actionPerformed(ActionEvent arg0) {
					help.setVisible(true);
					
				}
				
			});
		}
		return helpitem;
	}
	/**
	 * This method initializes aboutitem	
	 * 	
	 * @return javax.swing.JMenuItem	
	 */
	private JMenuItem getAboutitem() {
		if (aboutitem == null) {
			aboutitem = new JMenuItem();
			aboutitem.setText("关于(A)");
			aboutitem.setMnemonic(KeyEvent.VK_A);
			aboutitem.addActionListener(new java.awt.event.ActionListener() {
				public void actionPerformed(java.awt.event.ActionEvent e) {
					about.setVisible(true);
				}
			});
		}
		return aboutitem;
	}
	private AboutDialog about;  //  @jve:decl-index=0:visual-constraint="209,132"
    private HelpDialog help;
}  //  @jve:decl-index=0:visual-constraint="129,54"

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -