用java编写一个简单的程序,显示窗口,如图所示,按动按钮后,修改按钮初时值为“按钮已按”
3个回答
展开全部
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
public class Button extends JPanel implements ActionListener {
private JButton button;
private final String ACCEPT = "接受按键";
private final String PRESSED = "按钮已按";
private static final String TITLE = "Button";
public Button() {
button = new JButton(ACCEPT);
button.setVerticalTextPosition(AbstractButton.CENTER);
button.setHorizontalTextPosition(AbstractButton.CENTER);
button.setAlignmentX(Component.CENTER_ALIGNMENT);
button.setMargin(new Insets(20, 20, 20, 20));
button.addActionListener(this);
setLayout(new BoxLayout(this, BoxLayout.Y_AXIS));
add(Box.createVerticalGlue());
add(button);
add(Box.createVerticalGlue());
}
public void actionPerformed(ActionEvent e) {
button.setText(PRESSED);
}
private static void createAndShowGUI() {
JFrame frame = new JFrame(TITLE);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Button newContentPane = new Button();
newContentPane.setOpaque(true);
frame.setContentPane(newContentPane);
frame.setSize(400, 250);
frame.setVisible(true);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
createAndShowGUI();
}
});
}
}
结果:
点击前:
点击后:
追问
谢谢,不过已经考完了
就采纳你的了
展开全部
怎么也得给个几十分吧,一都没有....
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
public class TestFrame extends JFrame
{
int i = 0;
JButton b = null;
public TestFrame()
{
this.setBounds(100, 100, 200, 200);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setVisible(true);
this.setLayout(null);
b = new JButton();
b.setBounds(20, 20, 100, 30);
b.setText("按钮");
b.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
i++;
b.setText("已按" + i + "次");
}
});
this.add(b);
}
/**
* @param args
*/
public static void main(String[] args)
{
TestFrame f = new TestFrame();
}
}
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
public class TestFrame extends JFrame
{
int i = 0;
JButton b = null;
public TestFrame()
{
this.setBounds(100, 100, 200, 200);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setVisible(true);
this.setLayout(null);
b = new JButton();
b.setBounds(20, 20, 100, 30);
b.setText("按钮");
b.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
i++;
b.setText("已按" + i + "次");
}
});
this.add(b);
}
/**
* @param args
*/
public static void main(String[] args)
{
TestFrame f = new TestFrame();
}
}
本回答被网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
2016-05-05
展开全部
祝你好运气 。。。。。。。。。。。。。。。。。
追问
我靠
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询