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

📄 booksellerguiimpl.java

📁 JADE实例——多Agent系统展示(图书交易系统)
💻 JAVA
字号:
package bookTrading.seller;

import jade.core.AID;

import java.awt.*;
import java.awt.event.*;

import javax.swing.*;

import bookTrading.buyer.BookBuyerAgent;
import bookTrading.buyer.BookBuyerGui;
import bookTrading.buyer.BookBuyerGuiImpl;
import bookTrading.swing.DateChooser;

import java.text.DateFormat;
import java.text.NumberFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;

public class BookSellerGuiImpl extends JFrame implements BookSellerGui{
	BookSellerGuiImpl(BookSellerAgent a) {
		super(a.getLocalName());
		myAgent = a;
		
		JPanel mainPanel = new JPanel();
		mainPanel.setLayout(new GridLayout(2, 1, 2, 2));
		
		GridBagLayout gridbag = new GridBagLayout();
        GridBagConstraints c = new GridBagConstraints();
        JPanel p = new JPanel();
        p.setLayout(gridbag);
        c.fill = GridBagConstraints.BOTH;
        c.weightx = 1.0;
        
        JLabel l = new JLabel("Book title:");
        gridbag.setConstraints(l, c);
        p.add(l);
        titleField = new JTextField(15);
        titleField.setText("Book title");
        c.gridwidth = GridBagConstraints.REMAINDER;
        gridbag.setConstraints(titleField, c);
        p.add(titleField);
        
        l = new JLabel("Best price:");
        c.gridwidth = 1;
        gridbag.setConstraints(l, c);
        p.add(l);
        bestPriceField = new JFormattedTextField(
        		NumberFormat.getCurrencyInstance());
        bestPriceField.setValue(new Float(100));
        gridbag.setConstraints(bestPriceField, c);
        p.add(bestPriceField);
        l = new JLabel("Min price:");
        gridbag.setConstraints(l, c);
        p.add(l);
        minPriceField = new JFormattedTextField(
        		NumberFormat.getCurrencyInstance());
        minPriceField.setValue(new Float(10));
        c.gridwidth = GridBagConstraints.REMAINDER;
        gridbag.setConstraints(minPriceField, c);
        p.add(minPriceField);
        
        l = new JLabel("Dealine:");
        c.gridwidth = 1;
        gridbag.setConstraints(l, c);
        p.add(l);
        DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        deadlineField = new JFormattedTextField(dateFormat);
        Calendar deadline = GregorianCalendar.getInstance();
        deadline.add(Calendar.DAY_OF_MONTH, 8);
        deadlineField.setValue(deadline.getTime());
        c.gridwidth = 2;
        gridbag.setConstraints(deadlineField, c);
        p.add(deadlineField);
        JButton b = new JButton("Set");
        b.addActionListener(new ActionListener() {
        	public void actionPerformed(ActionEvent ev) {
        		if (dateChooser == null) {
        			dateChooser = new DateChooser(deadlineField);
        		}
        		Point p = getLocationOnScreen();
        	    p.y = p.y+30;
        		dateChooser.showDialog("DateTime Chooser", p);
        	}
        });
        c.gridwidth = GridBagConstraints.REMAINDER;
        gridbag.setConstraints(b, c);
        p.add(b);
        
        mainPanel.add(p);
        
        msgArea = new JTextArea(8, 40);
		msgArea.setEditable(false);
		msgArea.setLineWrap(true);
		JScrollPane msgPanel = new JScrollPane(msgArea);
		mainPanel.add(msgPanel);
        
		Container container = getContentPane();
		container.add(mainPanel, BorderLayout.CENTER);
		
		b = new JButton("Sell");
		b.addActionListener( new ActionListener() {
			public void actionPerformed(ActionEvent ev) {
				try {
					String title = titleField.getText().trim();
					Number number = (Number)bestPriceField.getValue();
					int initPrice = number.intValue();
					number = (Number)minPriceField.getValue();
					int minPrice = number.intValue();
					Date deadline = (Date)deadlineField.getValue();
					myAgent.putForSale(title, initPrice, minPrice, deadline);
				}
				catch (Exception e) {
					JOptionPane.showMessageDialog(
							BookSellerGuiImpl.this,
							"Invalid values. " + e.getMessage(),
							"Error",
							JOptionPane.ERROR_MESSAGE); 
				}
			}
		});
		
		p = new JPanel();
		p.add(b);
		b = new JButton("Reset");
		p.add(b);
		b = new JButton("Exit");
		b.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent ev) {
				BookSellerGuiImpl.this.dispose();
			}
		});
		p.add(b);
		
		container.add(p, BorderLayout.SOUTH);
		
		// Make the agent terminate when the user closes 
		// the GUI using the button on the upper right corner	
		addWindowListener(new	WindowAdapter() {
			public void windowClosing(WindowEvent e) {
				myAgent.doDelete();
			}
		});
		
		setResizable(false);
	}
	
	public void show() {
		pack();
		Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
		int centerX = (int)screenSize.getWidth() / 2;
		int centerY = (int)screenSize.getHeight() / 2;
		setLocation(centerX - getWidth() / 2, centerY - getHeight() / 2);
		super.show();	
	}
	
	public void hide() {
		super.setVisible(false);
	}
	
	public void dispose() {
		super.dispose();
	}
	
	public void notifyUser(final String message) {
		Runnable notifyU = new Runnable() {
			public void run() {
				msgArea.append(message);
				msgArea.append("\n");
			}
		};
		SwingUtilities.invokeLater(notifyU);
	}
	
	private BookSellerAgent myAgent;
	
	private JTextField titleField;
	private JFormattedTextField bestPriceField;
	private JFormattedTextField minPriceField;
	private JFormattedTextField deadlineField;
	private DateChooser dateChooser;
	
	private JTextArea msgArea;

}

⌨️ 快捷键说明

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