有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java试图运行GUI应用程序,但GUI从未出现

在尝试从Main运行我的基本GUI应用程序时,我设法首先使GUI显示(但它小于我在代码中设置的大小,并且没有显示任何组件),然后神奇地(添加pack()和setlocationrelativeto(null))它根本不会弹出。我使用的是Netbeans(如果有帮助的话),它主要给我一个工具提示,说明我的GUI“从未使用过”,因此它运行并输出“已完成的构建”,而不是继续运行并显示GUI。我提供了两组代码(1)main方法和(2)GUI类。如果我感到困惑,请告诉我,因为这在我的头脑中当然是有道理的,但可能沟通不好。我还没有包括完整的代码,但如果有必要,请让我知道,我会这样做

package logTime;

public class LogInTime {   

public static void main(String[] args) {

    try{
        LogAppFrame app = new LogAppFrame();  //IDE gives tooltip that app is unused
    }
    catch(Exception e){
        System.err.println("\n\nError Occurred: "); //am going to print message later
    }
  }
}

实际GUI代码-不包括导入或actionlisteners:

    public void LogAppFrame(){

    frame = new JFrame("Time Log Application");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    cl = new CardLayout();
    frame.getContentPane().setLayout(cl);
    //frame.setLayout(cl);  
    frame.setSize(new Dimension(375,385));

    logNewFrame = new JPanel();
    logNewFrame.setLayout(new GridLayout(5,1));
    logNewFrame.setBorder(new EmptyBorder(20,20,20,20));
    frame.getContentPane().add(logNewFrame, "logNewFrame");

    historyFrame = new JPanel(); 
    historyFrame.setLayout(new GridLayout(2,1)); //given 0 for rows to add numerous rows
    historyFrame.setBorder(new EmptyBorder(20,20,20,20));
    frame.getContentPane().add(historyFrame, "historyFrame");
    .
    .
    .
    //added lots of components but will not include code as there is no error within this portion of code - i used to have both Main and LogAppFrame class all together and my GUI worked and showed components but I felt it may be best practice not to do it this way and cardlayout wasnt working
    .
    .
    .
    frame.pack();
    frame.setLocationRelativeTo(null);
    frame.setVisible(true); 

在下面添加SSCE:

package logTime;

import java.awt.CardLayout;
import java.awt.Dimension;
import java.awt.event.*;
import java.awt.FlowLayout;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.GridLayout;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;

public class LogAppFrame{

    private static JFrame frame;
    private static CardLayout cl;
//menu option components
    /**
     * Help option: How To Use/Read Me - explains how to use it 8 Hour Day -
     * shows the arrival and leave time based on lunch type
     */
    /**
     * Log New Date option: shows screen to input new values into date
     */
    /**
     * View Past Dates option: shows all past dates since forever - may add
     * month tabs later
     */
    /**
     * Edit Past Date option: doesnt exist yet but will be added to View Past
     * menu option as side button to edit any old date
     */
    private static JMenuBar menuBar = new JMenuBar();
    private static JMenu help;
    private static JMenuItem logNewDate;
    private static JMenuItem viewPastDates;
    private static JMenuItem workDay;
    private static JMenuItem about;
//Log New Date components
    /**
     * 4 labels, 1 button, 1 calendar, 2 dropdowns, 2 textfields, 5x2 gridlayout
     */
    private static JLabel dateToday;
    private static JLabel timeInToday;
    private static JLabel timeOutToday;
    private static JLabel lunchTypeToday;
    private static JLabel timeColon1;
    private static JLabel timeColon2;
    private static JButton saveButton;
    private static JComboBox month;
    private static JComboBox day;
    private static JComboBox year;
    private static JComboBox amPm1;
    private static JComboBox amPm2;
    private static JComboBox hrTimeIn;
    private static JComboBox hrTimeOut;
    private static JComboBox minTimeIn;
    private static JComboBox minTimeOut;
    private static JPanel dateTodayPanel;
    private static JPanel timeInPanel;
    private static JPanel timeOutPanel;
    private static JPanel lunchTypePanel;
    private static JPanel saveButtonPanel;
    private static JComboBox lunchType;
//View Past Dates components
    /**
     * 4x*infinitiy* gridlayout or have a flowlayout, 4 labels
     */
    private static JLabel pastDates;
    private static JLabel pastTimeIns;
    private static JLabel pastTimeOuts;
    private static JLabel pastLunchTypes;
    private static JPanel headers; //holds header labels
    private static JPanel oldLogs; //will hold all past log panels

//Frames to hold the logNew and viewOld views    
    private static JPanel logNewFrame;    
    private static JPanel historyFrame;

    public void LogAppFrame(){

        frame = new JFrame("Time Log Application");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        cl = new CardLayout();
        frame.getContentPane().setLayout(cl);
        //frame.setLayout(cl);  
        frame.setSize(new Dimension(375,385));

        logNewFrame = new JPanel();
        logNewFrame.setLayout(new GridLayout(5,1));
        logNewFrame.setBorder(new EmptyBorder(20,20,20,20));
        frame.getContentPane().add(logNewFrame, "logNewFrame");

        historyFrame = new JPanel(); 
        historyFrame.setLayout(new GridLayout(2,1)); //given 0 for rows to add numerous rows
        historyFrame.setBorder(new EmptyBorder(20,20,20,20));
        frame.getContentPane().add(historyFrame, "historyFrame");


//Menu components
        menuBar = new JMenuBar();
        help = new JMenu("Help");
        logNewDate = new JMenuItem("Log New Date");
        viewPastDates = new JMenuItem("View Past Dates");

        workDay = new JMenuItem("8 Hour Day");
        about = new JMenuItem("How To ...");
        help.add(workDay);
        help.add(about);

        menuBar.add(logNewDate);
        menuBar.add(viewPastDates);
        menuBar.add(help);

        frame.setJMenuBar(menuBar);


        frame.pack();
        frame.setLocationRelativeTo(null);
        frame.setVisible(true); 

    }
}

共 (2) 个答案

  1. # 1 楼答案

    在应该是构造函数的东西之前,您已经编写了void。因此它被编译器误认为是一个永远不会被调用的方法。不幸的是,在这种情况下,编译器会为您生成一个无操作构造函数。只需删除void关键字

    顺便说一下,从字段中删除所有讨厌的static关键字。那很痛

  2. # 2 楼答案

    如果您想编写简单的gui应用程序,应该选中WindowBuilder。这是一个真正有效的java拖放gui