gpt4 book ai didi

java - 如何实现 Jbutton 和事件监听器来关闭应用程序?

转载 作者:行者123 更新时间:2023-12-01 08:13:28 27 4
gpt4 key购买 nike

以下是我的代码...jButton 将添加到 GUI 方法中。我需要一个 jbutton 来添加到容器中,还需要一个事件监听器来使应用程序在按下时关闭。

public class BeetsWk1 extends JFrame{

public BeetsWk1(){

GuI();
}

public void GuI(){
FlowLayout layout = new FlowLayout();

layout.setAlignment(FlowLayout.CENTER);

Container container;
container = getContentPane();
container.setBackground(new Color(052,062,138));
container.setLayout(layout);

JLabel label = new JLabel();
label.setText ("Hello World" );
label.setSize( 500, 400);
label.setFont( new Font( "SanSerif", Font.PLAIN, 15) );
label.setHorizontalAlignment( JLabel.CENTER );
label.setForeground(Color.white);
container.add( label );
}

public static void main(String[] args) {
// TODO code application logic here
Dimension dimension = new Dimension(500, 500);
BeetsWk1 window = new BeetsWk1();
window.setVisible(true);
window.setSize(dimension);
window.setDefaultCloseOperation(window.EXIT_ON_CLOSE);
}

private Dimension Dimension(int i, int j) {
throw new UnsupportedOperationException("Not yet implemented");
}
}

最佳答案

是什么阻碍了你?

JButton button = new JButton();
button.setText("Some text");
getContentPane().add(button);

button.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent evt) {
System.exit(0);
}
});

关于java - 如何实现 Jbutton 和事件监听器来关闭应用程序?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15477523/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com