I am currently building a game and I don't know how to make a main menu. I don't know how to put the text on the GIF background. Now the thing is when I try to put them on I try this code:
private void initializeDisplay() {
playBtn.setBorderPainted(false);
playBtn.setFocusPainted(false);
playBtn.setContentAreaFilled(false);
playBtn.setText("PLAY");
playBtn.setFont(new Font("Poiret One", Font.TRUETYPE_FONT, 27));
playBtn.setForeground(Color.WHITE);
mainPanel.add(playBtn);
mainPanel.add(bgImg);
mainPanel.setLayout(new GridBagLayout());
bgImg.setLocation(new Point(200, 200));
mainPanel.setOpaque(true);
mainPanel.addKeyListener(new Keyboard());
mainPanel.addMouseListener(new Mouse());
mainPanel.setFocusable(true);
}
Also when I do that I use a GridBagLayout for the layout type but it seems that I'm not get the result I want, the problem is the button not the image in the background because that's appearing but the button isn't appearing.
You could also add an image to a jlabel's icon field, and then add the label to your panel's content pane.
This post explains it more:
How to set a background picture in JPanel
Related
Hi so I'm struggling to apply a JScrollPanel within a JEditorPane.
I have a main content window (which has a scroll bar) and within that a JEditorPane that I would like to scroll slide horizontal and vertical. So far I have
JEditorPane monApp = new JEditorPane();
and have tried
ScrollablePanel panel = new ScrollablePanel();
panel.setScrollableWidth( ScrollablePanel.ScrollableSizeHint.FIT );
along with a few other things but couldn't seem to get any of them working.
I am designing a file browser using Java Swing, and here is what I have so far:
I have a JFileChooser in a panel, however it stays the same size when I reshape the window.
However, I want to make it look like this:
Is it possible to make the actual Browser box resize along with the form?
EDIT: I do not want a popup JFileChooser, the JFileChooser is INSIDE the Frame.
You don't need to add the file chooser to a panel - if you just initialize one and set it visible, it will automatically be resizable.
JFileChooser chooser = new JFileChooser();
chooser.setVisible(true);
int returnVal = chooser.showOpenDialog(null);
if (returnVal == JFileChooser.APPROVE_OPTION) {
//continue your code here
To incorporate the file chooser into the panel, try this:
JFrame frame = new JFrame();
JPanel panel = new JPanel(new BorderLayout());
JFileChooser chooser = new JFileChooser();
panel.add(chooser);
frame.add(panel);
frame.pack();
frame.setVisible(true);
I'm not sure how you used the BorderLayout before but this code works perfectly on my computer.
If you just add the file chooser panel to a panel it will retain its preferred size because by default a panel uses a FlowLayout.
Try adding the file chooser panel to the CENTER of a panel using a BorderLayout. Then hopefully the components will resize as you size the frame (assuming the file chooser panel uses appropriate layout managers).
I am learning Java.I just created an application (liked desktop one in c#) by adding some labels,a text view and a button.
Its fun learning this new thing,but i soon encountered an issue ,when u tried to add vertical scrolling to the text view i added on the UI.
I also tried adding vertical scroll to the text area but still my text area is not displaying the scroll bar.
The part of the code created when i added the controls from the panel to the UI is as below:
thisLayout.setVerticalGroup(thisLayout.createSequentialGroup()
.addContainerGap(17, 17)
.addComponent(getJtxtArea(), GroupLayout.PREFERRED_SIZE, 158, GroupLayout.PREFERRED_SIZE)
The code for the function getJtxtArea() is as below:
private JTextArea getJtxtArea() {
if(jtxtArea == null) {
jtxtArea = new JTextArea();
jtxtArea.setBackground(new java.awt.Color(255,255,255));
jtxtArea.setFont(new java.awt.Font("Segoe UI",3,14));
jtxtArea.setWrapStyleWord(true);
jtxtArea.setLineWrap(true);
JScrollPane scroll = new JScrollPane(jtxtArea);
scroll.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
}
return jtxtArea;
}
Can anyone tell me why i am not getting the scroll bar on the text view.Thanks in advance.
Note:I am using Eclipse Helios as the IDE and using Jigloo plugin in eclipse for the GUI.
Add the component scroll instead of the jtxtArea. In addition to that, you might also want to resize your JScrollPane.
Hello I have a main layout made in this way:
| |
Main pane |Menu pane|
| |
Now, menu pane is made just of button: clicking on a button switches main panel with another panel.
On click event is made this way:
public void actionPerformed(ActionEvent evt){
mainPanel = new MyNewPanel();
this.revalidate();
}
But, for some reason, main panel does not change!
You are not setting the main component of your container. You need to add your new panel and call validate() on that container.
Note, depending on your the layout of your container you may need to remove the currently visible component first.
CardLayout could manage all this for you.
JOptionPane.showConfirmDialog(this,
message,
"title",
JOptionPane.YES_NO_OPTION,
JOptionPane.WARNING_MESSAGE);
The message can be 10 lines and message can be 500 lines. It changes dynamically. I want to implement a scroll bar if the message exceeds the screen height.
So I tried:
JTextArea textArea = new JTextArea (message);
JscrollPane scrollPane = new JScrollPane(textArea,JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED,JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
JOptionPane.showConfirmDialog(this,
scrollPane,
"title",
JOptionPane.YES_NO_OPTION,
JOptionPane.WARNING_MESSAGE);
This will open a dialog and a scroll bar in windows and it works fine, but in mac os the dialog goes out of screen.
Can any one help me?
You can set the preferred size for your scrollPane before showing the dialog to limit its size:
scrollPane.setPreferredSize(new Dimension(400, 200));
The dimension to use can be based on the screen size that you can retrieve like this:
Toolkit.getDefaultToolkit().getScreenSize()
Note that I have not tested this on other platforms than Mac OS X.