我在一个外部类中有一个JLabel对象,想在内部类中修改其text属性,可是在内部类中得到的这个JLabel对象却处于invalid状态,请高手指点迷津!
代码如下:
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.GridLayout;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Timer;
import java.util.TimerTask;import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.SwingConstants;
import javax.swing.border.LineBorder;
import javax.swing.border.TitledBorder;public class Repair { private JLabel l_state = new JLabel(); private JTextField tf_ip; private JTextField textField; private JTextField tf_echoNum; private JTextField tf_bytes; private JFrame frame; private static String testHost = "202.204.48.66"; private static int sendBytes = 1; private static int echoNum = 1; private static int repairFrequency = 5000; private static Repair m_instance = null; private static Timer timer = new Timer(); private static TimerTask task = new RepairTask(); /**
 * Launch the application
 * 
 * @param args
 */
public static void main(String args[]) {
try {
Repair window = new Repair();
window.frame.setVisible(true);
timer.scheduleAtFixedRate(task, 0, Repair.getInstance()
.getRepairFrequency());
} catch (Exception e) {
e.printStackTrace();
}
} /**
 * Create the application
 */
private Repair() {
initialize();
} public static Repair getInstance() {
if (m_instance == null) {
synchronized (Repair.class) {
if (m_instance == null) {
m_instance = new Repair();
}
}
}
return m_instance;
} /**
 * Initialize the contents of the frame
 */
private void initialize() {
frame = new JFrame();
frame.setResizable(false);
frame.setIconImage(new ImageIcon("tips.gif").getImage());
frame.setTitle("J.L网络修复器");
frame.setSize(308, 207);
frame.setLocation((int) (Toolkit.getDefaultToolkit().getScreenSize()
.getWidth() / 2 - frame.getWidth() / 2), (int) (Toolkit
.getDefaultToolkit().getScreenSize().getHeight() / 2 - frame
.getHeight() / 2));
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); final JPanel panel = new JPanel();
panel.setBorder(new TitledBorder(null, "网络设置",
TitledBorder.DEFAULT_JUSTIFICATION,
TitledBorder.DEFAULT_POSITION, null, null)); final GridLayout gridLayout = new GridLayout(0, 2);
gridLayout.setVgap(2);
panel.setLayout(gridLayout);
frame.getContentPane().add(panel, BorderLayout.CENTER); final JLabel label = new JLabel();
label.setHorizontalAlignment(SwingConstants.CENTER);
label.setText("发送字节数:");
panel.add(label); tf_bytes = new JTextField();
tf_bytes.setText("1");
panel.add(tf_bytes); final JLabel label_1 = new JLabel();
label_1.setHorizontalAlignment(SwingConstants.CENTER);
label_1.setText("回应包数量:");
panel.add(label_1); tf_echoNum = new JTextField();
tf_echoNum.setText("1");
panel.add(tf_echoNum); final JLabel label_3 = new JLabel();
label_3.setHorizontalAlignment(SwingConstants.CENTER);
label_3.setText("修复频率(秒):");
panel.add(label_3); textField = new JTextField();
textField.setText("5");
panel.add(textField); final JLabel label_2 = new JLabel();
label_2.setHorizontalAlignment(SwingConstants.CENTER);
label_2.setText("校园网地址:");
panel.add(label_2); tf_ip = new JTextField();
tf_ip.setText("202.204.48.66");
panel.add(tf_ip); final JPanel panel_1 = new JPanel();
frame.getContentPane().add(panel_1, BorderLayout.SOUTH); final JButton bt_ok = new JButton();
bt_ok.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) { }
});
bt_ok.setText("确定");
panel_1.add(bt_ok); final JButton bt_cancel = new JButton();
bt_cancel.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
System.out.println(l_state);
l_state.setText("f");
}
});
bt_cancel.setText("取消");
panel_1.add(bt_cancel); l_state.setBorder(new LineBorder(new Color(0, 128, 255), 1, true));
l_state.setHorizontalAlignment(SwingConstants.CENTER);
l_state.setText("网络状态...");
frame.getContentPane().add(l_state, BorderLayout.NORTH);
} public String getTestHost() {
return testHost;
} public void setTestHost(String testHost) {
Repair.testHost = testHost;
} public int getEchoNum() {
return echoNum;
} public void setEchoNum(int echoNum) {
Repair.echoNum = echoNum;
} public int getRepairFrequency() {
return repairFrequency;
} public void setRepairFrequency(int repairFrequency) {
Repair.repairFrequency = repairFrequency;
} public int getSendBytes() {
return sendBytes;
} public void setSendBytes(int sendBytes) {
Repair.sendBytes = sendBytes;
} public JLabel getL_state() {
return l_state;
} public static class RepairTask extends TimerTask {
String line = null; StringBuffer pingCmd = new StringBuffer(); String[] repairCmds = { "arp -d *", "nbtstat -R", "ipconfig /flushdns",
"nbtstat -RR", "ipconfig /registerdns" }; @Override
public void run() {
// TODO Auto-generated method stub
try {
pingCmd.delete(0, pingCmd.length());
pingCmd.append("ping ");
pingCmd.append(Repair.getInstance().getTestHost());
pingCmd.append(" -n " + Repair.getInstance().getEchoNum());
pingCmd.append(" -l " + Repair.getInstance().getSendBytes());
Process process = Runtime.getRuntime().exec(pingCmd.toString());
BufferedReader in = new BufferedReader(new InputStreamReader(
process.getInputStream()));
line = in.readLine();
while (line != null) {
if (line.startsWith("Request")) {
Repair.getInstance().getL_state().setText(
"网络故障,正尝试修复...");
System.out.println("网络故障,正尝试修复...");
for (int i = 0; i < repairCmds.length; i++) {
Runtime.getRuntime().exec(repairCmds[i]);
}
Repair.getInstance().getL_state().setText("修复完毕!");
System.out.println("修复完毕!");
break;
} else {
System.out.println(Repair.getInstance().getL_state());
Repair.getInstance().getL_state().setText("网络正常!");
Repair.getInstance().getL_state().repaint();
//System.out.println("网络正常!");
}
line = in.readLine();
}
} catch (IOException e) {
// TODO Auto-generated catch block
System.out.println("IO错误!");
}
}
}}

解决方案 »

  1.   

    呵呵,我在RepairTask这个内部类中,输出外部类中的JLabel对象,javax.swing.JLabel[,0,0,302x20,invalid,alignmentX=0.0,alignmentY=0.0,border=javax.swing.border.LineBorder@1cafa9e,flags=8388608,maximumSize=,minimumSize=,preferredSize=,defaultIcon=,disabledIcon=,horizontalAlignment=CENTER,horizontalTextPosition=TRAILING,iconTextGap=4,labelFor=,text=ff,verticalAlignment=CENTER,verticalTextPosition=CENTER],看到这个对象处于invalid状态,而且这个对象的text属性已经改变了,可是没有显示出来