java - 在java swing中格式化框架
问题描述
操作系统 - Windows 10 文本编辑器 - VSCode
在下面的程序中,我无法摆脱框架周围的白色轮廓。我有一个黑色桌面,并试图让应用程序融入其中以作为小部件运行。然而,我还没有找到任何挥杆中似乎摆脱有问题的白色轮廓的方法。
代码在下面,因此您可以运行它并查看我尝试过的内容以及所有好东西。我已经注释掉了我正在试验的点点滴滴,看看是否会改变任何东西。还有一个文本字段,它从我制作的另一个类文件中获取日期和时间输出,所以关于这个问题,请忽略它。除非以某种方式格式化文本字段可能会解决问题。
我浏览了 swing 提供的所有方法,但找不到一种似乎可以解决我的问题的方法。虽然我错过的机会是 100%。
import java.awt.*;
import javax.swing.*;
public class GUI implements GUIInterface{
private JFrame frame;
private JPanel panel;
//private JLabel label;
private JTextField txt;
GUI(DateAndTime current){
//label = new JLabel("Time");
//label.setBounds(10,20,80,25);
txt = new JTextField();
txt.setEditable(false);
txt.setBounds(0, 0, 130, 80);
txt.setBackground(Color.BLACK);
txt.setFont(new Font("Arial", Font.BOLD, 12));
//txt.setOpaque(false);
txt.setHorizontalAlignment(JTextField.CENTER);
txt.setText(" " + current.toString() + " ");
panel = new JPanel();
panel.setBorder(BorderFactory.createLineBorder(Color.BLACK));
panel.setLayout(new GridLayout());
//panel.add(label);
panel.add(txt);
frame = new JFrame();
//frame.setIconImage();
frame.setUndecorated(true);
frame.getContentPane().setForeground(Color.BLACK);
frame.getContentPane().setBackground(Color.BLACK);
//frame.setOpacity(0.95f);
//frame.setBounds(200, 100, 120, 70);;
//frame.setShape();
//frame.setGlassPane(new Component() );
//frame.setDefaultLookAndFeelDecorated(true);
frame.setMinimumSize(new Dimension(130, 80));
frame.setMaximumSize(new Dimension(130, 80));
frame.setResizable(false);
frame.setLocation(200,100);
//frame.setTitle("Time");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(panel, BorderLayout.CENTER);
frame.pack();
frame.setVisible(true);
while(true){
try
{
Thread.sleep(1000);
}
catch (InterruptedException ex)
{
break;
}
current.incSecond();
System.out.print("\r \r" + current.toString()); // outputs to command window
txt.setText(" " + current.toString() + " "); // outputs to GUI textField
}
}
}
解决方案
白线是Border
文本字段的:
尝试:
txt.setBorder(new EmptyBorder(0, 0, 0, 0));
另外,不要使用while (true)
循环。对于动画,使用Swing Timer。
推荐阅读
- javascript - 在 React 中使用 prevState 修改父组件中的状态
- javascript - 在Javascript中更改模板字符串中的复选框的三元代码
- docker - solr(8.2.0)docker中的索引(发布)文件拒绝访问
- swift - 向 YouTube 数据 API 发送请求以启动可恢复的上传会话时出现“解析错误”
- javascript - 根据 JSON Parse,名称未显示在 HTML 上
- node.js - npm start 无法通过 localhost 访问
- laravel - 试图获取图像的非对象属性
- express - 如何在 auth0 身份验证中处理不同的用户类型?
- python - 在电视节目中返回的照片格式
- javascript - 如何处理 PHP 结果?