-1

In this code I have a Button1, where I tried to set the size of 100,400 (I'm going to add more buttons). Although when I run the application the Button is just big enough for the text "Empty Task" to fit.

I would also like to add a title, not a title for the application which I've already done but instead I would like to add a title above the button that says Grocery List in bold letters.

import java.awt.Color;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Toolkit;
import javax.swing.JButton;
import javax.swing.JFrame;

public class Frame extends JFrame {

    private static final long serialVersionUID = 1L;

    public static void main(String[] args) {
        new Frame().setVisible(true);
    }

    public Frame() {
        super("GroceryList");
        setSize(500, 850);
        setResizable(false);
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        setLayout(new FlowLayout());
        Dimension dim = Toolkit.getDefaultToolkit().getScreenSize();
        this.setLocation(dim.width / 2 - this.getSize().width / 2, dim.height / 2 - this.getSize().height / 2);

        JButton Button1 = new JButton("Empty task");
        setResizable(false);
        Button1.setSize(400, 100);
        setVisible(true);
        add(Button1);
    }

    public void Button1() {
        setVisible(true);
    }
}
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
  • You can adjust the `margin` property, use layout properties. *"I would like to add a title above the button that says Grocery List in bold letters."* Use a `JLabel`, making use of it's basic HTML support – MadProgrammer Jul 25 '17 at 10:50

1 Answers1

0
Button1.setPreferredSize(new Dimension(400,100)); (not preferred, better to use suitable layout manager)
Button1.setBorder(BorderFactory.createTitledBorder("Grocery List"));

This might help.

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459