如何在不同的类中打印jtextarea上的文本消息

时间:2014-10-15 02:42:34

标签: java class object jtextarea

我创建了一个基于控制台的程序,允许用户进行数学测验。该程序基本上生成随机数,并确定答案是对还是错。

现在,我正在尝试创建此程序的GUI版本而且我被卡住了。

我想从另一个类在jtextarea中打印文本消息。我使用了get和set方法,但由于某种原因它不输出文本消息。我已经对摇摆工作者进行过一些研究,但我不知道如何让它工作,所以我尽量避免使用它。

这不是家庭作业。我4个月前开始学习java,所以我可能不理解高级概念。

我猜我想知道的......我是否必须使用摇摆工作者?所有真正想做的是生成随机数并在jtextarea中输出结果......它不应该冻结gui,对吗?无论如何,提前谢谢。

package algorithmsProgramGUI.view;


import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;

import java.awt.Toolkit;
import java.awt.Panel;

import javax.swing.JLabel;

import java.awt.Font;

import javax.swing.text.JTextComponent;
import javax.swing.JFormattedTextField;

import java.awt.Choice;
import java.awt.Label;

import javax.swing.JPopupMenu;

import java.awt.Component;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.TextArea;
import java.awt.Button;

import org.eclipse.wb.swing.FocusTraversalOnArray;

import javax.swing.JTextArea;
public class ProFrame extends JFrame {

	/**
	 * 
	 */
	private static final long serialVersionUID = -7222968352076888482L;


	private static JTextArea textArea;
	
	
	private JPanel contentPane;
	
	

	
	
	

	public static JTextArea getTextArea() {
		
		return textArea;
	}


	public static void setTextArea(JTextArea string) {
		ProFrame.textArea = string;
	}


	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					ProFrame frame = new ProFrame();
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the frame.
	 */
	public ProFrame() {
		

		
		
		
		setIconImage(Toolkit.getDefaultToolkit().getImage(ProFrame.class.getResource("/algorithmsProgramGUI/resources/AlgorithmsLogo.png")));
		setTitle("Algorithms");
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 864, 590);
		contentPane = new JPanel();
		contentPane.setToolTipText("Choose a test to take");
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(null);
		
		JLabel lblSelectTest = new JLabel("Select Test :");
		lblSelectTest.setFont(new Font("Times New Roman", Font.BOLD, 15));
		lblSelectTest.setBounds(35, 42, 95, 14);
		contentPane.add(lblSelectTest);
		
		JFormattedTextField formattedTextField = new JFormattedTextField();
		formattedTextField.setToolTipText("Input answer here");
		formattedTextField.setBounds(419, 415, 108, 24);
		contentPane.add(formattedTextField);
		
		Label label = new Label("Answer :");
		label.setFont(new Font("Times New Roman", Font.BOLD, 15));
		label.setBounds(339, 415, 74, 22);
		contentPane.add(label);
		
		
		
		
		Panel status_panel = new Panel();
		status_panel.setFont(new Font("Times New Roman", Font.PLAIN, 12));
		status_panel.setBounds(220, 445, 558, 75);
		contentPane.add(status_panel);
		
		Button button_1 = new Button("Next Question");
		button_1.setFont(new Font("Times New Roman", Font.BOLD, 15));
		button_1.setBounds(545, 415, 154, 22);
		contentPane.add(button_1);
		
		
		
		JTextArea textArea = new JTextArea();
		textArea.setEditable(false);
		textArea.setBounds(257, 62, 521, 305);
		contentPane.add(textArea);
		
		
		
		
		Choice Test = new Choice();
		Test.setFont(new Font("Times New Roman", Font.BOLD, 12));
		Test.setBounds(35, 62, 130, 20);
		Test.add("Practice Quiz");
		Test.add("Test 1");
		Test.add("Test 2");
		contentPane.add(Test);
		
		
		
		
		
		
		Button button = new Button("Generate Test");
		button.setFont(new Font("Times New Roman", Font.BOLD, 15));
		button.setBounds(35, 235, 117, 26);
		button.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				String s = Test.getSelectedItem().toString();
				textArea.setText(s);
				textArea.setFont(new Font("Times New Roman", Font.BOLD, 12));
				
				if (Test.getSelectedItem().equalsIgnoreCase("Practice Quiz")) {
					
	
				algorithmsProgramGUI.view.PracticeQuizGUI.runPracticeQuizGUI();
				
				}
				
				
				if (Test.getSelectedItem().equalsIgnoreCase("Test 1")) {
					
					textArea.setText("Test 1 is not available at this time.");
					
				}
				
				
				if (Test.getSelectedItem().equalsIgnoreCase("Test 2")) {
					textArea.setText("Test 2 is not available at this time.");
				}
				
				
				
				
				
				
			}
		});
		
		
		contentPane.add(button);
		
		
		
		contentPane.setFocusTraversalPolicy(new FocusTraversalOnArray(new Component[]{label, formattedTextField, Test, button, status_panel, lblSelectTest, button_1}));
	}
	@SuppressWarnings("unused")
	private static void addPopup(Component component, final JPopupMenu popup) {
		component.addMouseListener(new MouseAdapter() {
			public void mousePressed(MouseEvent e) {
				if (e.isPopupTrigger()) {
					showMenu(e);
				}
			}
			public void mouseReleased(MouseEvent e) {
				if (e.isPopupTrigger()) {
					showMenu(e);
				}
			}
			private void showMenu(MouseEvent e) {
				popup.show(e.getComponent(), e.getX(), e.getY());
			}
		});
	}


	public static void setTextArea(String string) {
		// TODO Auto-generated method stub
		
	}

	
	

	



	

	
}

package algorithmsProgramGUI.view;

import javax.swing.JTextArea;
import javax.swing.text.JTextComponent;

import java.awt.*;

public class PracticeQuizGUI {
	
	

	static void runPracticeQuizGUI() {
		
		
		// System.out.println("This statement was created in PracticeQuizGUI class.");
		
		
		//algorithmsProgramGUI.view.ProFrame.ProFrame().textArea.setText("s");	
		
	
		//algorithmsProgramGUI.view.ProFrame.getTextArea();
		
		algorithmsProgramGUI.view.ProFrame.setTextArea("Welcome to the practice quiz.");
		
		//algorithmsProgramGUI.view.ProFrame.getTextArea();
		
		
		
		
		
	}

}

1 个答案:

答案 0 :(得分:1)

在您当前的代码中:

public static void setTextArea(String string) {
    // TODO Auto-generated method stub
}

没有做任何事情,使用 JTextArea.setText(String t)方法设置值