Reputation: 21
I know there are other similar questions but none of those solutions worked for me or i dont know how to implement those.
I am making a very basic room sketcher, i have a sidepanel with jlabels that i will drag on the bigger jpanel in the future. But how do i make a side JPanel that i can use to click and drag furniture to?
My code looks like this :
public class maj11 {
JSplitPane splitPaneV;
JSplitPane splitPaneH;
JFrame frame;
JPanel panel, panel2;
JLabel l1, l2, l3, l4, l5, l6, l7, l8, l9, l10;
ImageIcon b1, b2, b3, b4, b5, b6, b7, b8, b9, b10;
public static void main(String[] args) {
new maj11();
}
public maj11() {
b1 = new ImageIcon("/Users/a/Desktop/nyabilder/toa.png");
b2 = new ImageIcon("/Users/a/Desktop/nyabilder/badkar.png");
b3 = new ImageIcon("/Users/a/Desktop/nyabilder/bank.png");
b4 = new ImageIcon("/Users/a/Desktop/nyabilder/disk.png");
b5 = new ImageIcon("/Users/a/Desktop/nyabilder/dusch.png");
b6 = new ImageIcon("/Users/a/Desktop/nyabilder/fridge.png");
b7 = new ImageIcon("/Users/a/Desktop/nyabilder/soffa.png");
b8 = new ImageIcon("/Users/a/Desktop/nyabilder/spis.png");
b9 = new ImageIcon("/Users/a/Desktop/nyabilder/wall.png");
b10 = new ImageIcon("/Users/a/Desktop/nyabilder/window.png");
l1 = new JLabel(b1);
l2 = new JLabel(b2);
l3 = new JLabel(b3);
l4 = new JLabel(b4);
l5 = new JLabel(b5);
l6 = new JLabel(b6);
l7 = new JLabel(b7);
l8 = new JLabel(b8);
l9 = new JLabel(b9);
l10 = new JLabel(b10);
frame = new JFrame("Room Designer");
panel = new JPanel();
panel2 = new JPanel();
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(200, 600);
// frame.pack();
panel.setLayout(new GridLayout(5, 2, 10, 10));
panel.setSize(200, 600);
//panel2.setLayout(new BorderLayout());
panel.add(l1);
panel.add(l2);
panel.add(l3);
panel.add(l4);
panel.add(l5);
panel.add(l6);
panel.add(l7);
panel.add(l8);
panel.add(l9);
panel.add(l10);
frame.add(panel);
}
}
the output now is :imgur
and i want it to be: imgur
Upvotes: 1
Views: 37
Reputation: 51445
It helps a lot when you create a Swing GUI in the correct order.
In the main
method, you must call the SwingUtilities
invokeLater
method to put the creation and execution of the Swing components on the Event Dispatch Thread.
Then create a JFrame
. The methods to create a JFrame
must be called in a specific order.
Then create your JPanels
. I usually create a JPanel
in a separate class.
Create your Swing components within a JPanel in row, column order. Keep the method calls pertaining to each Swing component together.
Keeping your GUI code separated helps you to spot errors and help keep your code organized so it's easier to understand.
I colored the ModelingPanel
orange, so you can see the difference between the IconPanel
and the ModelingPanel
. You'll have to add your furniture icons back in the code.
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.GridLayout;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
public class Maj11 implements Runnable {
public static void main(String[] args) {
SwingUtilities.invokeLater(new Maj11());
}
@Override
public void run() {
JFrame frame = new JFrame("Room Designer");
frame.setDefaultCloseOperation(
JFrame.EXIT_ON_CLOSE);
IconPanel iconPanel = new IconPanel();
frame.add(iconPanel.getPanel(),
BorderLayout.BEFORE_LINE_BEGINS);
ModelingPanel modelingPanel =
new ModelingPanel();
frame.add(modelingPanel.getPanel(),
BorderLayout.CENTER);
frame.pack();
frame.setLocationByPlatform(true);
frame.setVisible(true);
}
public class IconPanel {
private JPanel panel;
public IconPanel() {
createPartControl();
}
private void createPartControl() {
panel = new JPanel();
panel.setLayout(
new GridLayout(5, 2, 10, 10));
panel.setPreferredSize(
new Dimension(200, 600));
JLabel l1 = new JLabel(" ");
panel.add(l1);
JLabel l2 = new JLabel(" ");
panel.add(l2);
JLabel l3 = new JLabel(" ");
panel.add(l3);
JLabel l4 = new JLabel(" ");
panel.add(l4);
JLabel l5 = new JLabel(" ");
panel.add(l5);
JLabel l6 = new JLabel(" ");
panel.add(l6);
JLabel l7 = new JLabel(" ");
panel.add(l7);
JLabel l8 = new JLabel(" ");
panel.add(l8);
JLabel l9 = new JLabel(" ");
panel.add(l9);
JLabel l10 = new JLabel(" ");
panel.add(l10);
}
public JPanel getPanel() {
return panel;
}
}
public class ModelingPanel {
private JPanel panel;
public ModelingPanel() {
createPartControl();
}
private void createPartControl() {
panel = new JPanel();
panel.setBackground(Color.ORANGE);
panel.setPreferredSize(
new Dimension(600, 600));
}
public JPanel getPanel() {
return panel;
}
}
}
Upvotes: 1