当前位置: 技术问答>java相关
如何改变button上的标签,比如start按钮按下之后变成stop,在按一次又成start.如此循环。
来源: 互联网 发布时间:2015-01-16
本文导语: 谁能帮我看看应该怎么处理这个事件。 import java.awt.*; import java.awt.event.*; import javax.swing.*; public class ServerPort { public static void main(String[] args) { JFrame frame=new ServerFrame(); frame.show(); } } class ServerFrame extends JF...
谁能帮我看看应该怎么处理这个事件。
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class ServerPort
{ public static void main(String[] args)
{ JFrame frame=new ServerFrame();
frame.show();
}
}
class ServerFrame extends JFrame
{ public ServerFrame()
{ setSize(300,200);
setTitle("服务器");
addWindowListener(new WindowAdapter()
{ public void windowClosing(WindowEvent e)
{ System.exit(0);
}
} );
Container contentPane=getContentPane();
JPanel panel=new JPanel();
if (startflag==true)
{ StartPush="Start";
}
else
{ StartPush="Stop";
}
JButton startButton=new JButton(StartPush);
panel.add(startButton);
startButton.addActionListener(new ActionListener()
{ public void actionPerformed(ActionEvent evt)
{ startflag=!startflag;
ServerSocketPort Ssp = new ServerSocketPort(p);
Ssp.start();
}
});
textArea=new JTextArea(8,40);
scrollPane=new JScrollPane(textArea);
contentPane.add(panel,"South");
contentPane.add(scrollPane,"Center");
}
private JScrollPane scrollPane;
private JTextArea textArea;
private int p=8189;
private boolean startflag=true;
private String StartPush;
}
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class ServerPort
{ public static void main(String[] args)
{ JFrame frame=new ServerFrame();
frame.show();
}
}
class ServerFrame extends JFrame
{ public ServerFrame()
{ setSize(300,200);
setTitle("服务器");
addWindowListener(new WindowAdapter()
{ public void windowClosing(WindowEvent e)
{ System.exit(0);
}
} );
Container contentPane=getContentPane();
JPanel panel=new JPanel();
if (startflag==true)
{ StartPush="Start";
}
else
{ StartPush="Stop";
}
JButton startButton=new JButton(StartPush);
panel.add(startButton);
startButton.addActionListener(new ActionListener()
{ public void actionPerformed(ActionEvent evt)
{ startflag=!startflag;
ServerSocketPort Ssp = new ServerSocketPort(p);
Ssp.start();
}
});
textArea=new JTextArea(8,40);
scrollPane=new JScrollPane(textArea);
contentPane.add(panel,"South");
contentPane.add(scrollPane,"Center");
}
private JScrollPane scrollPane;
private JTextArea textArea;
private int p=8189;
private boolean startflag=true;
private String StartPush;
}
|
if (startButton.getText()!="Start") startButton.setText("Start");
else startButton.setText("Stop");
else startButton.setText("Stop");
|
楼上老兄记错了,应是:
if (startButton.getLabel()!="Start") startButton.setLabel("Start");
else startButton.setLabel("Stop");
if (startButton.getLabel()!="Start") startButton.setLabel("Start");
else startButton.setLabel("Stop");
|
因为你用的匿名内部类,
startButton.addActionListener(new ActionListener()
{ public void actionPerformed(ActionEvent evt)
{ startflag=!startflag;
ServerSocketPort Ssp = new ServerSocketPort(p);
Ssp.start();
}
});
在内部类里无法取得方法里定义的变量,除非是公有的或static,也就是说,startButton应该定义在public ServerFrame()外,为ServerFrame的一个属性。
startButton.addActionListener(new ActionListener()
{ public void actionPerformed(ActionEvent evt)
{ startflag=!startflag;
ServerSocketPort Ssp = new ServerSocketPort(p);
Ssp.start();
}
});
在内部类里无法取得方法里定义的变量,除非是公有的或static,也就是说,startButton应该定义在public ServerFrame()外,为ServerFrame的一个属性。