用java编写一个简单的程序,显示窗口,如图所示,按动按钮后,修改按钮初...
发布网友
发布时间:2024-01-19 23:33
我来回答
共3个回答
热心网友
时间:2024-08-08 05:59
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();
}
});
}
}
结果:
点击前:
点击后:
热心网友
时间:2024-08-08 05:57
怎么也得给个几十分吧,一都没有....
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();
}
}
热心网友
时间:2024-08-08 06:02
祝你好运气 。。。。。。。。。。。。。。。。。