另一个班级的开放框架 [英] Opening Frame from another class

查看:62
本文介绍了另一个班级的开放框架的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好,
我有两个类,帮助和RecylingGui.我在RecyclingGui上有一个名为帮助"的按钮,单击该按钮将打开在帮助"类中创建的文本区域.我为此添加了ActionListener,但没有任何反应.任何帮助,将不胜感激.谢谢.

代码:
回收课

Hello,
I have two classes, Help and RecylingGui. I have a button on the RecyclingGui called Help, which when clicked should open the text area created in the Help class. I added the ActionListener for this but nothing is happening. Any help would be appreciated. Thanks.

The Code:
Recycling Class

import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
/**
 * Simple Graphical User Interface for the Recycling Machine.
 
 *
 */
public class RecyclingGUI extends JFrame implements ActionListener  {
 
	CustomerPanel myPanel = new CustomerPanel(new Display()); 
	public CustomerPanel getPanel() { return myPanel; } 
 
 
	public void actionPerformed(ActionEvent e) {
		//System.out.println("Received: e.getActionCommand()="+e.getActionCommand()+
		//					" e.getSource()="+e.getSource().toString() ); 
		if( e.getSource() == slot1 )
		{
			myPanel.itemReceived(1);
		} else if( e.getSource() == slot2 ) { 
			myPanel.itemReceived(2);
		} else if( e.getSource() == slot3 ) {
			myPanel.itemReceived(3);
		} else if(e.getSource() == slot4 ){
			myPanel.itemReceived(4);
		} else if( e.getSource() == receipt ) {
			myPanel.printReceipt();
		} else if(e.getSource() == check){
			int siize = Integer.parseInt(size.getText().toString());
			int weeight = Integer.parseInt(weight.getText().toString());
			myPanel.printitem(siize,weeight);
		}else if(e.getSource() == unknown){
			myPanel.itemReceived(5);
		}
 
	}
 
	static JButton slot1 = new JButton("Slot 1"); 
	static JButton slot2 = new JButton("Slot 2"); 
	static JButton slot3 = new JButton("Slot 3"); 
	static JButton slot4 = new JButton("Slot 4");
	static JButton unknown = new JButton("unknown");
	static JButton help = new JButton("help");
	static JButton receipt = new JButton("Receipt");  
	static JTextField weight = new JTextField(5);
	static JTextField size = new JTextField(5);
	static JButton check = new JButton("Check"); 
	static JLabel sizeLabel  = new JLabel("Size");
	static JLabel weightLabel = new JLabel("Weight");
 
 
	private static JPanel getFieldPanel() {
	      JPanel p = new JPanel(new GridLayout(4,2));
	      p.setBorder(BorderFactory.createTitledBorder("Recycle"));
	      p.add(new JLabel("Weight:",SwingConstants.RIGHT));
	      p.add(size);
	      p.add(new JLabel("Size:",SwingConstants.RIGHT));
	      p.add(weight);
	      p.add(check);
	      p.add(help);
	      return p;
	   }
 
	public static JPanel getButtonPanel() {
	      JPanel p = new JPanel(new GridLayout(5,1));
	      //p.setBorder(BorderFactory.createTitledBorder("Recycle"));
	      p.add(slot1);
	      p.add(slot2);
	      p.add(slot3);
	      p.add(slot4);
	      p.add(unknown);
	      p.add(receipt);
	      return p;
	   }
 
	public RecyclingGUI() {
		super();
		setSize(400, 100);
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);	
		JPanel panel = new JPanel();
		panel.setLayout(new GridLayout(2,1));
 
 
		panel.add(getButtonPanel()); 
		panel.add(getFieldPanel());
 
		slot1.addActionListener(this); 
		slot2.addActionListener(this);
		slot3.addActionListener(this); 
		slot4.addActionListener(this);		 
		receipt.addActionListener(this); 
		check.addActionListener(this);
		unknown.addActionListener(this); 
		help.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent e){
				new Help();
			}
 
		});
 
 
		getContentPane().add(panel);
		panel.repaint();
 
	}
 
	public static void main(String [] args ) { 
		RecyclingGUI myGUI = new RecyclingGUI(); 
		myGUI.setVisible(true); 
		myGUI.pack();
	}
}



帮助课



Help Class

import javax.swing.*;
import java.awt.*;
 
public class Help extends JPanel {
	public Help(){
		this.setLayout(new BorderLayout());
		this.setPreferredSize(new Dimension(400,200));
 
		JTextArea textArea = new JTextArea(5,40);
		textArea.setText("Help - FAQ");
		textArea.setEditable(false);
		JScrollPane ScrollPane = new JScrollPane(textArea);
		this.add(ScrollPane, BorderLayout.CENTER);
	}		
 
	public static void main(String[] args) {
		//Help GU = new Help();
		JPanel panel = new Help();
		panel.setOpaque(true);
		JFrame frame = new JFrame("Help");
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frame.setContentPane(panel);
		frame.pack();
		frame.setVisible(true);
	}	
}



感谢



Thanks

推荐答案

在您的ActionListener中,您需要将新的Help对象的Visible属性设置为true.您正在创建它,但未在动作侦听器中显示它.
您首先需要将其放入JFrame,使其可见,然后使JFrame可见.使用您在帮助"类中放入的主要方法作为模型,来满足您在动作侦听器中需要的方法.
In your ActionListener, you need to set the new Help object''s Visible property to true. You''re creating it but not displaying it in the action listener.
You first need to put it in a JFrame, make it visible, then make the JFrame visible. Use the main method you put in the Help class as a model for what you need in your action listener.


这篇关于另一个班级的开放框架的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆