我的 Jframe 在执行我的 Java 程序期间不可查看.我只是调整框架大小后,所有字段都是可见的



我的 Jframe 在执行我的 Java 程序期间不可查看。我只是调整框架大小后,所有字段都是可查看的。从 eclipse 运行 java 文件后,输出仅显示空帧,然后我调整窗口大小,所有字段都是可查看的。

import java.awt.Color;
import java.awt.Font;
import java.awt.event.ActionListener;
import java.util.Vector;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.UIManager;
import javax.swing.table.JTableHeader;
public class DBtoExcel extends JFrame { 
    JLabel l1, l2, l3, l4, l5, l6, l7, l8, l9;
    JTextField tf1, tf2,tf3, tf4, tf5, tf6;
    JButton btn1, btn2, upload ,saveAsExcel;
    //JPasswordField p1, p2;
    JTextArea tf7;
    JTable table;
    JScrollPane scrollPane;
    Vector columnNamesVector;
    Vector dataVector;
    JComboBox combo = new JComboBox();
    JScrollPane pane = new JScrollPane(table);
    Vector<String> v = new Vector<String>();
    DBtoExcel(){
        combo.setEditable(true);
        pack();
        //setContentPane(pane);
        setLocationByPlatform(true);
        setResizable(true);
        setVisible(true);
        setSize(600, 800);
        setLayout(null);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setTitle("CUSTOMER INFO");
        l1 = new JLabel("Add customer details");
        l1.setForeground(Color.blue);
        l1.setFont(new Font("Serif", Font.BOLD, 20));
        l2 = new JLabel("Firstname:");
        l3 = new JLabel("Secondname:");
        l4 = new JLabel("Contact No:");
        l5 = new JLabel("Email-ID:");
        l6 = new JLabel("Country:");
        l7 = new JLabel("State:");
        l8 = new JLabel("Description:"); 
        l9 = new JLabel("Config upload:"); 
        tf1 = new JTextField();
        tf2 = new JTextField();
        tf3 = new JTextField();
        tf4 = new JTextField();
        //p1 = new JPasswordField();
        //p2 = new JPasswordField();
        tf5 = new JTextField();
       tf6 = new JTextField();
        tf7 = new JTextArea();




            //create table with data
            JTable table = new JTable(dataVector, columnNamesVector);
            JTableHeader header = table.getTableHeader();
            //;


        btn1 = new JButton("Submit");
        btn2 = new JButton("Clear");
        upload = new JButton("Browse");
        saveAsExcel = new JButton("Save as Excel");
       /* btn1.addActionListener(this);
        btn2.addActionListener(this);
        upload.addActionListener(this);
        saveAsExcel.addActionListener(this);*/
        l1.setBounds(100, 30, 400, 30);
        l2.setBounds(80, 70, 200, 30);
        l3.setBounds(80, 110, 200, 30);
        l4.setBounds(80, 150, 200, 30);
        l5.setBounds(80, 190, 200, 30);
        l6.setBounds(80, 230, 200, 30);
        l7.setBounds(80, 270, 200, 30);
        l8.setBounds(80, 310, 200, 30);
        l9.setBounds(80, 400, 200, 30);
        tf1.setBounds(300, 70, 200, 30);
        tf2.setBounds(300, 110, 200, 30);
        tf3.setBounds(300, 150, 200, 30);
        tf4.setBounds(300, 190, 200, 30);
        //p1.setBounds(300, 150, 200, 30);
        //p2.setBounds(300, 190, 200, 30);
        //tf5.setBounds(300, 235, 200, 30);
        //combo.setBounds(300, 235, 200, 30);
        tf6.setBounds(300, 270, 200, 30);
        tf7.setBounds(300, 310, 200, 70);

        upload.setBounds(300, 400, 100, 30);

        btn1.setBounds(170, 450, 100, 30);
        btn2.setBounds(280, 450, 100, 30);
        saveAsExcel.setBounds(450, 450, 150, 30);
        header.setBounds(80, 500, 800, 30);
        table.setBounds(80, 530, 800, 250);
        table.setBackground(Color.pink);
        add(l1);
        add(l2);
        add(tf1);
        add(l3);
        add(tf2);
        //add(l4);
        //add(p1);
        //add(l5);
        //add(p2);
        add(l4);
        add(tf3);
        add(l5);
        add(tf4);
        add(l6);
        //add(tf5);
        add(l7);
        add(tf6);
        add(l8);
        add(tf7);
        add(l9);
        add(btn1);
        add(upload);
        add(btn2);
        add(saveAsExcel);
        add(header);
        add( table );
       //add(pane);

      }
    public static void main(String args[]){
        try {
            UIManager.setLookAndFeel("com.sun.java.swing.plaf.nimbus.NimbusLookAndFeel");
        }
        catch (Exception ex){
         ex.printStackTrace();
        }
        System.out.println("enter");
    //new Registration();
        new DBtoExcel();
    }
}`

尽管您已经为组件设置了边界,但组件仍未显示的原因是您以错误的方式和错误的顺序实现 GUI。

    首先,除非您
  • 有正当理由,否则不建议将类扩展到 JFrame,相反,您可以扩展到 JPanel 并将您自定义的 JPanel 添加到 JFrame。您很少需要定制的 JFrame。

  • 现在,组件未显示的主要原因是,您首先设置了 JFrame 的大小,然后添加组件。Java 绘制管理器在检测到状态更改(例如调整大小/鼠标悬停(时会重新绘制 JFrame 中的所有组件。因此,当您设置 JFrame 的大小时,它将重新绘制所有组件(在此阶段,在您的案例中尚未添加任何组件,因此不会显示任何内容(。

  • 由于您在设置大小后添加了组件,因此在手动将鼠标悬停在组件上或调整 JFrame 的大小之前,您无法看到它。您可以在添加所有组件后尝试设置大小,然后您将明白我的意思。(组件现在将显示,但这并不能解决问题的根源。

你可以有一个这样的结构:

class MainPanel extends JPanel
{
    //Add your members and components in here..
}
public class MainClass{
    public static void main(String[] args){
        JFrame frame = new JFrame();
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.add(new MainPanel());
        frame.pack();  //resize, thus repaint will occur here
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
    }
}

最后但并非最不重要的一点是,您可能希望为容器使用布局。为了简单明了,显示了上面的代码。您可能希望改为在事件调度线程中运行 UI。

最新更新