首页 > 解决方案 > 在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
        }
    }

}

标签: javaswingjframe

解决方案


白线是Border文本字段的:

尝试:

txt.setBorder(new EmptyBorder(0, 0, 0, 0));

另外,不要使用while (true)循环。对于动画,使用Swing Timer


推荐阅读