jtextfield 设置边框颜色
时间: 2023-09-27 22:03:55 浏览: 156
要设置 JTextField 的边框颜色,可以使用 Border 类。具体实现可以参考以下代码:
```java
import javax.swing.BorderFactory;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextField;
import java.awt.Color;
import java.awt.Dimension;
public class TextFieldDemo {
public static void main(String[] args) {
JFrame frame = new JFrame("JTextField Border Demo");
JPanel panel = new JPanel();
JTextField textField = new JTextField("Example Text", 15);
// 设置边框颜色为红色
textField.setBorder(BorderFactory.createLineBorder(Color.RED));
panel.add(textField);
frame.add(panel);
frame.setPreferredSize(new Dimension(300, 100));
frame.pack();
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
```
在上述代码中,我们使用 `setBorder` 方法将 `JTextField` 的边框设置为红色。你可以将 `Color.RED` 替换为任何想要的颜色。
相关推荐
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![doc](https://img-home.csdnimg.cn/images/20210720083327.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)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![doc](https://img-home.csdnimg.cn/images/20210720083327.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pptx](https://img-home.csdnimg.cn/images/20210720083543.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)