简体   繁体   English

带有Applet和JPanel菜单的JFrame

[英]JFrame with an Applet and a JPanel Menu

I Have a JPanel and a Applet inside a JFrame, and im tryng to align them like this: 我在JFrame中有一个JPanel和一个Applet,然后尝试像这样对齐它们:

在此处输入图片说明

Im almost loosing my hair on this as it seems so hard to align... 我几乎要把头发弄松了,因为很难对齐...

This is my actual snippet: 这是我的实际代码段:

The JFrame is opening very small with only the button on it. JFrame的打开非常小,仅带有按钮。

 final JFrame f = new JFrame();

    JPanel appletPanel = new JPanel();
    appletPanel.setBackground(Color.RED);

    JPanel menuPanel = new JPanel();
    menuPanel.setBackground(Color.BLUE);


    f.setLayout(new GridBagLayout());
    GridBagConstraints c = new GridBagConstraints();

   // f.setExtendedState(JFrame.MAXIMIZED_BOTH);
   // f.setResizable(false);
    int w = Toolkit.getDefaultToolkit().getScreenSize().width;
    int h = Toolkit.getDefaultToolkit().getScreenSize().height;

    f.setSize(Toolkit.getDefaultToolkit().getScreenSize());
    VNCApplet applet = new VNCApplet();

    menuPanel.add(new JButton("TEST"));

    appletPanel.setSize((int)(w*0.7),h);
    menuPanel.setSize((int)(w*0.3),h);

    c.gridx = 0;
    c.gridy = 0;

    f.getContentPane().add(appletPanel,c);

    c.gridx = 0;
    c.gridy = 1;


    f.getContentPane().add(menuPanel,c);

    f.pack();
    applet.init();
    applet.start();
    f.setVisible(true);

Thanks alot for the attention ! 非常感谢您的关注!

That layout could be achieved a number of ways using a single layout (eg a GridBagLayout or a GroupLayout ) but I'd do it as a combination of layouts. 使用单个布局(例如GridBagLayoutGroupLayout )可以通过多种方式实现该布局,但是我将其作为布局的组合来实现。 Like this: 像这样:

在此处输入图片说明

import java.awt.*;
import java.awt.image.BufferedImage;
import javax.swing.*;
import javax.swing.border.EmptyBorder;
import javax.swing.border.TitledBorder;

public class AppletWithButtonsOnRight {

    private JComponent ui = null;

    AppletWithButtonsOnRight() {
        initUI();
    }

    public void initUI() {
        if (ui!=null) return;

        ui = new JPanel(new BorderLayout(4,4));
        ui.setBorder(new TitledBorder("BorderLayout(4,4)"));

        JPanel appletPanel = new JPanel(new GridLayout());
        appletPanel.setBackground(Color.RED);
        appletPanel.add(new JLabel(new ImageIcon(new BufferedImage(400, 300, BufferedImage.TYPE_INT_ARGB))));
        ui.add(appletPanel);

        JPanel menuPanel = new JPanel(new BorderLayout());
        menuPanel.setBorder(new TitledBorder("BorderLayout()"));
        ui.add(menuPanel, BorderLayout.LINE_END);

        JPanel buttonPanel = new JPanel(new GridLayout(0, 1, 10, 10));
        buttonPanel.setBorder(new TitledBorder("GridLayout(0,1,10,10)"));
        menuPanel.add(buttonPanel, BorderLayout.PAGE_START);

        for (int i=1; i<5; i++) {
            JButton b = new JButton("Button " + i);
            b.setFont(b.getFont().deriveFont(24f));
            buttonPanel.add(b);
        }
    }

    public JComponent getUI() {
        return ui;
    }

    public static void main(String[] args) {
        Runnable r = new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (Exception useDefault) {
                }
                AppletWithButtonsOnRight o = new AppletWithButtonsOnRight();

                JFrame f = new JFrame(o.getClass().getSimpleName());
                f.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
                f.setLocationByPlatform(true);

                f.setContentPane(o.getUI());
                f.pack();
                f.setMinimumSize(f.getSize());

                f.setVisible(true);
            }
        };
        SwingUtilities.invokeLater(r);
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM