在此代碼中,我有一個Button1
,其中我試圖設置100,400的大小(我將添加更多按鈕)。儘管當我運行該應用程序時,Button對於文本「Empty Task」來說足夠大以適應。如何在JFrame中設置JButton的大小並添加標題和註釋
我還想添加一個標題,而不是我已經完成的應用程序的標題,但是我想在按鈕上添加一個標題,該標題以粗體字顯示雜貨店列表。
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);
}
}
您可以調整'margin'屬性,使用佈局屬性。 *「我想在按鈕上方添加一個以粗體字顯示雜貨清單的標題。」*使用'JLabel',利用它的基本HTML支持 – MadProgrammer