相关疑难解决方法(0)

扩展JFrame与在程序中创建它

在使用Swing创建应用程序时,我看到人们会做两件事之一来创建JFrame.哪种方法更好,为什么?

我是Java和编程的初学者.我唯一的学习来源是书籍,YouTube和Stack Overflow.

import {imports};

public class GuiApp1 {

    public static void main(String[] args) {

        new GuiApp1();
    }

    public GuiApp1()  {
        JFrame guiFrame = new JFrame();

        guiFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        guiFrame.setTitle("Example GUI");
        guiFrame.setSize(300,250);
        ................
    }
Run Code Online (Sandbox Code Playgroud)

import {imports};

public class GuiApp1 extends JFrame {

    public Execute() {
        getContentPane().setBackground(Color.WHITE);
        getContentPane().setLayout(null);
        setSize(800, 600);
        .............
    }

    public static void main(String[] args) {
        Execute frame1 = new Execute();
        frame1.setVisible(true);

    }
}
Run Code Online (Sandbox Code Playgroud)

java oop inheritance swing composition

27
推荐指数
4
解决办法
1万
查看次数

JButton 在其边框和按钮本身之间有填充

我有一个 JButton,其背景为绿色,边框为 LineBorder。我想在按钮和边框之间插入一个空格,一种填充。我试过 setMargin(new Insets(x,y,t,z)) 但它似乎不起作用。这是我的一段代码。

JButton JBtn=new JButton("sdfd");
JBtn.setBorder(BorderFactory.createLineBorder(Color.CYAN,5));
JBtn.setBackground(Color.GREEN);
JBtn.setMargin(new Insets(5,5,10,10));
Run Code Online (Sandbox Code Playgroud)

有什么建议吗?

java swing border padding jbutton

5
推荐指数
2
解决办法
2万
查看次数

标签 统计

java ×2

swing ×2

border ×1

composition ×1

inheritance ×1

jbutton ×1

oop ×1

padding ×1