首页 > 解决方案 > 无法在框架上放置按钮?

问题描述

我目前正在使用 Intellij 在主框架顶部创建一个带有 3 个按钮的 GUI。我对 GUI 还很陌生,并且仍在学习。当我尝试运行代码时,我收到一条错误消息,告诉我我不能将 main 方法设为静态,但如果我删除静态,我会收到一条错误消息,说我需要它是静态的。有没有办法轻松避免这种情况?

package CA3;

import javax.swing.*; import java.awt.*; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.event.WindowAdapter; import java.awt.event.WindowEvent;

public class Main extends JFrame{

    public static void main(String[] args) {

        // Attach window listener
        addWindowListener(new WindowCloser()); // Just in-case it's needed

        // Adding first customer panel
        JPanel pCustomerL = new JPanel();
        pCustomerL.setBounds(0,0,750,750);
        pCustomerL.setBackground(Color.BLUE);


        // Adding second customer panel
        JPanel pCustomerR = new JPanel();
        pCustomerR.setBounds(750,0,750,750);
        pCustomerR.setBackground(Color.BLACK);
        // Adding first invoice panel
        JPanel pInvoiceL = new JPanel();
        pInvoiceL.setBounds(0,0,750,750);
        pInvoiceL.setBackground(Color.BLUE);
        // Adding second product panel
        JPanel pInvoiceR = new JPanel();
        pInvoiceR.setBounds(750,0,750,750);
        pInvoiceR.setBackground(Color.BLACK);

        // Button Listener
        ButtonListener listener = new ButtonListener();
        // Adding "Customer" Button
        JButton b = new JButton("Customer");
        b.addActionListener(listener);
        add(b);
        b.setBounds(1300,10,150,35);
        // Adding "Product" Button
        b = new JButton("Product");
        b.addActionListener(listener);
        add(b);
        b.setBounds(1150,10,150,35);
        // Adding "Invoice" Button
        b = new JButton("Invoice");
        b.addActionListener(listener);
        add(b);
        b.setBounds(1000,10,150,35);


        // Adding first product panel
        JPanel pProductL = new JPanel();
        pProductL.setBounds(0,0,750,750);
        //pProductL.setBackground(Color.BLUE);
        // Adding second product panel
        JPanel pProductR = new JPanel();
        pProductR.setBounds(750,0,750,750);
        //pProductR.setBackground(Color.BLACK);


        // Frame
        JFrame frame = new JFrame();
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setLayout(null);
        frame.setSize(1500,750);
        frame.setVisible(true);
        // Add panels
        frame.add(pCustomerL);
        frame.add(pCustomerR);
        frame.add(pProductL);
        frame.add(pProductR);
        frame.add(pInvoiceL);
        frame.add(pInvoiceR);

        // Customer Panel Settings
        pCustomerL.setVisible(true);
        pCustomerR.setVisible(true);
        // Product Settings
        pProductL.setVisible(false);
        pProductR.setVisible(false);
        // Invoice settings
        pInvoiceL.setVisible(false);
        pInvoiceR.setVisible(false);
    }

    // Listener for buttons
    class ButtonListener implements ActionListener {
        public void actionPerformed(ActionEvent evt) {
            String buttonLabel = evt.getActionCommand();
        }
    }

    // Listener for window
    class WindowCloser extends WindowAdapter {
        public void windowClosing(WindowEvent evt) { System.exit(0);}
    }
}

标签: javauser-interfaceintellij-idea

解决方案


您必须将主代码中的代码移动到另一个非静态方法,或者您可以将其放在构造函数上。错误是因为您使用的是从 JFrame 继承的addWindowsListener等普通方法。你还必须删除:

        JFrame frame = new JFrame();

这不是必需的,因为您的类已经从 JFrame 继承,所以您的代码必须是这样的:

public class Main extends JFrame
{

public Main(){
    // Attach window listener
    addWindowListener(new WindowCloser()); // Just in-case it's needed

    // Adding first customer panel
    JPanel pCustomerL = new JPanel();
    pCustomerL.setBounds(0,0,750,750);
    pCustomerL.setBackground(Color.BLUE);


    // Adding second customer panel
    JPanel pCustomerR = new JPanel();
    pCustomerR.setBounds(750,0,750,750);
    pCustomerR.setBackground(Color.BLACK);
    // Adding first invoice panel
    JPanel pInvoiceL = new JPanel();
    pInvoiceL.setBounds(0,0,750,750);
    pInvoiceL.setBackground(Color.BLUE);
    // Adding second product panel
    JPanel pInvoiceR = new JPanel();
    pInvoiceR.setBounds(750,0,750,750);
    pInvoiceR.setBackground(Color.BLACK);

    // Button Listener
    ButtonListener listener = new ButtonListener();
    // Adding "Customer" Button
    JButton b = new JButton("Customer");
    b.addActionListener(listener);
    add(b);
    b.setBounds(1300,10,150,35);
    // Adding "Product" Button
    b = new JButton("Product");
    b.addActionListener(listener);
    add(b);
    b.setBounds(1150,10,150,35);
    // Adding "Invoice" Button
    b = new JButton("Invoice");
    b.addActionListener(listener);
    add(b);
    b.setBounds(1000,10,150,35);


    // Adding first product panel
    JPanel pProductL = new JPanel();
    pProductL.setBounds(0,0,750,750);
    //pProductL.setBackground(Color.BLUE);
    // Adding second product panel
    JPanel pProductR = new JPanel();
    pProductR.setBounds(750,0,750,750);
    //pProductR.setBackground(Color.BLACK);




        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        this.setLayout(null);
        this.setSize(1500,750);
        

    this.add(pCustomerL);
    this.add(pCustomerR);
    this.add(pProductL);
    this.add(pProductR);
    this.add(pInvoiceL);
    this.add(pInvoiceR);

    // Customer Panel Settings
    pCustomerL.setVisible(true);
    pCustomerR.setVisible(true);
    // Product Settings
    pProductL.setVisible(false);
    pProductR.setVisible(false);
    // Invoice settings
    pInvoiceL.setVisible(false);
    pInvoiceR.setVisible(false);
   this.setVisible(true);
}

 // Listener for buttons
class ButtonListener implements ActionListener {
    public void actionPerformed(ActionEvent evt) {
        String buttonLabel = evt.getActionCommand();
    }
}

// Listener for window
class WindowCloser extends WindowAdapter {
    public void windowClosing(WindowEvent evt) { System.exit(0);}
}

public static void main(String[] args) {
new Main();
}
}

推荐阅读