从 java 中的另一个 class 接收 textArea 的内容(附代码)
Receiving content of textArea from another class in java (With Code)
我正在开发一个有两个 classes MainGUI 和 MainTest 的项目。所以我的问题是我想在 MainGUI 的 testArea 上打印在 MainTest class 中完成的计算结果。
我所做的是,
MainGUI obj=new MainGUI(); \declared in MainTest class
obj.myTextField.setTest("Result"); \Result from the function
我还尝试了 append() 函数 too.But 它在我执行代码时显示了我的代码中的一堆错误。
请帮忙..
节目具体如下
1.Class MainGUI
public class MainGUI {
private JFrame frame;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
MainGUI window = new MainGUI();
window.frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the application.
*/
public MainGUI() {
initialize();
}
/**
* Initialize the contents of the frame.
*/
private void initialize() {
frame = new JFrame();
frame.setBounds(100, 100, 450, 300);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().setLayout(null);
JTextArea myTextArea = new JTextArea();
myTextArea.setBounds(124, 71, 192, 116);
frame.getContentPane().add(myTextArea);
JLabel lblTheResultWill = new JLabel("The Result Will Be Displyed Below.");
lblTheResultWill.setBounds(118, 46, 274, 14);
frame.getContentPane().add(lblTheResultWill);
}
}
2.Class 主测试
public class SquareRootDemo2
{
public static void main(String[] args)
{
//Number for which square root is to be found
double number = -12;
//This method finds out the square root
findSquareRoot(number);
}
/*This method finds out the square root without using
any built-in functions and displays it */
public static void findSquareRoot(double number)
{
MainGUI obj = new MainGUI();
boolean isPositiveNumber = true;
double g1;
//if the number given is a 0
if(number==0)
{
obj.myTextField.setTest("Square root of "+number+" = "+0);
}
//If the number given is a -ve number
else if(number<0)
{
number=-number;
isPositiveNumber = false;
}
//Proceeding to find out square root of the number
double squareRoot = number/2;
do
{
g1=squareRoot;
squareRoot = (g1 + (number/g1))/2;
}
while((g1-squareRoot)!=0);
//Displays square root in the case of a positive number
if(isPositiveNumber)
{
obj.myTextField.setTest("Square roots of "+number+" are ");
obj.myTextField.setTest("+"+squareRoot);
obj.myTextField.setTest("-"+squareRoot);
}
//Displays square root in the case of a -ve number
else
{
obj.myTextField.setTest("Square roots of -"+number+" are ");
obj.myTextField.setTest("+"+squareRoot+" i");
obj.myTextField.setTest("-"+squareRoot+" i");
}
}
}
您必须将代码放在按钮中 method.so 当按下按钮时您的代码 implements.that 这就是您得不到结果的原因。
private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {
MainGUI obj=new MainGUI();//declared in MainTest class'
obj.myTextField.setText("Result");//Result from the function'
obj.setVisible(true);
}
What I need is that I want to print the result of Squre root of a number in the textArea of MainGUI class
问题 #1...
无法从 initialize
方法外部访问 JTextArea
...
private void initialize() {
//...
JTextArea myTextArea = new JTextArea();
您需要将其设为实例字段
public class MainGUI {
private JFrame frame;
private JTextArea myTextArea;
//...
private void initialize() {
//...
myTextArea = new JTextArea();
接下来,您需要提供一些方法来修改 JTextArea
...
public class MainGUI {
//...
public void seText(String text) {
myTextArea.append(text);
}
public void append(String text) {
myTextArea.seText(text);
}
问题 #2...
您违反了 Swing 的单线程规则,您不应该在事件调度线程之外创建或修改任何 UI 组件...
类似...
public static void findSquareRoot(double number) {
MainGUI obj = null;
SwingUtilities.invokeAndWait(new Runnable() {
@Override
public void run() {
obj = new MainGUI();
}
});
boolean isPositiveNumber = true;
double g1;
//if the number given is a 0
if (number == 0) {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
obj.myTextField.setTest("Square root of " + number + " = " + 0);
}
});
} //If the number given is a -ve number
else if (number < 0) {
number = -number;
isPositiveNumber = false;
}
//Proceeding to find out square root of the number
double squareRoot = number / 2;
do {
g1 = squareRoot;
squareRoot = (g1 + (number / g1)) / 2;
} while ((g1 - squareRoot) != 0);
//Displays square root in the case of a positive number
if (isPositiveNumber) {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
obj.append("Square roots of " + number + " are ");
obj.append("+" + squareRoot);
obj.append("-" + squareRoot);
}
});
} //Displays square root in the case of a -ve number
else {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
obj.append("Square roots of -" + number + " are ");
obj.append("+" + squareRoot + " i");
obj.append("-" + squareRoot + " i");
}
});
}
}
例如...
关于这里,我认为 SwingWorker
会更有用
看看:
更多详情...
我正在开发一个有两个 classes MainGUI 和 MainTest 的项目。所以我的问题是我想在 MainGUI 的 testArea 上打印在 MainTest class 中完成的计算结果。 我所做的是,
MainGUI obj=new MainGUI(); \declared in MainTest class
obj.myTextField.setTest("Result"); \Result from the function
我还尝试了 append() 函数 too.But 它在我执行代码时显示了我的代码中的一堆错误。 请帮忙..
节目具体如下
1.Class MainGUI
public class MainGUI {
private JFrame frame;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
MainGUI window = new MainGUI();
window.frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the application.
*/
public MainGUI() {
initialize();
}
/**
* Initialize the contents of the frame.
*/
private void initialize() {
frame = new JFrame();
frame.setBounds(100, 100, 450, 300);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().setLayout(null);
JTextArea myTextArea = new JTextArea();
myTextArea.setBounds(124, 71, 192, 116);
frame.getContentPane().add(myTextArea);
JLabel lblTheResultWill = new JLabel("The Result Will Be Displyed Below.");
lblTheResultWill.setBounds(118, 46, 274, 14);
frame.getContentPane().add(lblTheResultWill);
}
}
2.Class 主测试
public class SquareRootDemo2
{
public static void main(String[] args)
{
//Number for which square root is to be found
double number = -12;
//This method finds out the square root
findSquareRoot(number);
}
/*This method finds out the square root without using
any built-in functions and displays it */
public static void findSquareRoot(double number)
{
MainGUI obj = new MainGUI();
boolean isPositiveNumber = true;
double g1;
//if the number given is a 0
if(number==0)
{
obj.myTextField.setTest("Square root of "+number+" = "+0);
}
//If the number given is a -ve number
else if(number<0)
{
number=-number;
isPositiveNumber = false;
}
//Proceeding to find out square root of the number
double squareRoot = number/2;
do
{
g1=squareRoot;
squareRoot = (g1 + (number/g1))/2;
}
while((g1-squareRoot)!=0);
//Displays square root in the case of a positive number
if(isPositiveNumber)
{
obj.myTextField.setTest("Square roots of "+number+" are ");
obj.myTextField.setTest("+"+squareRoot);
obj.myTextField.setTest("-"+squareRoot);
}
//Displays square root in the case of a -ve number
else
{
obj.myTextField.setTest("Square roots of -"+number+" are ");
obj.myTextField.setTest("+"+squareRoot+" i");
obj.myTextField.setTest("-"+squareRoot+" i");
}
}
}
您必须将代码放在按钮中 method.so 当按下按钮时您的代码 implements.that 这就是您得不到结果的原因。
private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {
MainGUI obj=new MainGUI();//declared in MainTest class'
obj.myTextField.setText("Result");//Result from the function'
obj.setVisible(true);
}
What I need is that I want to print the result of Squre root of a number in the textArea of MainGUI class
问题 #1...
无法从 initialize
方法外部访问 JTextArea
...
private void initialize() {
//...
JTextArea myTextArea = new JTextArea();
您需要将其设为实例字段
public class MainGUI {
private JFrame frame;
private JTextArea myTextArea;
//...
private void initialize() {
//...
myTextArea = new JTextArea();
接下来,您需要提供一些方法来修改 JTextArea
...
public class MainGUI {
//...
public void seText(String text) {
myTextArea.append(text);
}
public void append(String text) {
myTextArea.seText(text);
}
问题 #2...
您违反了 Swing 的单线程规则,您不应该在事件调度线程之外创建或修改任何 UI 组件...
类似...
public static void findSquareRoot(double number) {
MainGUI obj = null;
SwingUtilities.invokeAndWait(new Runnable() {
@Override
public void run() {
obj = new MainGUI();
}
});
boolean isPositiveNumber = true;
double g1;
//if the number given is a 0
if (number == 0) {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
obj.myTextField.setTest("Square root of " + number + " = " + 0);
}
});
} //If the number given is a -ve number
else if (number < 0) {
number = -number;
isPositiveNumber = false;
}
//Proceeding to find out square root of the number
double squareRoot = number / 2;
do {
g1 = squareRoot;
squareRoot = (g1 + (number / g1)) / 2;
} while ((g1 - squareRoot) != 0);
//Displays square root in the case of a positive number
if (isPositiveNumber) {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
obj.append("Square roots of " + number + " are ");
obj.append("+" + squareRoot);
obj.append("-" + squareRoot);
}
});
} //Displays square root in the case of a -ve number
else {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
obj.append("Square roots of -" + number + " are ");
obj.append("+" + squareRoot + " i");
obj.append("-" + squareRoot + " i");
}
});
}
}
例如...
关于这里,我认为 SwingWorker
会更有用
看看:
更多详情...