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));
Related
I have a Panel which I have made scrollable in my frame.
What I need is to add a button that stays fixed in the lower right corner even when I scroll.
I'm new to Java Swing so would appreciate all and any help that I can get.
mainPanel = new SimulationPanel(); //class SimulationPanel extends JPanel
//making mainPanel scrollable
mainPanel.setPreferredSize(new Dimension(((int)(WIDTH*1.2)), HEIGHT));
JScrollPane scrollPane = new JScrollPane(mainPanel);
scrollPane.setViewportView(mainPanel);
// Settings for JFrame
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
frame = new JFrame("Warehouse Simulator");
frame.setContentPane(scrollPane);
frame.setSize(screenSize.width, screenSize.height);
frame.setResizable(true);
frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
frame.setVisible(true);
I would use nested panels with the outer one be with BorderLayout. Then one with FlowLayout and align FlowLayout.RIGHT and the button inside it.
public class Example extends JFrame {
public Example() {
super("");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLayout(new BorderLayout());
JTextArea textArea = new JTextArea(10000, 0);
JScrollPane scrollPane = new JScrollPane(textArea);
add(scrollPane, BorderLayout.CENTER);
JButton button = new JButton("button");
JPanel panelWithButton = new JPanel(new FlowLayout(FlowLayout.RIGHT));
panelWithButton.add(button);
add(panelWithButton, BorderLayout.PAGE_END);
setLocationByPlatform(true);
pack();
setSize(600, 600);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(() -> {
new Example().setVisible(true);
});
}
}
Result:
I would go for a BoxLayout. Add another panel (metaPanel) in which your first put your scrollingPanel, and then you add a button. Instead of usgin scrollingPanel as contentPane, you use metaPanel. Example (the example works, but you need to modify it to make the interface look nice):
JPanel mainPanel = new JPanel();
JScrollPane scrollPane = new JScrollPane(mainPanel);
scrollPane.setViewportView(mainPanel);
JPanel metaPanel = new JPanel();
BoxLayout boxlayout = new BoxLayout(metaPanel, BoxLayout.Y_AXIS);
metaPanel.setLayout(boxlayout);
metaPanel.add(scrollPane);
metaPanel.add(new JButton("button"));
// Settings for JFrame
frame = new JFrame("Warehouse Simulator");
frame.setContentPane(metaPanel); // Put metaPanel here
frame.setSize(500, 300);
frame.setResizable(true);
frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
frame.setVisible(true);
I just wanted to know if it's possible to paint over the JXBrowser component? I have searched on the internet and found that the used BrowserView inherits paintcomponents etc. But I can't seem to get it to work.
Here's the code:
public test() {
browser = new Browser();
view = new BrowserView(browser);
JPanel panel = new JPanel();
panel.setLayout(new BorderLayout());
panel.add(view, BorderLayout.CENTER);
test = new JButton("Open FOE");
test.addActionListener(this);
test1 = new JButton("Helpen");
test1.addActionListener(this);
test1.setMaximumSize(new Dimension(90, 20));
JPanel panel1 = new JPanel();
panel1.setLayout(new BoxLayout(panel1, BoxLayout.Y_AXIS));
panel1.add(test);
panel1.add(Box.createRigidArea(new Dimension(0, 15)));
panel1.add(test1);
JPanel panel2 = new JPanel();
panel2.setLayout(new BorderLayout());
panel2.add(panel, BorderLayout.CENTER);
panel2.add(panel1, BorderLayout.EAST);
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
frame.add(panel2);
frame.setSize(1500, 1000);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
How it looks now:
What I want to achieve:
Use glass pane,
sample code fragment:
JPanel glassPane = new JPanel()
{
#Override
protected void paintComponent(Graphics g)
{
super.paintComponent(g);
g.drawString("Test",100,100);
g.setColor(Color.BLUE);
g.drawRect(300,300,300,300);
}
};
glassPane.setOpaque(false);
frame.getGlassPane().setVisible(true);
frame.setGlassPane(glassPane);
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());
public void paintComponent(Graphics g){
g.drawImage(back,0,0,this);
if(winner || loser){
g.setColor(Color.white);
g.setFont(new Font(null,Font.BOLD,55));
g.drawString("SCORE:",350,250);
g.drawString(""+score,600,250);
g.drawImage(enemy,100,500,this);
g.drawImage(enemy2,175,500,this);
g.drawImage(enemy3,250,500,this);
g.drawImage(enemy5,700,515,this);
g.drawImage(enemy6,775,515,this);
g.drawImage(enemy7,850,515,this);
g.drawImage(enemy4,495,515,this);
if(winner){
g.drawImage(winnerPic,200,50,this);
}
else{
g.drawImage(gameOver,220,50,this);
}
g.drawImage(endingTitle,190,590,this);
JButton menu=new JButton("Return to menu");
menu.setSize(200,100);
menu.setLocation(400,400);
}
}
How do I make a button appear on the screen. Please be very detailed, idk how to work with swings layout styles.
First of all you have a container (such as a JPanel for example) that is displayed on a JFrame.
After crating your button, you have to add it to the container. Most of the time you want your container to have a Layout, such as a BorderLayout.
JButton menu = new JButton("Back to the menu");
container.add(menu, BorderLayout.CENTER);
EDIT:
If this isn't the way you want to implement it, it will atleast help you understand the hierarchy.
public void buttonExample(){
JFrame frame = new JFrame("Button Example");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel panel = new JPanel();
panel.setLayout(new BorderLayout());
panel.setPreferredSize(new Dimension(200, 100));
JButton button = new JButton("Return to the menu");
panel.add(button, BorderLayout.CENTER);
frame.add(panel);
frame.pack();
frame.setVisible(true);
}
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.