将GUI链接到主类

时间:2018-08-10 02:09:20

标签: java swing

我已经创建了一个Java程序并为其创建了GUI。但是我无法将它们链接在一起,因为这是我的第一个Java程序。我的代码如下。

主要

public class FinalN {

    private static void setRoundedCorners(XSSFChart chart, boolean setVal) {
        if (chart.getCTChartSpace().getRoundedCorners() == null) chart.getCTChartSpace().addNewRoundedCorners();
        chart.getCTChartSpace().getRoundedCorners().setVal(setVal);
    }
    public static void main (String[]args) throws Exception {
        File src= new File("C:\\Users\\File.xlsx");
        FileInputStream fis= new FileInputStream (src);

        XSSFWorkbook wb = new XSSFWorkbook(fis);
        XSSFSheet sheet1= wb.getSheetAt(0);
// Continues

GUI

public class UserInterface {

    public class java {

    }

    private JFrame frame;
    private JTextField textField;

    /**
     * Launch the application.
     */
    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {

我试图从这篇文章How to load a Java GUI class from a main class?中了解一个想法 但我无法完成任务。请帮助

1 个答案:

答案 0 :(得分:3)

您只需要在FinalN类中实例化GUI类 并将代码添加到GUI的构造函数中。

main()函数的代码:

public static void main (String[]args) throws Exception {
File src= new File("C:\\Users\\File.xlsx");
FileInputStream fis= new FileInputStream (src);
UserInterface gui = new UserInterface();
XSSFWorkbook wb = new XSSFWorkbook(fis);
XSSFSheet sheet1= wb.getSheetAt(0);
}

构造函数使用JFrame创建基本GUI的代码

public class UserInterface {
private JFrame frame;
private JTextField textField;
private JButton button;
public UserInterface() {
    frame = new JFrame("Name of the Frame");
    textField = new JTextField(20); //width of the textfield
    button = new JButton("Click"); //text on the button
    frame.add(textField); //adding the component to the frame, all components must be explicitly added like this
    frame.setVisible(true); //to make the frame visible
    frame.setSize(500,500); //width and height of frame
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //to make the frame close on pressing x button on the frame
}

这应该有助于您从实现类中的主要方法创建基本框架GUI。请询问您是否需要更多说明