This question already has answers here:
Implementing back/forward buttons in Swing
(3 answers)
Switch Panels in a JFrame
(1 answer)
Java Swing How to Switch Panels? [duplicate]
(1 answer)
Closed 23 days ago.
I'm trying to make a simple java swing app and wonder if it's possible to have one JPanel object and change it's value depending on what i want to display. To discribe what i've tried in code, this is what i have.
JFrame class:
public class Window extends JFrame {
Jpanel mainPanel = new Jpanel();
JPanel currentPanel = new JPanel();
public Window(){
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setResizable(false);
this.setLocationRelativeTo(null);
this.setVisible(true);
Button bt1 = new Button("bt1");
bt1.setBounds(x, y, 75, 25);
bt1.addActionListener(e->{
currentPanel = new InitPanel();
repaint();
});
mainPanel.add(bt1);
Button bt2 = new Button("bt2");
bt2.setBounds(x, y, 75, 25);
bt2.addActionListener(e->{
currentPanel = new TestPanel();
repaint();
});
mainPanel.add(bt2);
mainPanel.add(currentPanel);
this.add(mainPanel);
}
}
And then i have my "InitPanel" and "TestPanel" set up like this
public class InitPanel extends JPanel {
public InitPanel(){
this.setBounds(0,0, width, height);
this.setFocusable(true);
this.setBackground(Color.BLACK);
this.setLayout(null);
this.setVisible(true);
}
}
Except the "TestPanel" class have white background just to try it out.
What im trying to accomplish is to be able to change "currentPanel" to any custom made extended JPanel objekt so it will be easy to implament new JPanels as well as use them during run time depending on what the user do
Related
So basically when I add a button it essentially pushes the black rectangle drawn in this program down, putting it out of its given location. How would you fix this?
import javax.swing.*;
import java.awt.*;
public class Grid {
public class homeGraphics extends JComponent {
homeGraphics() {
setPreferredSize(new Dimension(450, 600));
}
public void paint(Graphics g) {
super.paint(g);
g.fillRect(200, 275, 50, 50);
}
}
public void homeFrame() {
JFrame frame1 = new JFrame();
frame1.setSize(450, 600);
frame1.setResizable(false);
frame1.setDefaultCloseOperation(frame1.EXIT_ON_CLOSE);
JButton playButton = new JButton("Play");
playButton.setPreferredSize(new Dimension(60, 30));
JPanel panel1 = new JPanel();
panel1.add(playButton);
panel1.add(new homeGraphics());
frame1.add(panel1);
frame1.setVisible(true);
}
public static void main(String args[]) {
Grid frame = new Grid();
frame.homeFrame();
}
}```
it essentially pushes the black rectangle drawn in this program down, putting it out of its given location.
What do you mean out of its location? Painting is always done relative to the component. So your painting will always be done at (200, 275) of the component.
If you are attempting to paint at (200, 275) relative to the "frame", then don't. That is NOT how painting works.
Other problems with your code:
Don't attempt to set the size of your frame. If the custom panel is (450, 600) how can the frame possibly be the same size? The frame also contains the "title bar" and "borders". Instead of using setSize(), you invoke frame.pack()just beforeframe1.setVisible(….)`.
Class names start with an upper case character. Learn by example. Have you ever seen a class name in the JDK that doesn't start with an upper case character?
Custom painting is done by overriding paintComponent(…), not paint().
By default a JPanel uses a FlowLayout. So what you see it the button on one line and then the "HomeGraphics" class is too big to fit on the same line so it wraps the to the second line.
You should be more explicit when you do frame layout. So your code should be something like:
JPanel wrapper = new JPanel();
wrapper.add( playButton );
//JPanel panel1 = new JPanel();
//panel1.add(playButton);
//panel1.add(new homeGraphics());
JPanel panel1 = new JPanel( new BorderLayout() );
panel1.add(wrapper, BorderLayout.PAGE_START);
panel1.add(new HomeGraphics(), BorderLayout.CENTER);
Now the code shows your layout attempt more clearly.
This question already has answers here:
how to reposition JButton on resizing the window
(3 answers)
Closed 6 years ago.
I was completing an assignment for my Java II class and I wanted to position the button underneath the labels in the JFrame. I tried:
button.setLayout(new FlowLayout());
as well as:
FlowLayout flow = new FlowLayout(FlowLayout.CENTER);
button.setLayout(flow);
and neither affected the position of the button. Positioning the button wasn't required for the assignment so perhaps I am just complicating things for myself, I just thought it would look better centered.
You should Create an JPanel, then set a layout to the JPanel and then add the buttons you need to that panel, and then add that JPanel to the JFrame so you can change the layout of the buttons with affecting the rest of the components.
public class ControlPanel extends JPanel {
private JButton stop_jb;
private JButton start_jb;
public ControlPanel() {
initComponents();
}
private void initComponents() {
//this.setLayout(new GridLayout(0, 2));
this.setLayout(new FlowLayout());
stop_jb = new JButton("Stop");
stop_jb .setVisible(true);
stop_jb .setActionCommand("stop");
this.add(stop_jb );
start_jb = new JButton("Start");
start_jb .setVisible(true);
start_jb .setActionCommand("Start");
this.add(start_jb );
This question already has answers here:
How to set an image as a background for Frame in Swing GUI of java?
(8 answers)
Closed 7 years ago.
Alright, I figured out how to add a background picture but how do i make the buttons show. I'm making pong if you're interested.
Here is my Code:
public class Gui extends JFrame{
private JButton JB;
private JButton EB;
public Gui(){
super("Pong");
JPanel outside = new JPanel();
JPanel inside = new JPanel();
setLayout(new BorderLayout());
this.setContentPane(new JLabel(new ImageIcon("S:\\Music\\Pong title pic.jpg")));
outside.setLayout(new BoxLayout(outside, BoxLayout.LINE_AXIS));
inside.setLayout(new BoxLayout(inside, BoxLayout.PAGE_AXIS));
outside.add(Box.createHorizontalStrut(280));
outside.add(inside);
outside.add(Box.createHorizontalStrut(20));
inside.add(Box.createVerticalStrut(20));
JLabel title = new JLabel("Pong");
inside.add(title);
inside.add(Box.createVerticalStrut(20));
JButton btt1 = new JButton("Start");
Dimension d = new Dimension(200,40);
btt1.setSize(d);
btt1.setMinimumSize(d);
btt1.setMaximumSize(d);
btt1.setPreferredSize(d);
JButton btt2 = new JButton("Credits");
btt2.setSize(d);
btt2.setMinimumSize(d);
btt2.setMaximumSize(d);
btt2.setPreferredSize(d);
JButton btt3 = new JButton("Exit");
btt3.setSize(d);
btt3.setMinimumSize(d);
btt3.setMaximumSize(d);
btt3.setPreferredSize(d);
inside.add(btt1);
inside.add(Box.createVerticalStrut(5));
inside.add(btt2);
inside.add(Box.createVerticalStrut(5));
inside.add(btt3);
inside.add(Box.createVerticalStrut(20));
add(outside);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setSize(800,600);
this.setVisible(true);
this.setResizable(false);
}
}
Thanks for responding.
//setLayout(new BorderLayout());
this.setContentPane(new JLabel(new ImageIcon("S:\\Music\\Pong title pic.jpg")));
setLayout(new BorderLayout());
You set the layout but then you replace the content pane of the frame so you loose the layout manager.
You need to set the layout after you set the content pane.
Note you can only use the label as the background is the image of the label is larger than the components being added to the label.
Also, don't attempt to manipulate the size of the buttons by using setSize(), setPreferredSize(), setMinimumSize() and setMaximumSize(). Let the button display at its preferred size.
If you want the button to all be the same size, then add the buttons to a panel using a GridLayout first. You can specify the spacing between components when you create the GridLayout. Or you can use a GridBagLayout, which will allow you so specify a constraint that "fills" the width of each cell.
I'm trying to make a simple menu for my game. I have 4 buttons in the center and I want to make them a little bit bigger and center them.
The last part worked but I can't seem to call any of my JButtons and do a .setSize / .setPreferedSize(new Dimension()) on it.
public class mainMenu extends JFrame {
private JButton start, highscore, help, stoppen;
public mainMenu() {
super("Master Mind");
maakComponenten();
maakLayout();
toonFrame();
}
private void maakComponenten() {
start = new JButton("Start");
start.setBackground(Color.gray);
highscore = new JButton("Higscores");
help = new JButton("Help");
stoppen = new JButton("Stoppen");
}
private void maakLayout() {
JPanel hoofdmenu = new JPanel();
hoofdmenu.setLayout(new BoxLayout(hoofdmenu, BoxLayout.Y_AXIS ));
hoofdmenu.add(start);
start.setAlignmentX(CENTER_ALIGNMENT);
hoofdmenu.add(highscore);
highscore.setAlignmentX(CENTER_ALIGNMENT);
hoofdmenu.add(help);
help.setAlignmentX(CENTER_ALIGNMENT);
hoofdmenu.add(stoppen);
stoppen.setAlignmentX(CENTER_ALIGNMENT);
super.add(hoofdmenu);
}
private void toonFrame() {
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
setVisible(true);
setSize(500,500);
}
public static void main(String[] args) {
new mainMenu();
}
}
As an example, to change the size of the "Start" button,
change :
start1 = new JButton("Start");
to
start1 = new JButton("Start") {
{
setSize(150, 75);
setMaximumSize(getSize());
}
};
The problem is that JFrames use BorderLayout by default, which means that your JPanel will naturally fill the space.
Before adding your JPanel, call the following code to change the JFrame's layout to null and use the JPanel's settings instead.
this.setLayout(null);
JPanel hoofdmenu = new JPanel();
hoofdmenu.setBounds(0,0, 400, 100);
Alternatively, you could set the maximum size of the JButtons
Dimension maxSize = new Dimension(100, 100);
start.setMaximumSize(maxSize);
highscore.setMaximumSize(maxSize);
help.setMaximumSize(maxSize);
stoppen.setMaximumSize(maxSize);
Here's another example following behind the previous two - I'm making a soundboard program, and this is actually a sample from it - The JPanel actually is needed, agreeing to the second post.
JFrame frame = new JFrame();
JPanel menuPanel = new JPanel();
JButton Button1 = new JButton("<BUTTON NAME 1>");
Button1.setSize(80, 30);
Button1.setLocation(4, 4);
JButton Button2 = new JButton("<BUTTON NAME 2>");
Button2.setSize(80, 30);
Button2.setLocation(90, 4);
Ah, and another thing - You created the buttons in a different block from the second piece of code. Doing that causes the other blocks to not see it. You need to declare them outside the block so all the blocks can see them.
I've created simple JDialog to gain initial data for my application. Elements (JLabel, JTextField and JButton) are arranged by BoxLayout inside the BorderLayout. (Code at the end). So far it looks like this:
I have two problems:
I would like to center JButton in it's row. I tried startBtn.setAlignmentX(Component.CENTER_ALIGNMENT);, but it doesn't work properly, mess appears.
I want to add some left/right padding to TextField. First solution from this topic works fine, but other elements are moved right left padding value.
Can anybody give a hint how to place it? I'm new to Java and have no idea.
Here's code of my InitDialog class:
public class InitDialog extends JDialog {
JTextField dataTF;
JButton startBtn;
public InitDialog(JFrame owner) {
super(owner, "Rozpocznij test", Dialog.ModalityType.DOCUMENT_MODAL);
initUI();
}
public final void initUI() {
System.out.println("InitDialog::initUI");
JPanel outer = new JPanel(new BorderLayout());
JPanel inner = new JPanel();
outer.setBorder(new EmptyBorder(new Insets(20, 20, 20, 20)));
JLabel msg = new JLabel("<html>Podaj ilości liczb w zestawach testowych<br />(przedzielone średnikiem):");
inner.add(msg);
inner.add(Box.createVerticalStrut(15));
dataTF = new JTextField();
dataTF.setBorder(null);
dataTF.setText("50; 100; 200");
inner.add(dataTF);
inner.add(Box.createVerticalStrut(15));
startBtn = new JButton("Rozpocznij test");
inner.add(startBtn);
inner.setLayout(new BoxLayout(inner, BoxLayout.Y_AXIS));
outer.add(inner);
add(outer);
setSize(300, 180);
//setDefaultCloseOperation(DISPOSE_ON_CLOSE);
addWindowListener(new WindowAdapter() {
#Override public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
setResizable(false);
setLocationRelativeTo(getRootPane());
}
}
BoxLayout alignment is not what you think it is.
To get what you want this is the line you need
msg.setAlignmentX(Component.CENTER_ALIGNMENT);