intellij-idea 如何显示JButton

nhaq1z21  于 2022-11-01  发布在  其他
关注(0)|答案(2)|浏览(266)

我想做一个JButton,它位于框架的NorthPane上,但是当我运行程序时,没有按钮。为什么会这样呢?我使用的是IntelliJ IDEA。顺便说一句,我再次发布这个问题,因为第一次我没有得到想要的答案。
这是我的密码。

package com.company;

import javax.swing.*;
import javax.swing.border.Border;
import java.awt.*;

class Fantasyrpglifesim implements JButton {
    Fantasyrpglifesim() {
    }

    public static void main(String[] args) {
        MouseInputAdapter();
        //Frame//
        JFrame frame = new JFrame();
        frame.setDefaultCloseOperation(WindowConstants.HIDE_ON_CLOSE);
        frame.setSize(1500, 1500);
        frame.getContentPane();
        frame.setVisible(true);
        JPanel mainPanel = new JPanel(new BorderLayout());
        JPanel northPanel = new JPanel();
        mainPanel.add(northPanel,BorderLayout.NORTH );
        //frame.setLayout(new FlowLayout());//

        //Buttons//
        frame.add(BUTTON);
        BUTTON.setText("Age up");
        northPanel.add(BUTTON);
        northPanel.add(BUTTON1);
        BUTTON1.setText("Test");
        northPanel.add(BUTTON2);
        BUTTON2.setText("Test1");
        northPanel.add(BUTTON2);
        BUTTON2.setText("Test2");
        northPanel.add(BUTTON3);
        BUTTON3.setText("Test3");
        northPanel.add(BUTTON4);
        BUTTON4.setText("Test4");
        northPanel.add(BUTTON5);
        BUTTON5.setText("Test5");
        northPanel.add(BUTTON6);
        BUTTON6.setText("Test6");
        northPanel.add(BUTTON7);
        BUTTON7.setText("Test7");
        northPanel.add(BUTTON8);
        BUTTON8.setText("Test8");
        northPanel.add(BUTTON9);
        BUTTON9.setText("Test9");
        northPanel.add(BUTTON10);
        BUTTON10.setText("Test10");
        northPanel.add(BUTTON11);
       BUTTON11.setText("Test11");
        northPanel.add(BUTTON12);
        BUTTON12.setText("Test12");
        northPanel.add(BUTTON13);
        BUTTON13.setText("Test13");
        northPanel.setVisible(true);

        //panels//
        //mainPanel.add(northPanel, BorderLayout.NORTH);//
    }

    private static void MouseInputAdapter() {
    }
}
wdebmtf2

wdebmtf21#

要添加一个JButton,您必须创建一个JButton对象并将其添加到JPanel中。并且您需要为您希望拥有的每个按钮定义它。任何地方。我很惊讶您的编译器没有标记它
总之你要的东西应该是这样的

JButton testButton = new JButton("test");
        northPanel.add(testButton);
2w3rbyxf

2w3rbyxf2#

首先,我希望你检查一下你的编译器和IntelliJ,因为如果你能运行你发布的代码,它们就不能工作了。
你不能用implement来代替JButton,除非你自己做了一个接口。因为JButton不是一个接口。
1.不需要为你的JPanel设置边框布局。FlowLayout将完成这项工作。
1.使用for循环以避免重复代码。
1.了解如何使用Swing components.

import javax.swing.*;
import java.awt.*;

class Fantasyrpglifesim {
    private  static int count = 1;

    public static void main(String[] args) {
        JFrame frame = new JFrame();
        JPanel mainPanel = new JPanel();

        JButton age = new JButton("Age up");
        mainPanel.add(age);

        for (int x=0; x<14;x++){
            JButton button = new JButton();
            button.setText("Test"+ count++);
            mainPanel.add(button);
        }

        frame.getContentPane().add(BorderLayout.NORTH,mainPanel);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setSize(1500, 1500);
        frame.setVisible(true);
    }
}

这是您想要的输出吗?

相关问题