JButton 和 JPanel 未显示在 JFrame 上

问题描述 投票:0回答:1

我制作了一个程序,底部有一个“登录”按钮,在它开始不显示放置 JButton 的 JPanel 的背景之前,它工作了一段时间。尝试一段时间后,我发现 JButton 只在我将鼠标悬停在它上面时才会显示

我尝试排除代码的特定区域,我意识到这个故障发生在我初始化变量 JButton 时,即使排除

addAll();
方法(来自下面给出的代码)也会使 JPanel 可见。 代码非常基础和简单

public Panel() {
        this.setBackground(Color.darkGray);
        this.setSize(600,400);
        this.setVisible(true);
        addAll();
    }

    private void addAll() {
        String app = "res/files/debugging.jar";
        JButton b = new JButton();
        
        b.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent evt) {
                 if (app!=null) {
                     File file = new File(app);
                     try {
                        Desktop.getDesktop().open(file);
                        
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                 }
                }});
        b.setText("Login");
        Dimension size = b.getPreferredSize();
        b.setBounds(250,290,(int)size.getWidth(),(int)size.getHeight());
        this.setLayout(null);
        this.add(b);
    }

Panel 扩展类 JPanel。 下面提供了正在使用的 JFrame

public static void main(String[] args) {
        JFrame frame = new JFrame();
        
        
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setResizable(false);
        frame.setTitle("Login to your Account");
        //frame.setIconImage(frameIcon);
        frame.setSize(600,400);
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
        frame.add(new Panel());
    }
java swing jframe jpanel jbutton
1个回答
0
投票

试试这个。

import java.awt.Color;
import java.awt.Desktop;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.IOException;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;

public class FrameDemo extends JPanel {
    JFrame frame = new JFrame();
    JButton b = new JButton();
    String app = "res/files/debugging.jar";

    public static void main(String[] args) {
        SwingUtilities.invokeLater(() -> new FrameDemo().start());
    }

    public void start() {
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        b.setText("Login");
        b.addActionListener(new MyActionListener());
        Dimension size = b.getPreferredSize();
        this.setLayout(new FlowLayout());
        add(b);
        this.add(b);
    }
    public Dimension getPreferredSize() {
        return new Dimension(600,400);
    }
    public FrameDemo() {
        frame.add(this);
        this.setBackground(Color.darkGray);
        frame.setVisible(true);
        frame.pack();
    }

    private class MyActionListener implements ActionListener {
        public void actionPerformed(ActionEvent evt) {
            if (app != null) {
                File file = new File(app);
                try {
                    Desktop.getDesktop().open(file);

                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.