向GUI Java添加框架

向GUI Java添加框架,java,swing,user-interface,jframe,Java,Swing,User Interface,Jframe,好吧,这可能是一个愚蠢的问题,但我对GUI和Java一般都是新手。在我的GUI中,我创建了框架,因为我在哪里看不到JFrame。或者我必须创建一个JFrame并把我所有的东西都放在上面。我需要一个JFrame来完成最小化屏幕、更改图标等操作。感谢您的帮助 private ImageIcon bgi; private JLabel bgl; private JButton startButton; private JButton helpButton; private JButton back

好吧,这可能是一个愚蠢的问题,但我对GUI和Java一般都是新手。在我的GUI中,我创建了框架,因为我在哪里看不到JFrame。或者我必须创建一个JFrame并把我所有的东西都放在上面。我需要一个JFrame来完成最小化屏幕、更改图标等操作。感谢您的帮助

private ImageIcon bgi;
private JLabel bgl;


private JButton startButton;
private JButton helpButton;
private JButton backButton;
private final Action action = new SwingAction();


public static void main(String[] args) throws MalformedURLException, IOException {
    TwitterUnfollower gui = new TwitterUnfollower ();
    gui.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // when click x close program
    gui.setSize(902, 305);

    gui.setVisible(true);
    gui.setTitle("Solid Cloud Inc - Twitter Unfolower");
}

public TwitterUnfollower() throws MalformedURLException, IOException{

    bgi = new ImageIcon(getClass().getResource("tu.png"));
    getContentPane().setLayout(null);
    BufferedImage img = ImageIO.read(new URL("http://i1344.photobucket.com/albums/p656/SolidCloudInc/start_zpsf3781681.png"));
    //ImageIcon start = new ImageIcon(getClass().getResource("start.png"));
    startButton = new JButton("");
    startButton.setIcon(new ImageIcon(img));
    startButton.setBounds(22, 186, 114, 50);


    getContentPane().add(startButton);

    BufferedImage img2 = ImageIO.read(new URL("http://i1344.photobucket.com/albums/p656/SolidCloudInc/help_zpsc4fad867.png"));
    helpButton = new JButton("");
    helpButton.setIcon(new ImageIcon(img2));
    helpButton.setBounds(192, 186, 114, 50);

    getContentPane().add(helpButton);

    BufferedImage img3 = ImageIO.read(new URL("http://i1344.photobucket.com/albums/p656/SolidCloudInc/back_zps9d62b65b.png"));
    backButton = new JButton("");
    backButton.setIcon(new ImageIcon(img3));
    backButton.setBounds(105, 205, 82, 44);
    backButton.setBorder(BorderFactory.createEmptyBorder());
    backButton.setContentAreaFilled(false);
    backButton.setVisible(false);

    getContentPane().add(backButton);

    bgl = new JLabel (bgi);
    bgl.setBounds(0, 0, 886, 272);
    getContentPane().add(bgl);

    Events e = new Events();
    startButton.addActionListener(e);
    helpButton.addActionListener(e);
    backButton.addActionListener(e);
}

}

我确实有一个动作监听器,我从代码中删除了它以缩短它。我知道我应该避免空布局,但我使用的是WindowBuilder,这可能会改变。再次感谢

你的意思是这样的:

import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;

import javax.imageio.ImageIO;
import javax.swing.BorderFactory;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;

public class TwitterUnfollower extends JFrame {
    private ImageIcon bgi;
    private JLabel bgl;

    private JButton startButton;
    private JButton helpButton;
    private JButton backButton;

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    TwitterUnfollower gui = new TwitterUnfollower();
                    gui.setTitle("Solid Cloud Inc - Twitter Unfolower");
                    gui.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // when click x close program
                    gui.pack();
                    gui.setVisible(true);
                } catch (MalformedURLException e) {
                    e.printStackTrace();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        });
    }

    public TwitterUnfollower() throws MalformedURLException, IOException {

        bgi = new ImageIcon(ImageIO.read(new URL(
                "http://content.mcfc.co.uk//~/media/Images/Home/News/Club%20news/2012/twitter%20background%20new.ashx")));
        bgl = new JLabel(bgi);
        bgl.setLayout(new BorderLayout());
        getContentPane().setLayout(new FlowLayout(FlowLayout.LEFT, 0, 0));
        getContentPane().add(bgl);

        BufferedImage img = ImageIO.read(new URL("http://i1344.photobucket.com/albums/p656/SolidCloudInc/start_zpsf3781681.png"));
        // ImageIcon start = new ImageIcon(getClass().getResource("start.png"));
        startButton = getButton(img);

        BufferedImage img2 = ImageIO.read(new URL("http://i1344.photobucket.com/albums/p656/SolidCloudInc/help_zpsc4fad867.png"));
        helpButton = getButton(img2);

        BufferedImage img3 = ImageIO.read(new URL("http://i1344.photobucket.com/albums/p656/SolidCloudInc/back_zps9d62b65b.png"));
        backButton = getButton(img3);

        JPanel alignLeftPanel = new JPanel(new FlowLayout(FlowLayout.LEFT, 0, 0));
        alignLeftPanel.setOpaque(false);
        JPanel buttonPanel = new JPanel(new GridLayout(1, 0));
        buttonPanel.setOpaque(false);
        buttonPanel.add(startButton);
        buttonPanel.add(helpButton);
        buttonPanel.setBorder(BorderFactory.createEmptyBorder(20, 20, 20, 20));
        alignLeftPanel.add(buttonPanel);
        bgl.add(alignLeftPanel, BorderLayout.SOUTH);
    }

    private JButton getButton(BufferedImage img) {
        JButton button = new JButton(new ImageIcon(img));
        button.setContentAreaFilled(false);
        button.setBorderPainted(false);
        button.setFocusPainted(false);
        return button;
    }

}
重要提示:与其使用只会给您带来麻烦的
null
LayoutManager,不如使用LayoutManager


警告:photobucket返回状态503,因此我无法再测试此代码。

TwitterUnfollower似乎是您的框架
TwitterUnfollowerGUI=new TwitterUnfollower();setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE)
我的第一个猜测是,
TwitterUnfollower
实际上扩展了JFrame。为了更快地获得更好的帮助,请发布一个。此代码不可编译。TwitterUnfollower应该是一个扩展JFrame的类。此外,您还应该仔细阅读LayoutManager,并去掉setBounds()调用(它不会飞起来)。