so im having trouble moving components to a certain part of the jframe, ive tried using BorderLayout and setBounds() but both of those don't seem to work.
// sets the start button
start = new JButton(imgStart);
start.setPreferredSize(new Dimension(150, 55));
start.setFocusable(false);
start.addActionListener(this);
// sets the controls button
controls = new JButton(imgControl);
controls.setPreferredSize(new Dimension(150, 55));
controls.setFocusable(false);
controls.addActionListener(this);
controls.setBounds(151, 56, 0, 0);
JFrame frame = new JFrame();
frame.setContentPane(this);
frame.add(start);
frame.add(controls, BorderLayout.WEST);
frame.setTitle("Freedom Planet");
frame.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
frame.addWindowListener(this);
frame.setResizable(false);
frame.setSize(imgBackground.getIconWidth(), imgBackground.getIconHeight());
frame.setLocationRelativeTo(null);
frame.setVisible(true);
I also have the some problem with a panel im using in a different part of my program
// creates a panel called Info and and adds components to them
JPanel Info = new JPanel();
//Info.setBorder(BorderFactory.createLineBorder(Color.black, 5));
Info.setLayout(new FlowLayout(FlowLayout.LEFT, 30, 10));
//Info.setBounds(290, 180, -10, 10);
Info.setOpaque(false);
Info.add(lblTime);
Info.add(lblTimer);
Info.add(lblScore);
Info.add(lblShowScore);
addKeyListener(this);
setFocusable(true);
JFrame frame = new JFrame();
frame.setLayout(new BorderLayout());
frame.setContentPane(this);
frame.add(Info, BorderLayout.SOUTH);
frame.add(lblLevel);
frame.setTitle("Freedom Planet");
frame.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
frame.addWindowListener(this);
frame.setResizable(false);
frame.setSize(imgBackground[0].getIconWidth(), imgBackground[0].getIconHeight());
frame.setLocationRelativeTo(null);
frame.setVisible(true);
I'm trying to move the components horizontally
Then maybe you can use a Swing Border. An EmptyBorder will allow you to add extra space around the components.
Related
I was working on a java swing gui project for my course. When I was doing that, I found that I had too much information in a panel and it was not able to show everything. As a result, I wanted to add a Jscrollpane and I did some research about how to use this function but it didn't seem to work for my project, and I had no reason why even after I tried almost everything I could find on google.
Here is my code:
JFrame frame = new JFrame();
JPanel listpanel = new JPanel();
JScrollPane musiclist = new JScrollPane();
JButton selectButton = new JButton("Select song");
frame.setTitle("Music Player");
frame.getContentPane().setBackground(Color.DARK_GRAY);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLayout(null);
frame.setBounds(190,100,840,600);
Container c = frame.getContentPane();
musiclist.setViewportView (listpanel);
musiclist.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
musiclist.setPreferredSize(new Dimension(10, 1100));
musiclist.setBounds(0, 0, 190, 1000);
musiclist.setLayout(null);
listpanel.setLayout(null);
listpanel.setBackground(Color.GRAY);
listpanel.setBounds(0, 10, 160, 600);
selectButton.setBounds(55,20,100,30);
selectButton.setOpaque(true);
selectButton.setBackground(Color.LIGHT_GRAY);
selectButton.setBorder(null);
selectButton.setBorderPainted(false);
listpanel.add(selectButton);
c.add(musiclist, BorderLayout.WEST);
frame.setVisible(true);
musiclist.setVisible(true);
The problem now is that the scroll bar never shows up even I set the vertical to always show. I am just new to java, so any help will be helpful and thank you all for your time!
JFrame frame = new JFrame();
JPanel listpanel = new JPanel();
JScrollPane musiclist = new JScrollPane();
JButton selectButton = new JButton("Select song");
frame.setTitle("Music Player");
frame.getContentPane().setBackground(Color.DARK_GRAY);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setBounds(190,100,840,600);
Container c = frame.getContentPane();
musiclist.setViewportView (listpanel);
musiclist.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
musiclist.setPreferredSize(new Dimension(100, 1100));
musiclist.setBounds(0, 0, 190, 1000);
listpanel.setBackground(Color.GRAY);
listpanel.setBounds(0, 10, 160, 600);
selectButton.setBounds(55,20,100,30);
selectButton.setOpaque(true);
selectButton.setBackground(Color.LIGHT_GRAY);
selectButton.setBorder(null);
selectButton.setBorderPainted(false);
JList mlist=new JList();
DefaultListModel lmodel=new DefaultListModel();
lmodel.addElement("Song 1");
lmodel.addElement("Song 2");
lmodel.addElement("Song 3");
mlist.setModel(lmodel);
listpanel.setLayout(new BorderLayout());
listpanel.add(mlist, BorderLayout.CENTER);
listpanel.add(selectButton, BorderLayout.SOUTH);
c.add(musiclist, BorderLayout.WEST);
musiclist.setVisible(true);
frame.setVisible(true);
I am trying to set the sizes of my JButtons in a JPanel with BoxLayout correctly, but the behavior is beyond weird.
It will take the height from JButton.setPreferredSize, but completely ignore the width. This also only works when all buttons are set to the same height. As soon as one is smaller, it will revert all of them to some random minimum size (which isn't even the same for all buttons)
My code is this:
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(800, 500);
JPanel rightPanel = new JPanel();
JPanel leftPanel = new JPanel();
leftPanel.setLayout(new BoxLayout(leftPanel, BoxLayout.PAGE_AXIS));
JButton bBookmarks = new JButton("Bookmarks");
bBookmarks.setPreferredSize(new Dimension(200, 100));
//more buttons with same size
leftPanel.add(bBookmarks);
//more buttons
JSplitPane mainPanel = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, leftPanel, rightPanel);
mainPanel.setDividerLocation(200);
frame.add(mainPanel);
frame.setResizable(false);
frame.setVisible(true);
This creates this image.
The middle button is always wider than the rest as well. Using frame.pack() doesn't do anything except resizing the frame because the right panel is empty.
What am I doing wrong?
Edit: Should look like this:
Divide and conquer: break the design into small, easy to layout containers. In this case do not place the buttons directly in the left (BoxLayout) container but in a nested JPanel using GridLayout manager.
This ensures that all buttons have the same size:
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLocationRelativeTo(null);
//add all buttons to a panel using a GridLayout which shows all components having the same size
JPanel buttons = new JPanel(new GridLayout(0,1));
JButton bBookmarks = new JButton("Bookmarks"); buttons.add(bBookmarks);
JButton bPlotter = new JButton("Plotter"); buttons.add(bPlotter);
JButton bShips = new JButton("Ships"); buttons.add(bShips);
//add buttons and text area to a panel using BoxLayout
JPanel leftPanel = new JPanel();
leftPanel.setPreferredSize(new Dimension(100,400));
leftPanel.setLayout(new BoxLayout(leftPanel, BoxLayout.PAGE_AXIS));
leftPanel.add(buttons);
leftPanel.add(new TextArea(10,30));
JPanel rightPanel = new JPanel();
rightPanel.setPreferredSize(new Dimension(600,400));
rightPanel.add(new JLabel("right pane"));
JSplitPane mainPanel = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT,true, leftPanel, rightPanel);
frame.add(mainPanel);
frame.pack();
frame.setVisible(true);
I have created a JScrollPane with a RowHeaderView, a ColumnHeaderView and a ViewPortView. I added JPanels in diffrent colors and noticed, that there is one cornor left, on the upper-left where you cant just add a Component. I wanted to ask, how it is possible to add a Component there.
Here a image. The area I mean is green:
And here my Code:
public class Example {
public static void main(String[] args) {
JFrame frame = new JFrame();
frame.setSize(1000, 800);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLocationRelativeTo(null);
JPanel panel0 = new JPanel();
panel0.setBackground(Color.yellow);
JPanel panel1 = new JPanel();
panel1.setBackground(Color.red);
panel1.setPreferredSize(new Dimension(30, 200));
JPanel panel2 = new JPanel();
panel2.setBackground(Color.blue);
panel2.setPreferredSize(new Dimension(200, 30));
JScrollPane scrollPane = new JScrollPane();
scrollPane.setViewportView(panel0);
scrollPane.setRowHeaderView(panel1);
scrollPane.setColumnHeaderView(panel2);
scrollPane.setBackground(Color.green);
frame.add(scrollPane);
frame.setVisible(true);
}
}
It's easy. Use the method setCorner
scrollPane.setCorner(JScrollPane.UPPER_LEFT_CORNER, new JButton());
I am new to Java, so this question might be obvious.
I have this Initialization code:
frame = new JFrame();
frame.setTitle("Test");
frame.setBounds(100, 100, 512, 468);
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
JPanel panel = new MyJPanel();
FlowLayout flowLayout = (FlowLayout) panel.getLayout();
panel.setAlignmentY(Component.TOP_ALIGNMENT);
panel.setAlignmentX(Component.LEFT_ALIGNMENT);
panel.setPreferredSize(new Dimension(0, 0));
panel.setMinimumSize(new Dimension(0, 0));
frame.getContentPane().add(panel, BorderLayout.CENTER);
JMenuBar menuBar = new JMenuBar();
frame.getContentPane().add(menuBar, BorderLayout.NORTH);
JMenu mnFile = new JMenu("File");
menuBar.add(mnFile);
mntmOpenBBinary.setPreferredSize(new Dimension(149, 22));
mnFile.add(mntmOpenBBinary);
JSeparator separator = new JSeparator();
mnFile.add(separator);
JMenuItem mntmExit = new JMenuItem("Exit");
mntmExit.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
System.exit(0);
}
});
mnFile.add(mntmExit);
MyJPanel is a custom class that extends the JPanel class. Just as a test, it just writes "test" to the screen in the paintComponent method:
public void paintComponent(Graphics g)
{
super.paintComponent(g);
g.setColor(Color.BLACK);
g.setFont(RenderFont);
g.drawString("TEST", 1, 1);
}
You can see from the image below that, for some reason, the drawString method is drawing behind the menu. The coordinates I give in drawString, I'd think, would be the coordinates relative to the JPanel window. Also, the JPanel is "filling" the entire space of the JFrame. I'd prefer that my MyJFrame be only 100x100, but it seems to always want to auto fill the JFrame. How can I solve these 2 issues?
The text is hidden by the menu bar because last parameter of drawString() is the text baseline, and not the upper bound: http://docs.oracle.com/javase/7/docs/api/java/awt/Graphics2D.html#drawString(java.lang.String,%20int,%20int)
So you need to use something like:
g.drawString("TEST", 1, 50);
Or better, use Font.getStringBounds() to compute your text height:
Rectangle2D textBounds = g.getFont().getStringBounds("TEST", (((Graphics2D) g).getFontRenderContext());
And to avoid having your panel taking all Frame space, replace:
frame.getContentPane().add(panel, BorderLayout.CENTER);
with:
JPanel mainPanel = new JPanel(new BorderLayout());
mainPanel.add(panel, BorderLayout.NORTH);
frame.getContentPane().add(mainPanel, BorderLayout.CENTER);
Although, you should not need this:
panel.setAlignmentY(Component.TOP_ALIGNMENT);
panel.setAlignmentX(Component.LEFT_ALIGNMENT);
panel.setPreferredSize(new Dimension(0, 0));
panel.setMinimumSize(new Dimension(0, 0));
I've been at this for a good while now, but I cannot seem to get a hand of it. I'm trying to produce a JPanel that has a JTextArea above and two JLabels below, but my JLabel ends up on the left side of my JTextArea and I cannot make the other appear.
Here's my code (sorry for the display stuff- just filler really):
public JPanel contentPane() {
JPanel something = new JPanel();
String information = "Please";
info = new JTextArea(information, 4, 30);
info.setEditable(false);
info.setLineWrap(true);
info.setWrapStyleWord(true);
JPanel one = new JPanel(new BorderLayout());
one.setBackground(Color.WHITE);
one.setLocation(10, 10);
one.setSize(50, 50);
one.add(info, BorderLayout.CENTER);
something.add(one, BorderLayout.NORTH);
JPanel two = new JPanel(new BorderLayout());
two.setBackground(null);
two.setLocation(220, 10);
two.setSize(50, 50);
two.add(new JLabel("Please work"), BorderLayout.EAST);
two.add(new JLabel("Oh gosh, please"), BorderLayout.WEST);
something.add(two, BorderLayout.SOUTH);
something.setOpaque(true);
return something;
}
public static void GUI() {
JFrame frame = new JFrame("You Guessed It!");
DisplayStudent panel = new DisplayStudent();
frame.setContentPane(panel.contentPane());
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(400, 150);
frame.setVisible(true);
}
Please and thank you to anyone who takes the time to help.
When you create you something, you don't specify any layout manager, but later on you attempt to add one to something using BorderLayout constants -- which will not work, since the default layout manager for a JPanel is FlowLayout.
Try this instead;
JPanel something = new JPanel(new BorderLayout());