我的代码有问题。我使用各种JPanel创建了一个GUI,我将按钮放在JPanel中,将JTextArea放在JPanel中。当我点击按钮创建命令console.append("");在控制台上显示消息将无法运行并告诉我:
线程中的异常" AWT-EventQueue-0"
单击按钮时,如何让我打印信息?如果它不起作用console.append
P.S。 我知道问题是调用console.append("");因为,例如,如果在构造函数中粘贴connect()的内容就会被执行,但这对我不利
这是我的代码:
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.*;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import javax.swing.*;
import javax.swing.JTextArea;
import lejos.pc.comm.*;
public class send {
JPanel pan1;
JPanel pan2;
JPanel pan3;
JPanel pan4;
JPanel pan5;
JButton reconnect;
JButton disconnect;
JButton start;
JCheckBox show1;
JLabel label1;
JCheckBox show2;
JLabel label2;
JSlider sldCentigrade;
JTextField txtCent;
JSlider Luminosity;
JTextField txtLum;
JLabel img;
DataOutputStream dos;
DataInputStream dis;
JFrame s;
JTextArea console;
JScrollPane pScroll;
public static void main(String[] args) {
send send = new send();
send.connect();//Start connection
}
public send(){
s = new JFrame();
s.setTitle("Server");
s.setSize(550,650);
s.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
s.setVisible(true);
JPanel pan1= new JPanel();
//pan1.setBackground(Color.GREEN);
JPanel pan2= new JPanel();
//pan2.setBackground(Color.MAGENTA);
pan2.setLayout(new BorderLayout());
JPanel pan3= new JPanel();
//pan3.setBackground(Color.RED);
pan3.setLayout(new BorderLayout());
JPanel pan4= new JPanel();
//pan4.setBackground(Color.BLUE);
JPanel pan5= new JPanel();
//pan5.setBackground(Color.YELLOW);
pan5.setLayout(new BorderLayout());
JTextArea console = new JTextArea();
JButton reconnect = new JButton("Re/Connect");
reconnect.setSize(50, 50);
pan4.add(reconnect);
reconnect.addActionListener(new reconnectListener());
JButton disconnect = new JButton("Disconnect");
disconnect.setSize(50,50);
pan4.add(disconnect);
disconnect.addActionListener(new disconnectListener());
JButton start = new JButton("Start");
start.setSize(50,50);
pan4.add(start);
start.addActionListener(new startListener());
/*JButton reconnect = new JButton("Reconnect");
start.setSize(50, 50);
pan3.add(reconnect);
JButton stop = new JButton("STOP");
stop.setSize(50, 50);
pan2.add(stop);
JButton pause = new JButton("PAUSE");
pause.setSize(50, 50);
pan5.add(pause);
*/
JCheckBox show1= new JCheckBox();
pan1.add(show1,BorderLayout.NORTH);
JLabel label1 = new JLabel("Automatic");
pan1.add(label1);
JCheckBox show2= new JCheckBox();
pan1.add(show2,BorderLayout.CENTER);
JLabel label2 = new JLabel("Manual");
pan1.add(label2);
JSlider sldCentigrade = new JSlider();
sldCentigrade.setPreferredSize(new Dimension(280, 20));
JTextField txtCent = new JTextField("Temperature");
pan5.add(txtCent,BorderLayout.CENTER);
pan2.add(sldCentigrade, BorderLayout.CENTER);
JSlider Luminosity = new JSlider();
JTextField txtLum = new JTextField("Luminosity");
pan5.add(txtLum,BorderLayout.SOUTH);
pan2.add(Luminosity,BorderLayout.SOUTH);
JScrollPane pScroll = new JScrollPane(console, JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
pScroll.setPreferredSize(new Dimension(600, 450));
pan3.add(pScroll,BorderLayout.SOUTH);
JLabel img= new JLabel();
img.setIcon(new ImageIcon("C:\\Users\\TEX\\Desktop\\mindstorms.jpg"));
img.setPreferredSize(new Dimension(50, 80));
pan3.add(img,BorderLayout.NORTH);
s.getContentPane().add(pan1,BorderLayout.WEST);
s.getContentPane().add(pan2,BorderLayout.EAST);
s.getContentPane().add(pan3,BorderLayout.NORTH);
s.getContentPane().add(pan4,BorderLayout.SOUTH);
s.getContentPane().add(pan5,BorderLayout.CENTER);
//s.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//s.setVisible(true);
}
public void connect()
{
NXTConnector conn = new NXTConnector();//create a new NXT connector
boolean connected = conn.connectTo("btspp://"); //try to connect to any NXT over bluetooth
if (!connected) {//failure
JTextArea console = new JTextArea();
console.append("Failed to connect to any NXT\n");
console.append("Press Reconect to retry.\n");
}
else//success!
{JTextArea console = new JTextArea();
console.append("Connected to " + conn.getNXTInfo() + "\n");
dos= new DataOutputStream (conn.getOutputStream ()); //create data input and output streams to send and recieve data.
dis = new DataInputStream (conn.getInputStream ());
}
}
public void disconnectAction()
{
try{
dos.writeInt(0); //NXT recognizes 0 as command to terminate bluetooth connection and seek a new one.
dos.flush();
console.append("NXT disconnected. Press Reconnect to reconnect.\n");
}
catch(Exception e){console.append("Could not send command\n");}
}
public void startAction()
{
try{
dos.writeInt(39); //NXT recognizes 39 as command to start NXT.
dos.flush();
console.append("NXT is doing :.\n");
String s = dis.readLine();
console.append( s +"\n" ) ;//read from NXT
}
catch(Exception e){console.append("Could not send command\n");}
}
class disconnectListener implements ActionListener
{
public void actionPerformed(ActionEvent evt)
{
disconnectAction();
}
}
class reconnectListener implements ActionListener
{
public void actionPerformed(ActionEvent evt)
{
connect();// call main connect method to try to connect again
}
}
class startListener implements ActionListener
{
public void actionPerformed(ActionEvent evt)
{
startAction();
}
}
}
答案 0 :(得分:1)
构建你的ui时,你隐藏了所有的类成员。而不是再次声明:
JPanel pan1= new JPanel();
只需分配给它:
pan1= new JPanel();
(这适用于所有班级成员)