1

在尝试从 Main 运行我的基本 GUI 应用程序时,我以某种方式设法首先显示 GUI(但它小于我在代码中设置的大小并且不显示任何组件)然后神奇地(在添加 pack()和 setlocationrelativeto(null)) 它根本不会弹出。我正在使用 Netbeans(如果有帮助的话),在 Main 它给了我一个工具提示,我的 GUI“从未使用过”,因此它运行并输出“已完成的构建”,而不是继续运行并显示 GUI。我提供了 2 组代码(1)主要方法和(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 代码 - 不包括导入或动作监听器:

    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); 

    }
}
4

2 回答 2

3

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

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

于 2013-09-10T17:32:41.900 回答
0

如果您想编写简单的 gui 应用程序,您应该检查WindowBuilder。它是真正有效的 java 拖放 gui。

于 2013-09-10T19:06:16.117 回答