Home > other >  How to make a JButtons appear
How to make a JButtons appear

Time:10-09

I want to make a JButton that lies on the NorthPane of the frame, but when I run the program there's no button. Why does it do that? I'm using IntelliJ IDEA. BTW I'm posting this question again, cause the first time I didn't get the desired answer.

Here's my code.

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() {
    }
}

CodePudding user response:

to add a JButton you have to create a JButton object and add that to the JPanel. And you need to define it for EVERY button that you want to have. anywhere. I'm surprised your compiler isn't flagging that

Anyway what you want should look sth like this

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

CodePudding user response:

First, I want you to check your compiler and IntelliJ cause they're not working if you can run the code you posted.

You cannot implement a JButton unless you have made an interface yourself. Cause the JButton is not an interface.

  1. No need to set a BorderLayout for your JPanel. FlowLayout will do the job.
  2. Use a for loop to avoid duplicate code.
  3. Learn how to use Output of the above code

    Is this your desired output?

  • Related