日韩av黄I国产麻豆传媒I国产91av视频在线观看I日韩一区二区三区在线看I美女国产在线I麻豆视频国产在线观看I成人黄色短片

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 >

java 更新jlabel_java – 如何动态更改JLabel

發布時間:2025/5/22 36 豆豆
生活随笔 收集整理的這篇文章主要介紹了 java 更新jlabel_java – 如何动态更改JLabel 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

我有一個JLabel和一個按鈕,JLabel顯示按下按鈕的次數,但是,我無法計算如何更新顯示按鈕按下次數的JLabel.

import java.awt.event.*;

import java.awt.*;

import javax.swing.*;

public class SimpleGui {

private JFrame f = new JFrame("Basic GUI"); // create Frame

int pressed = 0; // tracks number of button presses.

JLabel label1 = new JLabel("You have pressed button " + pressed + "times.");

private JButton start = new JButton("Click To Start!");

public SimpleGui() {

// Setup Main Frame

f.getContentPane().setLayout(new GridLayout(0,1));

start.addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent e) {

calculate();

}

});

// Add components

f.add(label1);

f.add(start);

// Allows the Swing App to be closed

f.addWindowListener(new ListenCloseWdw());

}

public class ListenMenuQuit implements ActionListener {

public void actionPerformed(ActionEvent e) {

System.exit(0);

}

}

public class ListenCloseWdw extends WindowAdapter {

public void windowClosing(WindowEvent e) {

System.exit(0);

}

}

public void launchFrame() {

// Display Frame

f.setDefaultCloSEOperation(JFrame.EXIT_ON_CLOSE);

f.pack(); // Adjusts panel to components for display

f.setVisible(true);

}

public static void main(String args[]) {

PrimeTime gui = new PrimeTime();

gui.launchFrame();

}

public void calculate() {

pressed++;

label1 = new JLabel("You have pressed button " + pressed + "times.");

// update the GUI with new jLabel

f.repaint();

}

}

總結

以上是生活随笔為你收集整理的java 更新jlabel_java – 如何动态更改JLabel的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。