Reputation: 1527
I Have a JPanel and a Applet inside a JFrame, and im tryng to align them like this:
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.
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 !
Upvotes: 1
Views: 277
Reputation: 168825
That layout could be achieved a number of ways using a single layout (e.g. a GridBagLayout
or a GroupLayout
) but I'd do it as a combination of layouts. 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);
}
}
Upvotes: 1