java中 编写一个图形界面的Application程序,内含一个按钮。
开始运行时,按钮显示“ClickMe”字样,当按下按钮是,按钮上面的文字变成“ClickMeAgain”,再按一次,则变回原来的“ClickMe”字样,如此循环。要求用S...
开始运行时,按钮显示“Click Me”字样,当按下按钮是,按钮上面的文字变成“Click Me Again”,再按一次,则变回原来的“Click Me”字样,如此循环。要求用Swing组件实现。
展开
2个回答
展开全部
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.WindowConstants;
import javax.swing.JFrame;
public class ButtonTest extends javax.swing.JPanel implements ActionListener {
private JButton button;
public static void main(String[] args) {
JFrame frame = new JFrame();
frame.getContentPane().add(new ButtonTest());
frame.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
frame.pack();
frame.setVisible(true);
}
public ButtonTest() {
super();
initGUI();
}
private void initGUI() {
try {
setPreferredSize(new Dimension(400, 300));
{
button = new JButton();
this.add(button);
button.setText("Click Me");
button.addActionListener(this);
}
} catch (Exception e) {
e.printStackTrace();
}
}
public void actionPerformed(ActionEvent e) {
if(button.getText().equals("Click Me")){
button.setText("Click Me Again");
}else{
button.setText("Click Me");
}
}
}
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.WindowConstants;
import javax.swing.JFrame;
public class ButtonTest extends javax.swing.JPanel implements ActionListener {
private JButton button;
public static void main(String[] args) {
JFrame frame = new JFrame();
frame.getContentPane().add(new ButtonTest());
frame.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
frame.pack();
frame.setVisible(true);
}
public ButtonTest() {
super();
initGUI();
}
private void initGUI() {
try {
setPreferredSize(new Dimension(400, 300));
{
button = new JButton();
this.add(button);
button.setText("Click Me");
button.addActionListener(this);
}
} catch (Exception e) {
e.printStackTrace();
}
}
public void actionPerformed(ActionEvent e) {
if(button.getText().equals("Click Me")){
button.setText("Click Me Again");
}else{
button.setText("Click Me");
}
}
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询