gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-29 05:38:04 25 4
gpt4 key购买 nike

在尝试从 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 代码 - 不包括导入或 Action 监听器:

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

}
}

最佳答案

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

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

关于java - 试图运行 GUI 应用程序,但 GUI 从未出现,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18724987/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com