java 获取button的值,从jButton获取文本值
时间: 2023-10-06 08:13:15 浏览: 145
要从 JButton 中获取文本值,可以使用 `getText()` 方法。该方法返回 JButton 上显示的文本。
例如:
```
JButton myButton = new JButton("点击我");
String buttonText = myButton.getText();
System.out.println(buttonText); // 输出:"点击我"
```
在这个例子中,我们创建了一个 JButton,它的文本值是 "点击我"。然后,我们使用 `getText()` 方法获取这个文本值,并将它保存到一个 String 变量中。最后,我们使用 `System.out.println()` 方法将这个文本值输出到控制台。
相关问题
Jbutton动态显示按钮值
好的,你可以通过设置JButton的文本(text)属性来动态显示按钮的值。具体实现方法如下:
```java
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class DynamicButtonDisplay extends JFrame {
private JPanel panel;
private JButton button;
public DynamicButtonDisplay() {
panel = new JPanel();
button = new JButton("Click me!");
panel.add(button);
this.add(panel);
this.setTitle("Dynamic Button Display");
this.setSize(300, 200);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setVisible(true);
}
public void setButtonText(String text) {
button.setText(text);
}
public static void main(String[] args) {
DynamicButtonDisplay display = new DynamicButtonDisplay();
display.setButtonText("New button label");
}
}
```
这个示例代码中创建了一个名为`DynamicButtonDisplay`的窗口,并在窗口中添加了一个按钮`JButton`,并设置了初始的文本为`Click me!`。在`setButtonText`方法中,我们可以通过`button.setText(text)`方法动态地设置按钮的文本。
在`main`方法中,我们创建了一个`DynamicButtonDisplay`对象`display`,并调用`setButtonText`方法,将按钮的文本设置为`New button label`。
运行程序后,你会发现按钮的文本已经动态地更新了。希望这个示例代码能够帮助你实现动态显示JButton的值。
java获取输入框内容
你可以使用Java Swing框架中的JTextField来获取输入框中的内容。首先,你需要创建一个JTextField对象,然后使用getText()方法来获取输入框中的文本内容。以下是一个简单的示例代码:
```
import javax.swing.*;
public class Main {
public static void main(String[] args) {
JFrame frame = new JFrame("Java获取输入框内容示例");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// 创建一个JTextField对象
JTextField textField = new JTextField(20);
frame.getContentPane().add(textField);
// 创建一个按钮,点击后获取输入框中的内容
JButton button = new JButton("获取输入框内容");
button.addActionListener(event -> {
String content = textField.getText();
System.out.println("输入框中的内容为:" + content);
});
frame.getContentPane().add(button);
frame.pack();
frame.setVisible(true);
}
}
```
该示例代码创建了一个JTextField对象,并将其添加到JFrame中。然后创建了一个按钮,点击该按钮后会获取输入框中的内容并输出到控制台。你可以根据自己的需要修改代码。
相关推荐
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)
![doc](https://img-home.csdnimg.cn/images/20210720083327.png)
![text/x-java](https://img-home.csdnimg.cn/images/20210720083646.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)