我是java swing的新手。
当我想将actionlistener设置为其中一个按钮时,它必须获取textArea内容并将其发送到另一个类。它没有以我期望的方式工作。
相反,当我更改文本区域的内容时,它不起作用,我不知道发生了什么?
我命名为按钮的第一个按钮,当我在Button的动作侦测程序中使用另一个名为 Clac
的actionlistener时,会出现同样的问题这是我的代码:
package FFT;
import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
public class FFT_Main_Frame extends JFrame {
JLabel label;
JButton button;
TextPanel txtPanel;
JButton Button;
JLabel label1;
//JTextArea[] BoxArray;
makePolynomial mp;
JButton Calc;
Complex[] Nums;
Complex[] Result;
int input;
FFT_Main fft_main;
ShowResult shr;
public FFT_Main_Frame()
{
super ("F F T");
//final int input;
label=new JLabel();
txtPanel=new TextPanel();
Button=new JButton("OK");
label.setText("Please Enter your Polynomial Degree");
setLayout(new FlowLayout());
add(label);
add(txtPanel);
add(Button);
Calc=new JButton("Claculate");
Button.addActionListener(new ActionListener()
{
//Test t;
//Integer content;
@Override
public void actionPerformed(ActionEvent arg0) {
try{
Integer content=new Integer(Integer.parseInt(txtPanel.getTextArea()));
input=content;
System.out.println(input);
//inputt=input;
mp=new makePolynomial(content);
setLayout(new FlowLayout());
add(mp);
//setLayout(new BorderLayout());
add(Calc);
Nums=new Complex[input+1];
Calc.addActionListener(new ActionListener()
{
@Override
public void actionPerformed(ActionEvent arg0) {
for(int i=input;i>=0;i--)
{
Nums[i]=new Complex(Double.parseDouble(mp.BoxArray[2*(i+1)- 1].getText()),Double.parseDouble(mp.BoxArray[2*i].getText()));
}
for (int i=0;i<=input;i++)
{
System.out.println(Nums[i]);
}
fft_main=new FFT_Main();
Result=new Complex[input];
Result=fft_main.Recursive_FFT(Nums);
shr=new ShowResult(Result);
setLayout(new BorderLayout());
add(shr,BorderLayout.CENTER);
System.out.println("Result\n\n");
for (int i=0;i<=input;i++)
{
System.out.println(Result[i]);
}
}
});
}
catch (NumberFormatException exception)
{
JOptionPane.showMessageDialog(null, "Please Enter Just Numbers! " , "Wrong Value", JOptionPane.ERROR_MESSAGE);
}
}
}
);
//Integer k=new Integer(Integer.parseInt(txtPanel.getTextArea()));
//System.out.println();
//Nums=new Complex[input+1];
setSize(600,500);
setVisible(true);
}
}
makePolynomial class:
package FFT;
import java.awt.FlowLayout;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextArea;
public class makePolynomial extends JPanel{
JLabel[] label;
public JTextArea[] BoxArray;
public static int l;
public makePolynomial(int i)
{
//label=new JLabel();
BoxArray=new JTextArea[2*(i+1)];
label=new JLabel[i];
l=i;
setLayout(new FlowLayout());
int b=i;
for (int x=2*(i+1)-1;x>=0;x-=2)
{
BoxArray[x]=new JTextArea(1,3);
BoxArray[x-1]=new JTextArea(1,3);
add(BoxArray[x]);
add(BoxArray[x-1]);
if (b >=1)
{
label[b-1]=new JLabel();
label[b-1].setText("<html>i X<sup>"+b+"</sup></html>");
add(label[b-1]);
}
if(b==0)
{
JLabel l1=new JLabel();
l1.setText("i");
add(l1);
}
b--;
}
}
}
ShowResult类:
package FFT;
import java.awt.FlowLayout;
import javax.swing.JLabel;
import javax.swing.JPanel;
public class ShowResult extends JPanel {
JLabel[] label;
public ShowResult(Complex[] c) {
label=new JLabel[c.length];
System.out.println("Complex"+c.length);
setLayout(new FlowLayout());
for (int i=0;i<c.length;i++)
{
label[i]=new JLabel();
label[i].setText("y["+i+"]= "+c[i].toString());
add(label[i]);
}
}
}
感谢您的帮助
答案 0 :(得分:0)
作为代码较小部分的可运行程序的示例,请参见下文。为了简短起见,我已将makePolynomial
类替换为JPanel
。我添加了代码来隐藏actionPerformed
Button
方法中的三个初始组件,但我很确定有更好的方法可以做到这一点。
此程序是否可以按照您的预期在您的计算机上运行?
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class ActionListenerBug {
public static void main(final String[] arguments) {
SwingUtilities.invokeLater(() -> new ActionListenerBug().createAndShowGui());
}
private void createAndShowGui() {
final JFrame frame = new JFrame("Stack Overflow");
frame.setBounds(100, 100, 800, 600);
frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
final JPanel panel = new JPanel();
JLabel label = new JLabel();
// TextPanel is a class?
JTextPane txtPanel = new JTextPane();
JButton Button = new JButton("OK");
label.setText("Please Enter your Polynomial Degree");
panel.setLayout(new FlowLayout());
panel.add(label);
panel.add(txtPanel);
panel.add(Button);
Button.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent arg0) {
try {
int input = Integer.parseInt(txtPanel.getText());
System.out.println(input);
//mp = new makePolynomial(content);
JPanel mp = new JPanel();
mp.add(new JLabel("input: " + input));
// Hide other components for now. Showing a different panel would be
// better.
label.setVisible(false);
txtPanel.setVisible(false);
Button.setVisible(false);
panel.setLayout(new FlowLayout());
panel.add(mp);
//setLayout(new BorderLayout());
JButton Calc = new JButton("Claculate");
panel.add(Calc);
panel.revalidate();
} catch (NumberFormatException exception) {
JOptionPane.showMessageDialog(null, "Please Enter Just Numbers! ",
"Wrong Value",
JOptionPane.ERROR_MESSAGE);
}
}
});
frame.getContentPane().add(panel);
frame.setVisible(true);
}
}